清单,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控制台
我的Chrome版本:58.0.3029.110(64位) 为什么chrome.runtime.onMessage在content_script中未定义? 请告诉我如何解决这个问题〜:)
是否有其他方法可以将后台sendMessage实现到内容脚本?
答案 0 :(得分:1)
chrome.runtime.onMessage
确实不是函数,它是Event
对象提供addListener
函数方法:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
...
});