Accessing the main world with <webview>.executeScript()
Web view content scripts run in an isolated world. This means that they can…
Accessing the main world with <webview>.executeScript()
Web view content scripts run in an isolated world. This means that they can
access the current page's DOM, but cannot share objects with scripts included
in the DOM because scripts in the DOM run in the main world. For a more
detailed explanation, checkout [this
video](https://www.youtube.com/watch?v=laLudeUmXHM).
Running in an isolated world is usually what the developer wants because
access to the DOM is enough. Sometimes, the developer may want to access
Javascript objects in the main world. This sample shows good and bad examples
of injecting Javascript into a <webview> that is intended to run in the
main world. To do this correctly, content script must create a <script> tag
and inject it into the DOM. Then, the script contained within the <script>
tag will be run in the main world.
The sample shows a page that contains an animated dragon. The injected script
adds the ability to construct dragons that animate in sync. In order to do
so, the script needs access to the Dragon
FOAM model. To gain access, the
injected script inserts a <script> tag into the DOM.
The sample contains two app windows: Incorrect injection attempts to inject
the script directly and Correct injection injects the script into a
<script> tag in the DOM.
See the source code at https://github.com/GoogleChrome/chrome-app-samples/tree/master/webview-samples/shared-script
This version was built from git commit ebc7784 https://github.com/GoogleChrome/chrome-app-samples/tree/ebc7784/webview-samples/shared-script