GreaseMonkey 中的循环

发布于 2024-10-01 10:46:09 字数 1004 浏览 2 评论 0 原文

我正在开发我的第一个 Greasemonkey 脚本(尝试编辑页面内容并将其添加到特定网站),并且由于某种原因,它拒绝在一个 while 循环之外工作。例如:

var anchorTag = window.document.getElementsByTagName('a');
var anchorTagNumber = window.document.getElementsByTagName('a').length;
..
..
i = 0
j = 0;
function collectId(i,link) {
    linkArray[j] = new Array(2);
    linkArray[j][0] = i;
    linkArray[j][1] = link;
    j++;
}


while(i <= anchorTagNumber)
{
    testHref = anchorTag[i].href;
    testHTML = anchorTag[i].innerHTML;
    patHref = /some regex/;
    patCaptureId = /some regex/;
    testId = patCaptureId.exec(testHref);
    patHTML = /some regex/;
    patHTML2 = /some regex/;
    patHTML3 = /some regex/;
    if(patHref.test(testHref) && !patHTML.test(testHTML) && !patHTML2.test(testHTML))
    {
        linkId = testId[1];
        collectId(i,linkId);
    }
    i++;
}

此 while 循环之后的任何内容结束,拒绝工作。即使是一个简单的警报似乎也无法执行。我有另一个具有类似结构的 while 循环,如果我把它放在第一位,它就会执行,而这个则不会。有什么想法吗?提前致谢 !

I'm developing my first greasemonkey script (trying to edit and add page content to a particular website) and for some reason, it refuses to work beyond one while loop.. Eg :

var anchorTag = window.document.getElementsByTagName('a');
var anchorTagNumber = window.document.getElementsByTagName('a').length;
..
..
i = 0
j = 0;
function collectId(i,link) {
    linkArray[j] = new Array(2);
    linkArray[j][0] = i;
    linkArray[j][1] = link;
    j++;
}


while(i <= anchorTagNumber)
{
    testHref = anchorTag[i].href;
    testHTML = anchorTag[i].innerHTML;
    patHref = /some regex/;
    patCaptureId = /some regex/;
    testId = patCaptureId.exec(testHref);
    patHTML = /some regex/;
    patHTML2 = /some regex/;
    patHTML3 = /some regex/;
    if(patHref.test(testHref) && !patHTML.test(testHTML) && !patHTML2.test(testHTML))
    {
        linkId = testId[1];
        collectId(i,linkId);
    }
    i++;
}

Anything after this while loop ends, refuses to work. Even a simple alert doesn't seem to execute. I have another while loop with a similar structure, and if I put that one first, it executes and this one doesn't. Any Ideas ? Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水微香 2024-10-08 10:46:09

最明显的问题是数组溢出,这将导致脚本出错。

这个:while(i <=anchorTagNumber)
应该是:while(i
例如,如果数组的长度为 5,则其最后一个元素的索引将为 4。

另外,这样:

var anchorTag = window.document.getElementsByTagName('a');
var anchorTagNumber = window.document.getElementsByTagName('a').length;

可以简化为:

var anchorTag       = window.document.getElementsByTagName('a');
var anchorTagNumber = anchorTag.length;

这将稍微加快代码速度,但也使未来的代码维护或重用更加容易。

The most obvious problem is that the array is being overrun, which will cause the script to error-out.

This: while(i <= anchorTagNumber)
Should be: while(i < anchorTagNumber).
If an array has length 5, for example, its last element will have an index of 4.

Also, this:

var anchorTag = window.document.getElementsByTagName('a');
var anchorTagNumber = window.document.getElementsByTagName('a').length;

Can be simplified to:

var anchorTag       = window.document.getElementsByTagName('a');
var anchorTagNumber = anchorTag.length;

That will speed the code slightly but also makes future code maintenance, or reuse, easier.

べ繥欢鉨o。 2024-10-08 10:46:09

您可以尝试使用 setInterval 每 100 毫秒调用一次函数,而不是使用 while。

interval = setInterval(function(){
    iDidIt = doSomethin()
    if(iDidIt){
       clear interval;
    }
},100)

Instead of using a while, you could try a setInterval to call you function every 100 miliseconds.

interval = setInterval(function(){
    iDidIt = doSomethin()
    if(iDidIt){
       clear interval;
    }
},100)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文