JavaScript代码在放入bookmarklet时出现语法错误

ohfgkhjo  于 2023-05-16  发布在  Java
关注(0)|答案(1)|浏览(130)

(背景:我尝试使用这里找到的JS代码https://github.com/refined-github/refined-github/issues/1892加载GitHub PR中的所有评论,但使用bookmarklet)
我有下面的JS代码,当粘贴在控制台(Chrome)工作正常。

(() => {
    let tryAttempts = 0;

    function loadComments () {
        let needRescheduling = false;
        const buttons = document.querySelectorAll(".ajax-pagination-btn[data-disable-with]")
        
        buttons.forEach((button) => {
            button.click();
            needRescheduling = true;
            tryAttempts = 0;
        })
        
        if (needRescheduling || tryAttempts < 5) {
            if (needRescheduling) {
                console.log("Loading comments.")
            } else {
                console.log("Looking for more to load.");
            }
            tryAttempts++;
            setTimeout(loadComments, 500)
        } else {
            console.log("All comments loaded.");
    
            const resolvedButtons = document.querySelectorAll(".js-toggle-outdated-comments[data-view-component]");
    
            resolvedButtons.forEach((button) => {
                button.click();
            })
            
            console.log("All resolved comments loaded.")
        }
    }
    loadComments();

})();

然后我尝试在Chrome中将其作为书签,将其转换为

javascript: (() => {    let tryAttempts = 0;    function loadComments () {        let needRescheduling = false;        const buttons = document.querySelectorAll(".ajax-pagination-btn[data-disable-with]")                buttons.forEach((button) => {            button.click();            needRescheduling = true;            tryAttempts = 0;        })                if (needRescheduling || tryAttempts < 5) {            if (needRescheduling) {                console.log("Loading comments.")            } else {                console.log("Looking for more to load.");            }            tryAttempts++;            setTimeout(loadComments, 500)        } else {            console.log("All comments loaded.");                const resolvedButtons = document.querySelectorAll(".js-toggle-outdated-comments[data-view-component]");                resolvedButtons.forEach((button) => {                button.click();            })                        console.log("All resolved comments loaded.")        }    }    loadComments();})();

并且这给出了语法错误。Uncaught SyntaxError: Unexpected identifier 'buttons'
我做错了什么?

hgqdbh6s

hgqdbh6s1#

您的代码依赖于automatic semi-colon insertion
也就是说,在你的代码中,有些地方你使用了 new lines 而不是 semi-colons
无论你使用什么方法将其转换为书签,都是在删除这些新行,但没有用分号替换它们。
您需要手动添加分号或修复方法,以便自动插入分号。

相关问题