内容脚本和背景页面之间的消息传递在chrome扩展是不工作,因为它应该是
messaging between content script and background page in a chrome extension is not working as it is supposed to be
我把代码贴在下面:
manifest.json
{
"manifest_version": 2,
"name": "Demo",
"description": "all_frames test",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [{
"matches": ["*://*/*"],
"js": ["content.js"],
"all_frames": true
}],
"permissions": [
"tabs",
"*://*/*"
]
}
background.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
var tabStatus = changeInfo.status;
if (tabStatus == 'complete') {
function return_msg_callback() {
console.log('Got a msg from cs...')
}
chrome.tabs.sendMessage(tabId, {
text: 'hey_cs'
}, return_msg_callback);
}
});
content.js
/* Listen for messages */
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
/* If the received message has the expected format... */
if (msg.text && (msg.text == 'hey_cs')) {
console.log('Received a msg from bp...')
sendResponse('hey_bp');
}
});
然后,如果我去一个包含多个跨域iframe的站点,例如http://www.sport.es/,你会看到页面内的所有iframe都收到来自后台页面的消息,但只有其中一个能够响应。这是正常的行为吗?
谢谢你的回答。
你只发送一个消息与直接回调,所以自然Chrome可以使用这个响应回调只是一次(这是一个一次性连接到一个实体,无论是页面或iframe)。
-
解决方案1:显式地向每个iframe发送多个消息:
清单。Json,附加权限:
"permissions": [ "webNavigation" ],
background.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { ............. // before Chrome 49 it was chrome.webNavigation.getAllFrames(tabId, ..... // starting with Chrome 49 tabId is passed inside an object chrome.webNavigation.getAllFrames({tabId: tabId}, function(details) { details.forEach(function(frame) { chrome.tabs.sendMessage( tabId, {text: 'hey_cs'}, {frameId: frame.frameId}, function(response) { console.log(response) } ); }); }); });
-
解决方案2:重新编写您的后台脚本逻辑,使内容脚本在通信中起主导作用,并让它在加载后发送消息。
content.js
chrome.runtime.sendMessage({text: "hey"}, function(response) { console.log("Response: ", response); });
background.js
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { console.log("Received %o from %o, frame", msg, sender.tab, sender.frameId); sendResponse("Gotcha!"); });
Chrome扩展中内容脚本和背景页面之间的通信
内容脚本到背景页
发送信息到后台页面
chrome.extension.sendRequest({message: contentScriptMessage});
从内容脚本接收信息
chrome.extension.onRequest.addListener(function(request, sender) {
console.log(request.message);
});
背景页到内容脚本
发送信息到内容脚本
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, { message: "TEST" });
});
从后台页面接收信息
chrome.runtime.onMessage.addListener(function(request, sender) {
console.log(request.message);
});
您可以使用executeScript
来代替消息传递。虽然回调的参数很少使用(我认为很多人都不知道它是如何工作的),但它在这里是完美的:
chrome.tabs.executeScript(tabId, {file: "script.js"}, function(results) {
// Whichever is returned by the last executed statement of script.js
// is considered a result.
// "results" is an Array of all results - collected from all frames
})
您可以确保,例如,最后执行的语句类似于
// script.js
/* ... */
result = { someFrameIdentifier: ..., data: ...};
// Note: you shouldn't do a "return" statement - it'll be an error,
// since it's not a function call. It just needs to evaluate to what you want.
确保script.js
能够在同一上下文中执行多次。
相关文章:
- 扩展移相器按钮类不工作
- window.location.htm无法在chrome扩展中工作
- 扩展在LeafletJS中是如何工作的,为什么我需要一个中介归因才能使其正确工作
- Chrome扩展程序:浏览器操作单击按钮工作一次
- 用于检查文件扩展名不工作的Javascript If语句
- 按钮不'不能在自定义镀铬扩展中工作
- Chrome扩展的BatteryStatusneneneba API不工作
- 谷歌Chrome扩展在用Javascript打开的弹出窗口中工作吗
- Chrome扩展和Chome浏览器中的Jquery工作方式不同
- 按钮点击不工作在谷歌扩展
- Javascript JIRA扩展无法在悬停时工作
- 从chrome扩展动态创建Google工作表
- 以下 Chrome 扩展程序 JavaScript 代码片段究竟是如何工作的
- Chrome扩展程序在后台工作
- 新的google.maps.LatLngBounds()绑定扩展和中心工作随机
- 让谷歌浏览器扩展程序只在一个标签上工作
- 无法在扩展程序之间获取 chrome.runtime.onMessage外部工作
- 工作流程/选项以扩展其他选项
- onPageLoad 在 Firefox 扩展开发中无法正常工作
- Chrome 扩展程序历史记录 API 无法正常工作