![](/img/trans.png)
[英]“Cannot call method 'get' of undefined” in Chrome Extension popup.js
[英]Chrome Extension Script not Running in popup (Cannot call method 'addListener' of undefined )
标题说明了一切。 我不明白为什么我会收到这个错误。
这是我现有的代码:
清单文件
{
"manifest_version": 2,
"name": "Helper",
"version": "1.0.0",
"description": "A helper program",
"icons": {
"16": "images/icon16.png",
"48": "images/icon48.png",
"128": "images/icon128.png"
},
"browser_action": {
"default_icon": {
"19": "images/icon19.png",
"38": "images/icon38.png"
},
"default_title": "Helper",
"default_popup": "popup.html"
},
"author": "ME",
"permissions": [
"tabs", "http://libraries.com/*"
]
}
弹出窗口.html
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Helper Popup</title>
<link rel="stylesheet" href="popup.css" />
<script type="text/javascript" src="eventpage.js"></script>
</head>
<body>
<p><span id="option1" class="options">Automated Scanning</span></p>
</body>
</html>
事件页面.js
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("option1").onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "QCScript.js"});
window.close();
});
});
目前,当我在扩展程序中加载弹出窗口时,它看起来不错,但会产生错误:
未捕获的类型错误:无法调用未定义的 eventpage.js:2 的方法“addListener”
我在某处读到您需要将其添加到window.onload
,但是如果我将 eventpage.js 更改为读取...
window.onload = function() {
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("option1").onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {file: "QCScript.js"});
});
});
};
错误确实消失了,但单击按钮什么也不做。 它应该与当前选项卡交互,自动单击图像以进行质量控制。 当我将 QCScript.js 的内容复制/粘贴到控制台时,它运行良好。
而不是.onClicked.addListener
,你应该使用:
document.getElementById("option1").addEventListener( 'click', function() { ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.