小書籤導致 Javascript 程式碼中出現語法錯誤
P粉127901279
P粉127901279 2024-04-01 16:16:44
0
1
408

(背景:我嘗試使用此處找到的 JS 程式碼 https://github.com/refined-github/refined-github/issues/1892 但使用書籤來載入 GitHub PR 中的所有評論)

我有以下 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:意外的識別碼「按鈕」

我在這裡做錯了什麼?

P粉127901279
P粉127901279

全部回覆(1)
P粉744691205

您的程式碼取決於自動分號插入.

即您的程式碼中有些地方使用換行而不是分號

無論您使用什麼方法將其轉換為小書籤,都會刪除這些新行,但無法用分號取代它們。

您需要手動新增分號或修復方法以便自動插入分號。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!