![](/img/trans.png)
[英]Chrome extension:Communicate between content script and webpage's script
[英]How to communicate between frame and Edge Extension content script?
我正在開發Edge擴展,我需要將消息從框架文檔 (不是頂部文檔,頂部文檔工作正常)發送到內容腳本。
如下:
`contentscript.js
window.addEventListener("message", function(event) {
console.log("window top message...event:");
console.log(event);
});`
使用以下內容從框架文檔發送消息:
window.top.postMessage("Hi, I am from frame", "*").
在控制台面板中,我可以看到window top message...event:
然后瀏覽器重新加載頁面。 似乎該消息已被阻止。
擴展名可以從github獲得 。
重現步驟:
加載擴展程序,打開Google ,打開控制台面板,切換到Frame,然后鍵入
window.top.postMessage("Hi, I am from frame", "*").
有人可以幫忙嗎?
如果您有背景頁面,並且您的contentscript.js
可以訪問browser.
對象,“便宜”的簡單方法可能是將郵件退回給擴展名並返回,如下所示:
contentscript.js
browser.runtime.sendMessage({name: 'bounce', payload: {name: hello}});
background.js
browser.runtime.onMessage.addListener(function (request, sender) {
if (request.name === 'bounce') {
browser.tabs.sendMessage(sender.tab.id, request.payload);
}
});
然后在您的內容{name: hello}
上收聽{name: hello}
。 所有框架(包括頂部)都應收到此消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.