(背景:我嘗試使用此處找到的 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:意外的識別碼「按鈕」
我在這裡做錯了什麼?
您的程式碼取決於自動分號插入.
即您的程式碼中有些地方使用換行而不是分號。
無論您使用什麼方法將其轉換為小書籤,都會刪除這些新行,但無法用分號取代它們。
您需要手動新增分號或修復方法以便自動插入分號。