我有一个非常相似的问题,只是我有一个无法将“postMessage”发送到我的 xul-background-task 的 html-popup(本地)。我想我让它工作了,奇怪的是通过启动我自己的 MessageEvent (与 postMessage 所做的一样)但有一个(我相信过时的)后备......简而言之:我从 MDN 和其他网站一起酿造了一些东西;)
我在内容中的脚本:
var Communicator =
{
postMessage: function(data)
{
// We need some element to attach the event to, since "window" wont work
// let's just use a fallback JSON-stringified textnode
var request = document.createTextNode(JSON.stringify(data));
// and as always attach it to the current contentwindow
document.head.appendChild(request);
// Now let's make a MessageEvent of our own, just like window.postMessage would do
var event = document.createEvent("MessageEvent");
event.initMessageEvent ("own_message", true, false, data, window.location, 0, window, null);
// you might want to change "own_message" back to "message" or whatever you like
//and there we go
request.dispatchEvent(event);
}
}
而不是 window.postMessage(data) 现在使用 Communicator.postMessage(data) 就是这样!现在在我的覆盖中除了我们的好老
addEventListener('own_message', someMessageFunc, false, true);
//or maybe even "message" just like originally
希望这也对您有用(没有在 iframe 上检查...)