Reputation: 47
Is it possible for a Chrome Packaged App to listen to events fired by a webview inside the ChromeApp?
My ChromeApp loads a webview on startup. I would like to be able to fire events from the webview so I can access local chrome.app functions.
The function I would like to use now is "close window" (so I can close the ChromeApp from the webview that is inside the ChromeApp). In the future I would like to be able to access the Storage API from the webview.
Upvotes: 3
Views: 1490
Reputation: 1887
<webview>
supports a restricted set of standard DOM events out of the box. For your "close window" interception, the aptly named close event
is probably the best fit.
If you want to establish a kind of communication channel between a webview guest and your app in the general case, as in your second use case with accessing Storage API, you have a couple of options:
You can use <webview>.contentWindow.postMessage
to post messages into the guest page and return replies in the reverse direction.
You can also inject pieces of JavaScript into your webview guest using <webview>.executeScript
. This way you can, for example, attach a click event handler to a button within the guest that will do something that you need to do in the execution context of your app (of course, for that your event handler has to be a closure). Or, you can gather some information from within the guest and return in to the app in the executeScript
's callback.
Finally, there is <webview>.insertCSS
- not a fit for your needs, but I'm mentioning it to complete the set of methods you can use to interact with or alter the webview guest.
Upvotes: 3