我正在查看嵌入了iframe的網頁。我需要從網頁和所有嵌入式iframe的擴展瀏覽器操作中收集信息。我有以下代碼:如何從iframe中的內容腳本傳遞消息?
清單
{
"manifest_version": 2,
...
"content_scripts": [
{
"all_frames": true,
"js": [ "contentscript.js" ],
"matches": [ "http://*/*", "https://*/*", "file:///*" ],
"run_at": "document_start"
}
],
...
}
瀏覽器操作彈出
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id,{command: 'collectPageDetails'},function (details) {
console.log('got details');
});
});
內容腳本
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.command === 'collectPageDetails') {
console.log('getting page details');
sendResponse({ details: 'something' });
return true;
}
});
我在這裏看到的行爲是該消息被接收主頁面和iframe。我可以通過在頁面控制檯中看到兩個getting page details
實例來驗證此情況。
但是,我只收到一個迴應瀏覽器操作的響應(got details
僅在瀏覽器操作中記錄了一次),這似乎只來自主頁而不是嵌入式iframe。
如何通過消息傳遞從頁面中嵌入的iframe進行通信?我期待在我的瀏覽器動作function (details) { }
中發生兩次回調。
消息是要走的路。謝謝。 – kspearrin