【问题标题】:Chrome Extension Script not Running in popup (Cannot call method 'addListener' of undefined )Chrome 扩展脚本未在弹出窗口中运行(无法调用未定义的方法“addListener”)
【发布时间】:2013-12-13 17:48:03
【问题描述】:

标题说明了一切。我不明白为什么会收到此错误。

这是我现有的代码:

manifest.json

{
    "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/*"
    ]
}

popup.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>

eventpage.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 的内容复制/粘贴到控制台时,它可以完美运行。

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension popup dom-events


    【解决方案1】:

    您应该使用:而不是.onClicked.addListener

    document.getElementById("option1").addEventListener( 'click', function() { ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 2022-08-23
      相关资源
      最近更新 更多