DOM Exception 12 for window.postMessage

后端 未结 1 923
我在风中等你
我在风中等你 2020-12-10 22:57

I\'m learning to build Chrome Extensions, and I\'m trying to follow one of the instructions in the official Guide here.

What I am trying to accomplish is:

相关标签:
1条回答
  • 2020-12-10 23:35

    Don't confuse the port.postMessage in the contentscript.js example with window.postMessage.

    port.postMessage is a Chrome extension-specific API, intended to pass messages around within the extension, while window.postMessage is a JavaScript method used for communicating with frames. The second argument to window.postMessage is required, and is used to validate whether the target is allowed to receive the message or not.

    In your case, using a wildcard is probably sufficient, because you're sending a message from a page to itself:

    window.postMessage({ type: "FROM_PAGE", text: "Hello from the webpage!" }, "*");
                                                                               ^^^
    0 讨论(0)
提交回复
热议问题