list ,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到内容脚本?
最佳答案
chrome.runtime.onMessage
实际上不是一个函数,它是一个提供 addListener
函数方法的 Event
对象:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
...
});
关于javascript - 内容脚本中未定义 chrome.runtime.onMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44352355/