简体   繁体   English

再次单击时,javascript自动循环不重置

[英]javascript autoclick loop not resetting when clicked again

I made a little script for Tampermonkey, to help me browse tumblr notes without any unnecessary clicking or looking at "user liked this" spam. 我为Tampermonkey制作了一个小脚本,帮助我浏览tumblr笔记,没有任何不必要的点击或查看“用户喜欢这个”垃圾邮件。

$('.more_notes_link').click(function() {
    var LoopCount = 10;
    var i = 0;
    clearInterval(loadNotesInterval);
    var loadNotesInterval = setInterval(function() {
        if ($('.more_notes_link').length ) {
            $('.more_notes_link').click();
            i++;
            console.log(i);
            if (i >= LoopCount) {
                clearInterval(loadNotesInterval);
            }
        }
        else
        {
            clearInterval(loadNotesInterval);
        }
    }, 1000)
});

GM_addStyle ( ".notes .note.without_commentary{display:none} .notes .note.reply{display:inline}" );

For some reason though, it just works perfectly when run once, logging numbers from 1 to 10, and then stopping... And then it doesn't work anymore. 但是出于某种原因,它只是在运行一次时完美运行,记录数字从1到10,然后停止......然后它不再起作用了。 No console output, no looping, and it only clicks once. 没有控制台输出,没有循环,它只点击一次。

I've tried and given up several times on this, so I thought I'd finally just ask here if someone could fix my problem. 我已经尝试过多次放弃了,所以我想我最后会问这里是否有人可以解决我的问题。 I can't figure it out myself. 我自己无法弄明白。

EDIT: I tried putting some console.logs all over the place, and it seems that the script doesn't detect my click at all after the first one. 编辑:我尝试在整个地方放置一些console.logs,似乎脚本在第一个之后根本没有检测到我的点击。 So for some reason, even though .more_notes_link still exists, it just doesn't notice me clicking it at all. 所以出于某种原因,即使.more_notes_link仍然存在,它根本没有注意到我点击它。

because loadNotesInterval is a local variable so each click you get a new one. 因为loadNotesInterval是一个局部变量,所以每次点击都会得到一个新变量。

So if you define it outside of the click and drop the var it will work the way you want. 因此,如果您在单击之外定义并删除var,它将按您希望的方式工作。

(function () {
    var loadNotesInterval;
    $('.more_notes_link').click(function() {
        var LoopCount = 10;
        var i = 0;
        clearInterval(loadNotesInterval);
        loadNotesInterval = setInterval(function() {
            if ($('.more_notes_link').length ) {
                $('.more_notes_link').click();
                i++;
                console.log(i);
                if (i >= LoopCount) {
                    clearInterval(loadNotesInterval);
                }
            }
            else
            {
                clearInterval(loadNotesInterval);
            }
        }, 1000)
    });    
}());

My guess is that the link itself is being regenerated, which means it could be missing when you check for its length. 我的猜测是链接本身正在重新生成,这意味着当你检查它的长度时它可能会丢失。

Maybe try using on instead of click method. 也许尝试使用on而不是click方法。 That way you can specify the entire document and use your selector to filter the descendants. 这样,您可以指定整个文档并使用您的选择器来过滤后代。

$('document').on('click', '.more_notes_link', function() {
  // your code
}

I solved the problem at last! 我终于解决了这个问题!

$('body').on('click', '.more_notes_link',function() {
    if (i == 0) {
        clearInterval(loadNotesInterval);
        var loadNotesInterval = setInterval(function() {
            if ($('.more_notes_link').length) {
                i++;
                $('.more_notes_link').click();
                if (i >= LoopCount) {
                    i = 0;
                    clearInterval(loadNotesInterval);
                }
            }
            else
            {
                i = 0;
                clearInterval(loadNotesInterval);
            }
        }, 1000)
    }
});

Firstly, it lost the item with the correct class after it was clicked once, so I had to search it as a child of the body. 首先,它在点击一次后丢失了正确的类,因此我不得不将其作为身体的一个孩子进行搜索。

Then the problem was that it clicked itself and started hundreds of loops at the same time. 然后问题是它点击了自己并同时启动了数百个循环。 I solved this by only allowing it to loop on click when the loop isn't running already. 我解决了这个问题,只是在循环没有运行时才允许它循环点击。

I hope this helps someone else fix the same problem, because this one was pretty tough to solve. 我希望这有助于其他人解决同样的问题,因为这个问题很难解决。

If anyone wants to use it, it's posted in HERE! 如果有人想使用它,它会发布在这里!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM