gpt4 book ai didi

javascript - Chrome 扩展 addEventListener null

转载 作者:行者123 更新时间:2023-12-03 01:53:09 24 4
gpt4 key购买 nike

我几周前刚刚开始学习 JS,并正在尝试构建一个 Chrome 扩展。我构建了 manifest.jsonpopup.htmlpopup.jscontent.js 文件.

当我尝试在 Chrome 上运行后台页面控制台以查看 popup.js 是否正常工作时,我不断收到错误:

Uncaught TypeError: Cannot read property 'addEventListener' of null
at popup.js:8

我尝试将 html 中的脚本移至正文底部,但仍然以错误结束,并尝试实现:

document.addEventListener('DOMContentLoaded', function () {
// My code here.. ( Your code here )
});

在我的 popup.jswindows.onload 方法中,但仍然无法消除错误。

如果有人可以指出我的代码中的任何错误并告诉我出了什么问题,我将非常感激。谢谢

弹出 HTML

<!DOCTYPE html>
<html>
<head>
<title>Like IG Links</title>
</head>

<body>

<h3><center>Paste Links Below</center></h3>
<hr>
<textarea rows="10" cols="60" id="urls"></textarea>
<br>
<textarea rows="1" cols="5" id="counter" disabled></textarea>
<br>
<br>

<button type="submit" align="center" style="width:60px;"
id="start">Start</button>

<br>
<br>

<button type="submit" align="right" style="width:60px;"
id="stop">Stop</button>

<script type="text/javascript" src="popup.js"></script>

</body>

</html>

内容 JS

console.log('CHROME');

POPUP JS

console.log('background running');

function loadUrls() {
console.log("123");
}

var startbutton = document.getElementById("start");
startbutton.addEventListener('click', loadUrls);

list JSON

{ 
"manifest_version": 2,
"name": "Like IG Links",
"version": "1.0",
"description": "Like IG Links",

"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
}
],

"background": {
"scripts": ["popup.js"]
},

"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},

"permissions": ["tabs", "storage", "activeTab"]
}

最佳答案

删除 list 的后台脚本部分。更新扩展后,错误应该会消失。在我正在开发的一个扩展中,我在 HTML 和 list 中添加了 jquery.js 和 popup.js。 list 的后台脚本标记用于跟踪浏览器事件并持续响应它们的逻辑,而不是用于扩展 HTML 后面的 js。

关于javascript - Chrome 扩展 addEventListener null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50339222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com