How to communicate with WebView in Chrome App?

后端 未结 3 700
栀梦
栀梦 2021-01-06 17:07

I have developed a website which I intend to display inside a webview, within a Chrome App. This works fine.

Now, I want to use postMessage from the website, to send

3条回答
  •  梦毁少年i
    2021-01-06 17:30

    • In the guest page inside the contained webview, use chrome.runtime.sendMessage() to send messages to the containing app.

    • In the app, use chrome.runtime.onMessage.addListener() to listen to those messages.

    Note that you can message any app this way, not only the one containing your webview, but you'll need to know the app's ID for that, and use onMessageExternal instead of onMessage. For the containing app, the ID is optional.

    Here's a working example of this mechanism. It's a Polymer element, but that doesn't change the mechanism: designerProxy_ is the equivalent of your guest page; registerDesignerProxyListener_ is the equivalent of your app.

提交回复
热议问题