我知道这个问题已经以不同方式反复被问过了,但我尝试查看了所有答案(希望没有错过任何人),但是它们都不能解决我的问题。
这是我的扩展程序的代码:
清单:
{
"name": "test",
"version": "1.1",
"background":
{
"scripts": ["contextMenus.js"]
},
"permissions": ["tabs", "<all_urls>", "contextMenus"],
"content_scripts" : [
{
"matches" : [ "http://*/*" ],
"js": ["jquery-1.8.3.js", "jquery-ui.js"],
"css": [ "jquery-ui.css" ],
"js": ["openDialog.js"]
}
],
"manifest_version": 2
}
contextMenus.js
function onClickHandler(info, tab) {
if (info.menuItemId == "line1"){
alert("You have selected: " + info.selectionText);
chrome.extension.sendMessage({action:'open_dialog_box'}, function(){});
alert("Req sent?");
}
}
chrome.contextMenus.onClicked.addListener(onClickHandler);
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({"id": "line1", "type": "normal", "title": "I'm line 1", "contexts":["selection"]});
});
openDialog.js
chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.action == 'open_dialog_box') {
alert("Message recieved!");
}
});
背景页的两个警报正常工作,而 content_script 的一个警报没有工作。
控制台日志信息:端口错误:无法建立连接。接收端不存在。
我的错误在哪里?
chrome.tabs.sendMessage()
来向内容脚本发送消息,而不是chrome.extension.sendMessage()
。 - apsillers