简体   繁体   中英

Chrome Extension Script not Running in popup (Cannot call method 'addListener' of undefined )

The title says it all. I don't get why I'm getting this error.

Here's my existing code:

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();
    });
});

Currently, when I load the popup in my extension, it looks fine, but generates the error:

Uncaught TypeError: Cannot call method 'addListener' of undefined eventpage.js:2

I read somewhere that you needed to add it to a window.onload , but if I change my eventpage.js to read...

window.onload = function() {
    document.addEventListener('DOMContentLoaded', function() {
        document.getElementById("option1").onClicked.addListener(function(tab) {
            chrome.tabs.executeScript(null, {file: "QCScript.js"});
        });
    });
};

The error does go away, but clicking the button does nothing. It should interface with the current tab, automatically clicking through images for Quality Control purposes. When I copy/paste the contents of QCScript.js into the console, it works perfectly.

而不是.onClicked.addListener ,你应该使用:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM