Google chrome extension 仅发送一条消息时,内容页上会收到多条消息?
当我发送信息时,警报触发了四次! 不知道怎么了! 我也在添加舱单 background.js:Google chrome extension 仅发送一条消息时,内容页上会收到多条消息?,google-chrome-extension,google-chrome-devtools,Google Chrome Extension,Google Chrome Devtools,当我发送信息时,警报触发了四次! 不知道怎么了! 我也在添加舱单 background.js: chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if(request.method == "123") { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.method == "123")
{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {method: "xyz"});
})
}
});
content.js:
chrome.runtime.onMessage.addListener(
function(request) {
alert(request.method);
if (request.method == "xyz"){
alert("hello);
}
});
这是清单文件的名称
舱单:
{
"manifest_version" : 2,
"version" : "0.1.0.0",
"name" : "xyz",
"short_name": "xyz",
"permissions" : [" <all_urls>","tabs","storage","notifications","unlimitedStorage","downloads"],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [{
"matches": ["<all_urls>","http://*/*","https://*/*"],
"js": [
"content.js",
],
"all_frames" : true
}],
"browser_action" : {
"default_title" : "test",
"default_popup" : "popup.html"
},
"devtools_page" : "xyz.html"
}
{
“清单版本”:2,
“版本”:“0.1.0.0”,
“名称”:“xyz”,
“短名称”:“xyz”,
“权限”:[“”、“选项卡”、“存储”、“通知”、“未受限制的存储”、“下载”],
“背景”:{
“脚本”:[“background.js”]
},
“内容脚本”:[{
“匹配项”:[“”、“http://*/*”、“https://*/*”],
“js”:[
“content.js”,
],
“所有帧”:正确
}],
“浏览器操作”:{
“默认标题”:“测试”,
“默认弹出窗口”:“popup.html”
},
“devtools_页面”:“xyz.html”
}
您在清单.json
中声明了“所有帧”:true
,这意味着您的内容脚本将被注入到您网页中的每个iframe中,然后当从后台页面发送消息时,内容脚本中的每个侦听器都将对此做出响应
为了解决这个问题,
“所有帧”
部分或将其设置为false
window!==窗口顶部
if (window !== window.top) {
chrome.runtime.onMessage.addListener(callback);
}
chrome.tabs.sendMessage(tabs[0].id, {method: "xyz"}, {frameId: 0});
manifest.json
中声明了“all_frames”:true
,这意味着您的内容脚本将被注入到您网页中的每个iframe中,然后当从后台页面发送消息时,内容脚本中的每个侦听器都将对此做出响应
为了解决这个问题,
“所有帧”
部分或将其设置为false
window!==窗口顶部
if (window !== window.top) {
chrome.runtime.onMessage.addListener(callback);
}
chrome.tabs.sendMessage(tabs[0].id, {method: "xyz"}, {frameId: 0});
您最初在哪里发送消息?我正在将消息从devtool面板发送到后台,然后从后台发送到内容!您能提供更多类似您的
manifest.json
的代码吗?我猜您也为iframes注入了脚本。添加了清单!您最初在哪里发送消息?我正在将消息从devtool面板发送到后台,然后从后台发送到内容!您能提供更多类似您的manifest.json
的代码吗?我猜您也为iframes注入了脚本。添加了清单!嘿,对不起,伙计,你说的所有画面是什么意思?如果我删除它,那么内容脚本仍将在当前网页上运行?@Ram,当然,所有_帧控制内容脚本是在匹配页面的所有帧中运行,还是仅在顶部帧中运行。ok!所以,如果它被删除,即使内容脚本将有权访问整个网页?嘿,对不起,伙计,但所有的框架你是什么意思?如果我删除它,那么内容脚本仍将在当前网页上运行?@Ram,当然,所有_帧控制内容脚本是在匹配页面的所有帧中运行,还是仅在顶部帧中运行。ok!所以,如果它被删除,即使内容脚本将访问整个网页?