(背景:我尝试使用此处找到的 JS 代码 https://github.com/refined-github/refined-github/issues/1892 但使用书签来加载 GitHub PR 中的所有评论) p>
我有以下 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:意外的标识符“按钮”
我在这里做错了什么?
您的代码取决于自动分号插入.
即您的代码中有些地方使用换行而不是分号。
无论您使用什么方法将其转换为小书签,都会删除这些新行,但无法用分号替换它们。
您需要手动添加分号或修复方法以便自动插入分号。