简体   繁体   English

从chrome-extension弹出窗口中删除单个URL似乎不适用于添加事件侦听器(事件侦听器可能是作用域?)

[英]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 deletes one link or delete all of the links in just one click. 我正在尝试创建一个功能强大的chrome扩展弹出窗口,该窗口可让用户在需要时添加链接(基于打开的标签的URL),并只需单击一下即可删除一个链接或删除所有链接。 Down below are all of my files! 下面是我所有的文件! I must say in advance that I am not very good (and not experienced) with using jQuery's library but if that's the only solution that I have, than I will use it in my code. 我必须事先说,我对使用jQuery的库不是很好(也没有经验),但是如果这是我唯一的解决方案,那么我将在我的代码中使用它。

The problem 问题

The buttons to delete all the links and the button to add one link does work perfectly without bugs. 删除所有链接的按钮和添加一个链接的按钮确实可以正常工作而不会出现错误。 However, the button to which one link should be deleted doesn't work , I tried various ways including splicing. 但是, 应该删除一个链接的按钮不起作用 ,我尝试了多种方法,包括剪接。 I am trying to remove the link from the DOM and from the chrome.storage.local, both actions do not work. 我正在尝试从DOM和chrome.storage.local中删除链接,这两个操作均无效。 In the following code you can see all of the files I have thus far. 在以下代码中,您可以看到到目前为止我拥有的所有文件。 The code of when the 'X' button is pressed doesn't get executed (see these pictures): https://i.stack.imgur.com/gg1Dy.png and https://i.stack.imgur.com/4oGdI.png 按下“ X”按钮时的代码不会执行(请参见以下图片): https : //i.stack.imgur.com/gg1Dy.pnghttps://i.stack.imgur.com/ 4oGdI.png

The code 编码

manifest.json: manifest.json的:

gist.github.com/kobrajunior/78acda830c2d1c384333542422f1494d gist.github.com/kobrajunior/78acda830c2d1c384333542422422f1494d

popup.js: popup.js:

functions to look at: addToDom and removeMe and the very first event listener when the DOM is fully loaded 要查看的函数: addToDomremoveMe以及完全加载DOM时的第一个事件侦听器

gist.github.com/kobrajunior/4852f85ae18cfb9edbe542a820b8c107 gist.github.com/kobrajunior/4852f85ae18cfb9edbe542a820b8c107

For extra information (if needed), the popup.html: 有关其他信息(如果需要),请访问popup.html:

gist.github.com/kobrajunior/1c26691734c19391c62dc336ed2e1791 gist.github.com/kobrajunior/1c26691734c19391c62dc336ed2e1791

Thank you in advance. 先感谢您。

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. 对于popup.js的以下行,您想要还原(显示所有项目/按钮)并绑定侦听器,但是请不要忘记在addToDom的回调中调用chrome.storage.local.get ,这意味着您将值分配给allButtons ,则不allButtons它们添加到DOM中,这会导致allButtons.length === 0并且您实际上没有绑定任何东西。

Try to move the binding logic inside the callback of restore (You may encounter other issues however that's not covered in this quesions). 尝试将绑定逻辑移动到restore的回调中(您可能会遇到其他问题,但本问题未解决)。

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

function restore() {
    // 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 DOM
        for (var i = 0, n = urlList.length; i < n; i++) {
            addToDom(urlList[i], titleList[i]);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 弹出窗口焦点不适用于鼠标悬停事件,但适用于Chrome扩展程序中的单击事件 - PopUp window focus not working on mouseover event but working on click event from a chrome extension 在 Chrome 扩展中添加多个事件监听器 - Adding Multiple Event Listeners in Chrome Extension 将 JS 事件侦听器添加到 Chrome 扩展弹出窗口 - Add JS Event Listener to Chrome Extension Popup Chrome 扩展程序:在站点加载后添加事件侦听器不起作用 - Chrome Extension: Adding event listener not working after site loads chrome扩展标签事件监听器不起作用 - chrome extension tab event listeners are not working 关闭弹出窗口的父窗口时,事件侦听器无法在弹出窗口上工作 - Event listeners are not working on popup window when close it's parent window 从内容脚本到Google Chrome扩展程序中的弹出窗口,点击事件均不起作用 - Click event is not working from content script to popup on Google Chrome Extension 向可能不在页面上的元素添加事件侦听器? - Adding event listeners to elements which might not be on the page? 如何在Chrome扩展程序的弹出窗口中添加复选框的事件监听器? - How to add event listener for checkbox in Chrome extension's popup? 我可以删除Chrome扩展程序的事件监听器吗? - Can I remove event listeners with a Chrome extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM