chrome.runtime.onMessage在内容脚本中未定义

时间:2017-06-04 08:56:28

标签: javascript google-chrome google-chrome-extension google-chrome-devtools

清单,JSON

{
"background": {
    "scripts": ["jquery-3.2.1.min.js", "background.js"]
},
"content_scripts": [{
    "all_frames": true,
    "run_at": "document_start",
    "matches": [
        "http://*/*",
        "https://*/*",
        "file:///*"
    ],
    "css": ["mystyles.css"],
    "js": ["jquery-3.2.1.min.js", "myscript.js"]
}],
"permissions": [
    "<all_urls>",
    "contextMenus",
    "storage",
    "clipboardWrite",
    "clipboardRead",
    "activeTab",
    "identity",
    "webRequest",
    "webRequestBlocking"
],
"web_accessible_resources": [
    "*.png"
]}

内容脚本

chrome.runtime.onMessage(function(message, sender, sendResponse){
console.log(message);});

Chrome控制台

in chrome console

我的Chrome版本:58.0.3029.110(64位) 为什么chrome.runtime.onMessage在content_script中未定义? 请告诉我如何解决这个问题〜:)

是否有其他方法可以将后台sendMessage实现到内容脚本?

1 个答案:

答案 0 :(得分:1)

chrome.runtime.onMessage确实不是函数,它是Event对象提供addListener函数方法:

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  ...
});