如何与带有chrome扩展名的popup.js和内容脚本进行通信?

时间:2018-12-14 22:03:33

标签: javascript html google-chrome-extension

这就是我现在正在做的事情:

Manifest.json

{
    "name": "my_extension",
    "version": "1.0",
    "description": "my_description",
    "manifest_version": 2,
    "browser_action": {
        "default_title": "my extension",
        "default_icon": "logo.png",
        "default_popup": "popup.html"
    },
    "content_scripts" : [{
        "matches": [
            "*://*/*",
            "file://*/*"
          ],
       "js" : ["contentscript.js"]
    }],
    "permissions": [
        "tabs"
      ]


}

Contentscript.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
      sendResponse({hello:'asdfsafsd'})
    });

Popup.js(已附加到我的弹出式HTML上)

    $("#calculate").click(() => {

        chrome.tabs.getSelected(null, function(tab){
            console.log(tab.index);
            chrome.tabs.sendMessage(tab.index,{greeting: "hello"},(response)=>{
                console.log(response.hello)
            })
        });



    })

当我单击#click_me按钮时,我得到以下信息:

Error in event handler for (unknown): TypeError: Cannot read property 'wat' of undefined
at chrome-extension://pmclodhoknpghfkblogphfmecjnopfjm/popup.js:6:50

0 个答案:

没有答案