javascriptgoogle-chromebookmarklet

Javascript code giving syntax error when put in bookmarklet


(Background: I'm trying to load all comments in a GitHub PR using the JS code found here https://github.com/refined-github/refined-github/issues/1892 but using a bookmarklet)

I have the following JS code that when pasted in the console (Chrome) works fine.


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

})();

I then try to make this a bookmarklet in Chrome, that converts it into

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

and this gives a syntax error. Uncaught SyntaxError: Unexpected identifier 'buttons'

What am I doing wrong here?


Solution

  • Your code depends on automatic semi-colon insertion.

    i.e. there are places in your code where you use new lines instead of semi-colons.

    Whatever method you are using to convert it to a bookmarklet is removing those new lines but failing to replace them with semi-colons.

    You need to either add the semi-colons manually or fix the method so it inserts them automatically.