我想从网页发送消息到Chrome扩展程序,但它不起作用。我看到后台功能没有接收来自content.js的消息发送 这里:
manifes.json:
{
"name": "My Checker",
"version": "1.0",
"description": "http://www.abc.dev Extension!",
"manifest_version": 2,
"permissions": [
"*://*.google.com/",
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["*://*.abc.dev/*"],
"js": ["jquery-3.3.1.min.js", "content.js"],
"run_at": "document_end"
}
],
"externally_connectable": {
"matches": ["*://*.abc.dev/*"]
},
"browser_action": {
"default_popup": "popup.html"
}
}
and background.js:
console.log("background page in loading...");
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
console.log("listener for request");
console.log(request);
if (request.openUrlInEditor)
console.log("Open :" + request.openUrlInEditor);
sendResponse({success : "success"});
}
);
并且content.js:
chrome.runtime.sendMessage("acldjllcapdfejdafbkjnfmpahdkendo", {openUrlInEditor: "https://www.google.com"}, function(response) {
if (response && !response.success)
handleError(url);
}
);