Skip to content Skip to sidebar Skip to footer

Delete A Single Url From A Chrome-extension Popup-window Seems Not Working With Adding Event Listeners (the Event Listener Might Be Scoped?)

Background information I am trying to make a functional chrome-extension popup-window that enables the user to add links (based on the open tab's URL) when he desires it, and delet

Solution 1:

For the following lines in popup.js, you want to restore (show all the items/buttons) and bind listeners, however don't forget addToDom is called inside the callback of chrome.storage.local.get, that means by the time you assign value to allButtons, they're not added to DOM, that causes allButtons.length === 0 and you didn't bind anything in fact.

Try to move the binding logic inside the callback of restore (You may encounter other issues however that's not covered in this quesions).

document.addEventListener('DOMContentLoaded', function () {
    restore();
    var allButtons = document.getElementsByClassName('buttons');
    functionlistenI(i) {
        allButtons[i].addEventListener('click', () =>removeMe(i));
    }
    for (var i = 0; i < allButtons.length; i++) {
        listenI(i);
    }
});

functionrestore() {
    // get the tab link and title
    chrome.storage.local.get({ urlList: [], titleList: [] }, function (data) {
        urlList = data.urlList;
        titleList = data.titleList;

        // add the titles and url's to the DOMfor (var i = 0, n = urlList.length; i < n; i++) {
            addToDom(urlList[i], titleList[i]);
        }
    });
}

Post a Comment for "Delete A Single Url From A Chrome-extension Popup-window Seems Not Working With Adding Event Listeners (the Event Listener Might Be Scoped?)"