[英]Delete a single URL from a chrome-extension popup-window seems not working with adding event listeners (the event listener might be scoped?)
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 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.png和https://i.stack.imgur.com/ 4oGdI.png
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 要查看的函数: addToDom和removeMe以及完全加载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.