在chrome扩展中,对函数的消息传递引用将变为null

时间:2018-10-29 23:46:34

标签: javascript google-chrome-extension

我有一个与下面类似的设置,我希望能够通过运行时消息传递来发送一个函数,但是它将变成空值。

为什么会发生这种情况,我该怎么办才能解决这个问题?

background.js

console.log("background");
chrome.runtime.onMessage.addListener(function(data){
    console.log(data);
});

content.js

console.log("content");
function test(){
    console.log("test")
}   
test2 = function(){
    console.log("test2")
}
chrome.extension.sendMessage({funcs:[test, test2], func:test, func2:test2}, function(background_response){

})

清单

{
    "name": "Functions won't send!",
    "version": "1.0",
    "description": "Functions won't send!",
    "permissions": [
        "tabs"
    ],
    "background": {
      "scripts": [
            "background.js"
        ],
      "persistent": true
    },

    "content_scripts": [
        {
            "matches": ["http://*/*", "https://*/*"],
            "js": [
                "content.js"
            ],
            "run_at": "document_start"
        }
    ],

    "manifest_version": 2

  }

enter image description here

0 个答案:

没有答案