Search code examples
javascriptgoogle-chrome-extension

On popup.js of Chrome extension, Lexical declaration cannot appear in a single-statement context error occurs


"Uncaught SyntaxError: Lexical declaration cannot appear in a single-statement context" means "You can't use lexical declarations (const and let) after statements like if, else, for etc. without a block ({})", this page says. However, declaring out of block({}), SyntaxError occurs as following:

        let day_of_the_week;
        if(interval%7==0){//Chrome says this line is error
            day_of_the_week = new Date().getDay();
        }

full code is following:

'use strict';

function click(e) {
    chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
        const currentTab = tabs[0]; // there will be only one in this array
        const url = currentTab.url;
        const interval = e.target.id;
        const last_browsed_time = new Date().getTime();
        if(interval%7==0){
            day_of_the_week = new Date().getDay();
        }
    
        chrome.storage.sync.get("pages", (result) => {
            result.pages[url] = {
                interval:interval,
                last_browsed_time:last_browsed_time,
                day_of_the_week:day_of_the_week
            }
            chrome.storage.sync.set(
                {"pages":result.pages}, () => {}               
            )
        })
    
        chrome.browserAction.setBadgeText({"text":interval}, () => {})
    
        window.close();
    }
    );
};

document.addEventListener('DOMContentLoaded', function () {
    let divs = document.querySelectorAll('div');
    for (let i = 0; i < divs.length; i++) {
      divs[i].addEventListener('click', click);
    }
});

By the way, deleting error line, "result.pages[url] = {" is error, Chrome says.


Solution

  • wOxxOm says:

    You're probably seeing an old error on chrome://extensions page. You need to clear that list yourself.

    I deleted the extension and reinstalled, error disappeared.