从后台脚本获取消息时遇到问题。我的思绪几乎被吹了,错在哪里?
清单
{
"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"
}
}
background.js
chrome.tabs.getSelected(null, function(tab) {
console.log('Loaded!');
chrome.tabs.sendMessage(tab.id, {
greeting: "hello"
});
console.log('Sended on '+tab.id+'...');
});
content.js
chrome.extension.onMessage.addListener(function(msg, _, request) {
console.log('Getting request...');
console.log(msg.greeting);
});
在后台页面中,我按预期进入控制台2消息。但是在内容页面上我没有任何消息,所以我认为这与content.js中的代码有关。我做错了什么?
答案 0 :(得分:1)
chrome.tabs.getSelected()已弃用Chrome 16
,请改用chrome.tabs.query()。
Problem in Your Script
chrome.tabs.getSelected()返回当前所选标签的引用,在加载扩展程序之后为chrome://chrome/extensions/
,因此您的后台页面正在向内容脚本{{的扩展页面发送消息1}}
修改后台脚本以适应chrome.tabs.query(),您可以在网页控制台中看到消息!