尝试从弹出窗口向我的内容脚本发送消息时出现此错误。我要做的是从我的 content.js 获取当前选项卡的文档并将其发送到弹出窗口。我该如何解决这个错误?
{
"manifest_version": 2,
"name": "Chrome Snapshot",
"description": "Save images and screenshots of sites to Dropbox.",
"version": "1.0",
"permissions": [
"<all_urls>",
"tabs"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "html/popup.html"
},
"background": {
"scripts": [
"vendor/dropbox.min.js",
"vendor/jquery-2.0.2.min.js"
],
"persistant": false
},
"content_scripts" : [{
"all_frames": true,
"matches" : ["*://*/*"],
"js" : ["js/content.js"],
"run_at": "document_end"
}]
}
js/popup.js
chrome.runtime.sendMessage({message: 'hi'}, function(response) {
console.log(response);
});
js/content.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
console.log('message', message);
sendResponse({farewell: 'goodbye'});
});
编辑 #1仍然出现同样的错误Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:235
chromeHidden.Port.dispatchOnDisconnect
修复了清单
更新 js/popup.js中的拼写错误“persistent”
chrome.tabs.query({'active': true,'currentWindow':true}, function(tab){
console.log('from tab', tab[0]);
chrome.tabs.sendMessage(tab[0].id, {message: 'hi'}, function(response){
console.log(JSON.stringify(response));
});
});