0

I am developing an Edge extension, I need to send a message from frame document(not the top document, top document works fine) to content script.
As following:

`contentscript.js
window.addEventListener("message", function(event) {
    console.log("window top message...event:");
    console.log(event);
});`

Send a message from the frame document, with:

window.top.postMessage("Hi, I am from frame", "*").

In the console panel, I can see window top message...event: and then the browser reload the page. It seems the message was blocked.
The extension can be got from github.
steps to reproduce:
Load the extension, open Google, open console panel, switch to Frame, and type

window.top.postMessage("Hi, I am from frame", "*").

Could anybody help?

BurningFish
  • 157
  • 1
  • 9

1 Answers1

1

If you have a background page, and your contentscript.js has access to browser. object, the cheap'n'easy way might be to bounce message to extension and back like so:

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);
  }
});

and just listen for {name: hello} on your contentscript. All frames, including top should get this message.

Anatoly Sazanov
  • 1,814
  • 2
  • 14
  • 24
  • Thanks for your input. But, I don't think browser.extension.sendMessage is supported in Edge, you can check from https://learn.microsoft.com/en-us/microsoft-edge/extensions/api-support/supported-apis#windows. – BurningFish May 15 '17 at 02:30
  • @BurningFish sorry, it should be `browser.runtime.sendMessage`. See here: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/sendMessage – Anatoly Sazanov May 15 '17 at 08:07
  • Hi, Anatoly, browser.runtime.sendMessage can help on passing message between background page and content script, but it does't not help on passing message between web page ifames and content script. I hope to send a message with window.top.postMessage in the ifame document, it fails. I have submitted a bug to Edge team https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/12016669/. Thanks for your input. – BurningFish May 16 '17 at 00:38
  • @BurningFish is this iframe part of your extension or a site? If latter, you can try to inject identical content scripts in all frames and use window.postMessage to talk to them. From these content scripts, even if it's in iframe, you will be able to send msg to background and then bounce back to your top content script. – Anatoly Sazanov May 16 '17 at 08:25
  • iframe is part of a site. I try to use window.postMessage in the iframe, the site will reload when executing window.postMessage, I think it's bug of Edge. – BurningFish May 18 '17 at 01:57