试图在我的 background.js 和 contentscript.js 之间进行通信
清单.json
{
"name": "xxx",
"version": "0.1",
"manifest_version": 2,
"permissions": [
"tabs"
],
"description": "xxx",
"icons": { "16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png" },
"browser_action": {
"default_title" : "xxx",
"default_icon": "icon16.png",
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
},
"content_scripts" : [
{
"matches" : [ "http://*/*" ],
"js" : [ "contentscript.js", "jquery.js" ]
}
]
}
背景.js
var listePdt = {};
var selectedPdt = null;
var selectedId = null;
function updatePdt(tabId)
{
chrome.tabs.sendMessage(tabId, {}, function(pdt) {
chrome.pageAction.show(tabId);
});
}
chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
if(change.status == "complete")
updatePdt(tabId);
});
chrome.tabs.onSelectionChanged.addListener(function(tabId, info) {
selectedId = tabId;
// and other things
});
chrome.tabs.getSelected(null, function(tab) {
updatePdt(tab.id);
});
内容脚本.js
if(window == top)
{
chrome.extension.onMessage.addListener(
function(req, sender, sendResponse) {
sendResponse(findPdt());
}
);
}
var findPdt = function() {
// operations on a string
return "string";
}
但是我在控制台中为生成的背景页面收到以下错误“端口错误:无法建立连接。接收端不存在”......不明白为什么。
有什么帮助吗?