Javascript chrome.runtime.onMessage.addListener未接收事件
这是我的manifest.json:Javascript chrome.runtime.onMessage.addListener未接收事件,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,这是我的manifest.json: { "manifest_version": 2, "name": "Testing", "version": "0.1", "permissions": [ "tabs" ], "background": { "scripts": [ "background.js" ] }, "content_scripts": [
{
"manifest_version": 2,
"name": "Testing",
"version": "0.1",
"permissions": [
"tabs"
],
"background": {
"scripts": [
"background.js"
]
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"jquery-min-3.4.1.js",
"content.js"
]
}
],
"browser_action": {
"default_icon": "icon.png"
}
}
下面是我在background.js中得到的信息:
chrome.browserAction.onClicked.addListener(function (tab) {
// Send a message to the active tab
console.log('on clicked')
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var activeTab = tabs[0];
console.log(activeTab);
chrome.tabs.sendMessage(activeTab.id, { "message": "clicked_browser_action" });
});
});
当我单击我的浏览器操作时,我确实看到“单击”日志和“活动”选项卡日志,但我没有看到“收到消息”日志。不确定这里发生了什么尝试在清单中添加选项卡权限。内容脚本的实例在每个匹配页面中运行,因此您应该查看选项卡的devtools,而不是后台脚本devtools。@V.Sambor我已经有了that@wOxxOm我检查了tab的开发工具,nothing gets loggedIt表示该选项卡中没有运行内容脚本,或者您在devtools控制台工具栏上输入了隐藏消息的筛选器。确保已在上重新加载两个扩展chrome://extensions 页面和选项卡。成功后,您将在选项卡的控制台工具栏上下文切换器以及“源”面板的“内容脚本”子选项卡中看到扩展名。注意:在公司网络中,一些URL可能被管理员阻止,请检查chrome://policy
chrome.browserAction.onClicked.addListener(function (tab) {
// Send a message to the active tab
console.log('on clicked')
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var activeTab = tabs[0];
console.log(activeTab);
chrome.tabs.sendMessage(activeTab.id, { "message": "clicked_browser_action" });
});
});