0

在我的网站 www.theprinterdepo.com 中,您可以查看页面源代码,我的 seo 咨询建议我将代码移至外部文件。

它是一个建立在 magento 上的电子商务网站。它是一个免费的开源工具,所以我没有开发它,我只是安装了它。

我需要知道代码的作用。

window.HDUSeed='c7025284683262a8eb81056c48968d74';
window.HDUSeedIntId = setInterval(function(){
    if (document.observe) {
        document.observe('dom:loaded', function(){
            for (var i = 0; i < document.forms.length; i++) {
                if (document.forms[i].getAttribute('action') &&  document.forms[i].getAttribute('action').match('contacts/index/post')) {
                    var el = document.createElement('input');
                    el.type = ('hidden');
                    el.name = 'hdu_seed';
                    el.value = window.HDUSeed;
                    document.forms[i].appendChild(el);
                }
            }
        });
        clearInterval(window.HDUSeedIntId)
    }
}, 100);
4

4 回答 4

7

简单来说

该脚本每隔100 毫秒左右调用一个函数(因为不能保证),以尝试验证 DOM 的加载状态以在其上添加一个钩子。

如果加载,它会处理页面中存在的所有表单,寻找具有“动作”属性的表单(通常在某个地方提交,这里contacts/index/post)。

对于找到的所有此类表单,它添加了一个包含“种子”值的新隐藏输入元素,但如果不了解代码库的更多信息,我们无法告诉您它的用途。

详细的代码审查

// seed value, purpose unknown
window.HDUSeed='c7025284683262a8eb81056c48968d74';

// invoke this function every 100ms
//   see: https://developer.mozilla.org/en/DOM/window.setInterval
window.HDUSeedIntId = setInterval(function(){
    // checks if document.observe method exists (added by the Prototype
    // JavaScript library, so we use this here to check its presence or
    // that it's been already loaded)
    if (document.observe) {
        // hook on load status (when the page's DOM has finished loading)
        //   see: http://www.prototypejs.org/api/document/observe
        document.observe('dom:loaded', function(){
            // process all forms contained within the page's context
            //   see: https://developer.mozilla.org/en/DOM/document.forms
            for (var i = 0; i < document.forms.length; i++) {
                // only act on forms with the 'contacts/index/post/' action attribute
                //   see: https://developer.mozilla.org/en/DOM/document.forms
                //   and: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match
                if (document.forms[i].getAttribute('action') && 
                    document.forms[i].getAttribute('action').match('contacts/index/post')) {
                    // create an element...
                    //   see: https://developer.mozilla.org/en/DOM/document.createElement
                    var el = document.createElement('input');
                    el.type = ('hidden');              // ... that is hidden
                    el.name = 'hdu_seed';              // w/ name 'hdu_seed'
                    el.value = window.HDUSeed;         // and the seed value
                    document.forms[i].appendChild(el); // and add it to the end of the form
                }
            }
        });
        // Remove the interval to not call this stub again,
        // as you've done what you want.
        // To do this, you call clearInterval with the ID of the
        // interval callback you created earlier.
        //   see: https://developer.mozilla.org/en/DOM/window.clearInterval
        clearInterval(window.HDUSeedIntId)
    }
}, 100); // 100ms
于 2012-06-06T18:47:52.577 回答
3

看起来像是对外部事物的某种澄清。我的意思是像谷歌分析这样的服务。请记住,如果您为您的网站使用任何第三方服务。如果不是,我建议您删除它,看看会发生什么。如果出现问题或其他问题,只需将代码恢复到文档中即可。

于 2012-06-06T18:48:00.087 回答
3

该代码查找其操作为联系人/索引/发布的表单,并尝试添加一个名为“hdu_seed”的隐藏字段。它看起来像是一种反垃圾邮件措施,可能服务器希望收到它,如果它不存在,则忽略它。这样,不使用 javascript 的机器人将不会包含此字段,并且表单可能会失败。

编辑:实际上一点也不复杂。

于 2012-06-06T18:48:26.367 回答
1

不知道setInterval,clearIntervalobserve函数在这里做什么是逻辑的粗略轮廓

setInterval使用函数和整数调用100 该函数检查observe函数是否已在 this 中加载,document 如果已加载,则observe使用dom:loaded参数和函数调用该函数并调用该clearInterval函数,否则不执行任何操作

内部函数循环遍历form文档中的每一个。循环检查action属性的形式。如果它存在并包含字符串,contacts/index/post那么它会使用of和 a创建一个隐藏input元素,然后 将此元素附加到.namehdu_seedvaluewindow.HDUSeedform

于 2012-06-06T18:48:08.653 回答