Chrome API: sendMessage 没有从内容脚本正确接收到 background.js
Chrome API: sendMessage not receiving properly from content script to background.js
我正在尝试设置我的 content_script.js 以将消息发送到 background.js。 Background.js 能够接收消息并对其进行处理,但是 content_script.js 遇到了问题 receiving/printing。
content_script.js
chrome.runtime.sendMessage({method: "getSettings"}, function(response) {
console.log(response.data);
console.log("This is not printing either");
});
background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "getSettings") {
chrome.storage.sync.get(null, function (val) {
sendResponse({data: "test"});
console.log("Responding: " + val.radioSettings);
});
}
});
background.js 中的 Console.log 打印来自 chrome.storage 的正确消息,但我的 content_script.js 中的 console.log 永远不会打印。
我觉得这很接近,但只是缺少一些小东西。这是我的清单的一部分:
manifest.json
{
"version": "0.0.1",
"manifest_version": 2,
"browser_action": {
"default_icon": "icons/icon19.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["js/background.js"]
},
"permissions": [
"storage",
"tabs"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["js/jquery-1.11.3.min.js", "js/content_script.js"]
}
]
}
那就解决了 -- 感谢重复问题 link。
为了让其他人更容易,这是其他答案所说的:
From the documentation for chrome.runtime.onMessage.addListener:
This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).
So you just need to add return true; after the [end of addListener] to indicate that you'll call the response function asynchronously.
这是代码的正确版本:
background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "getSettings") {
chrome.storage.sync.get(null, function (val) {
sendResponse({data: "test"});
console.log("Responding: " + val.radioSettings);
});
}
return true;
});
我正在尝试设置我的 content_script.js 以将消息发送到 background.js。 Background.js 能够接收消息并对其进行处理,但是 content_script.js 遇到了问题 receiving/printing。
content_script.js
chrome.runtime.sendMessage({method: "getSettings"}, function(response) {
console.log(response.data);
console.log("This is not printing either");
});
background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "getSettings") {
chrome.storage.sync.get(null, function (val) {
sendResponse({data: "test"});
console.log("Responding: " + val.radioSettings);
});
}
});
background.js 中的 Console.log 打印来自 chrome.storage 的正确消息,但我的 content_script.js 中的 console.log 永远不会打印。
我觉得这很接近,但只是缺少一些小东西。这是我的清单的一部分:
manifest.json
{
"version": "0.0.1",
"manifest_version": 2,
"browser_action": {
"default_icon": "icons/icon19.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["js/background.js"]
},
"permissions": [
"storage",
"tabs"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["js/jquery-1.11.3.min.js", "js/content_script.js"]
}
]
}
那就解决了 -- 感谢重复问题 link。
为了让其他人更容易,这是其他答案所说的:
From the documentation for chrome.runtime.onMessage.addListener:
This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).
So you just need to add return true; after the [end of addListener] to indicate that you'll call the response function asynchronously.
这是代码的正确版本:
background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "getSettings") {
chrome.storage.sync.get(null, function (val) {
sendResponse({data: "test"});
console.log("Responding: " + val.radioSettings);
});
}
return true;
});