Chrome 扩展将消息从 iFrame 发送到事件页面,然后发送到内容脚本
Chrome extension sending message from iFrame to event page then to content script
我从内容脚本中插入了一个 iframe。它工作正常。但是如果我想在 iframe 上显示 parent 的 html 内容,我必须使用消息传递在 iframe 和内容脚本之间进行通信,但它不起作用。然后我尝试将消息从 iframe 发送到 "event page",然后再发送到 "content script"。一旦内容脚本收到消息,它将查询 html 内容并回复。它也不起作用。我怎样才能让它发挥作用?
内容脚本:
var iframe = document.createElement('iframe');
iframe.id = "popup";
iframe.src = chrome.runtime.getURL('frame.html');
document.body.appendChild(iframe);
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.from === 'event' && msg.method == 'ping') {
sendResponse({ data: 'pong' });
}
});
活动页面:
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.from === 'popup' && msg.method === 'ping') {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
from: 'event',
method:'ping'}, function(response) {
sendResponse(response.data);
});
});
}
});
frame.js
// This callback function is never called, so no response is returned.
// But I can see message's sent successfully to event page from logs.
chrome.runtime.sendMessage({from: 'popup', method:'ping'},
function(response) {
$timeout(function(){
$scope.welcomeMsg = response;
}, 0);
});
我找到了一个相关的问题。
来自 chrome.runtime.onMessage.addListener 的文档:
这个函数在事件监听器 returns 时失效,除非你从事件监听器 return true 指示你希望异步发送响应(这将保持消息通道打开到另一端,直到调用 sendResponse。
所以我必须 return true 来指示 sendResponse 是异步的。
活动页面:
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.from === 'popup' && msg.method === 'ping') {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
from: 'event',
method:'ping'}, function(response) {
sendResponse(response.data);
});
});
return true; // <-- Indicate that sendResponse will be async
}
});
我从内容脚本中插入了一个 iframe。它工作正常。但是如果我想在 iframe 上显示 parent 的 html 内容,我必须使用消息传递在 iframe 和内容脚本之间进行通信,但它不起作用。然后我尝试将消息从 iframe 发送到 "event page",然后再发送到 "content script"。一旦内容脚本收到消息,它将查询 html 内容并回复。它也不起作用。我怎样才能让它发挥作用?
内容脚本:
var iframe = document.createElement('iframe');
iframe.id = "popup";
iframe.src = chrome.runtime.getURL('frame.html');
document.body.appendChild(iframe);
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.from === 'event' && msg.method == 'ping') {
sendResponse({ data: 'pong' });
}
});
活动页面:
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.from === 'popup' && msg.method === 'ping') {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
from: 'event',
method:'ping'}, function(response) {
sendResponse(response.data);
});
});
}
});
frame.js
// This callback function is never called, so no response is returned.
// But I can see message's sent successfully to event page from logs.
chrome.runtime.sendMessage({from: 'popup', method:'ping'},
function(response) {
$timeout(function(){
$scope.welcomeMsg = response;
}, 0);
});
我找到了一个相关的问题。
来自 chrome.runtime.onMessage.addListener 的文档:
这个函数在事件监听器 returns 时失效,除非你从事件监听器 return true 指示你希望异步发送响应(这将保持消息通道打开到另一端,直到调用 sendResponse。
所以我必须 return true 来指示 sendResponse 是异步的。
活动页面:
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.from === 'popup' && msg.method === 'ping') {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {
from: 'event',
method:'ping'}, function(response) {
sendResponse(response.data);
});
});
return true; // <-- Indicate that sendResponse will be async
}
});