You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将GitHub PR评论加载JS转为书签工具时出现语法错误求助

GitHub PR评论加载书签工具语法错误解决

问题描述

我尝试用一段JS代码通过书签工具加载GitHub PR的所有评论,这段代码在Chrome控制台运行完全正常,但转成书签工具后出现语法错误:Uncaught SyntaxError: Unexpected identifier 'buttons'。

控制台可正常运行的代码

(() => {
    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();

})();

出错的书签工具代码

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

解决方法

报错的核心原因是书签工具代码里使用了HTML实体转义的&quot;,浏览器会把它当作字符串的一部分,导致document.querySelectorAll的选择器语法错误,进而使得后续的buttons变量无法被正确识别。

修复步骤:

  • 将代码中所有的&quot;替换为原生双引号"
  • 确保代码没有其他多余的转义字符,压缩时保留正确的语法结构

修复后的书签工具代码

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(); })();

内容的提问来源于stack exchange,提问作者George Kastrinis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 04:52:36