从后台脚本获取消息时,我遇到了问题。我的心都快炸了,哪里错了?
显现
{
"name":"Some name",
"version":"0.1",
"manifest_version": 2,
"description":"extension for Google Chrome",
"permissions": ["tabs","http://*/*", "https://*/*", "<all_urls>","notifications","contextMenus","alarms","tabs","bookmarks"],
"content_scripts":[
{
"matches":["<all_urls>"],
"css":["style.css"],
"js":["content.js","jquery.js"]
}
],
"background": {
"scripts": ["background.js","jquery.js"],
"persistent": false
},
"icons":{
"16": "images/icon16.png",
"48": "images/icon48.png",
"128": "images/icon128.png"
},
"browser_action": {
"default_icon": {
"19": "images/icon19.png",
"38": "images/icon38.png"
},
"default_title": "Order Checker",
"default_popup": "popup.html"
}
}
背景.js
chrome.tabs.getSelected(null, function(tab) {
console.log('Loaded!');
chrome.tabs.sendMessage(tab.id, {
greeting: "hello"
});
console.log('Sended on '+tab.id+'...');
});
内容.js
chrome.extension.onMessage.addListener(function(msg, _, request) {
console.log('Getting request...');
console.log(msg.greeting);
});
正如预期的那样,在后台页面中,我收到了控制台 2 消息。但是在内容页面上我没有任何消息,所以我认为 content.js 中的代码有问题。我做错了什么?