Use the webview tag to embed 'guest' content
(such as web pages) in your Chrome App.
The guest content is contained within the webview container;
an embedder page within your Chrome App controls
how the guest content is laid out and rendered.
Different from the iframe,
the webview
runs in a separate process than your app;
it doesn't have the same permissions as your app and
all interactions between your app and embedded content will be asynchronous.
This keeps your app safe from the embedded content.
To embed a web page in your app,
add the webview tag to your app's embedder page
(this is the app page that will display the guest content).
In its simplest form,
the webview tag includes the src of the web page
and css styles that control the appearance of the webview container:
<webview id="foo" src="http://www.google.com/" style="width:640px; height:480px"></webview>
If you want to control the guest content in any way,
you can write JavaScript that listens for webview events
and responds to those events using the webview methods.
Here's sample code in app.js
with two event listeners:
one that listens for the web page to start loading,
the other for the web page to stop loading,
and displays a "loading..." message during the load time:
onload = function() {
var webview = document.getElementById("foo");
var indicator = document.querySelector(".indicator");
var loadstart = function() {
indicator.innerText = "loading...";
}
var loadstop = function() {
indicator.innerText = "";
}
webview.addEventListener("loadstart", loadstart);
webview.addEventListener("loadstop", loadstop);
}
<webview id="foo" src="http://www.google.com/" style="width:640px; height:480px"></webview>
Returns the visible URL. Mirrors the logic in the browser's omnibox: either returning a pending new navigation if initiated by the embedder page, or the last committed navigation. Writing to this attribute initiates top-level navigation.
Assigning src its own value will reload the current page.
The src attribute can also accept data URLs, such as "data:text/plain,Hello, world!".
<webview id="foo" src="http://www.google.com/" style="width:640px; height:480px" partition="persist:googlepluswidgets"></webview>
Storage partition ID used by the webview tag.
If the storage partition ID starts with persist:
(partition="persist:googlepluswidgets"),
the webview will use a persistent storage partition
available to all guests in the app
with the same storage partition ID.
If the ID is unset or if there is no 'persist': prefix,
the webview will use an in-memory storage partition.
This value can only be modified before the first navigation,
since the storage partition of an active renderer process cannot change.
Subsequent attempts to modify the value will fail with a DOM exception.
By assigning the same partition ID,
multiple webviews can share the same storage partition.
Exception thrown:
The partition attribute must be valid for navigation to proceed.
In the case of an invalid partition, such as partition="persist:",
the src attribute cannot be set and an exception is thrown.
<webview id="foo" src="http://www.google.com/" style="width:640px; height:480px" autosize="on" minwidth="576" minheight="432"></webview>
If "on", the webview will container will automatically resize within the bounds specified
by the attributes minwidth, minheight,
maxwidth, and maxheight.
These contraints do not impact the webview UNLESS autosize is enabled.
When autosize is enabled, the webview container size cannot be less than the minimum values
or greater than the maximum.
<webview src="https://www.google.com/" name="google-view"></webview>
This sets the guest content's window.name object.
By default, webviews are prevented from loading any resources packaged with
the app. However, webview partitions may be granted
access to these resources via a webview.partitions section in
the app manifest. Partitions may be granted access to a set of files by
matching partition name patterns with file name patterns. Both sorts of
patterns may contain the * wildcard.
Consider the following manifest.json snippet:
{
"name": "My extension",
...
"permissions": [
"webview"
],
"webview": {
"partitions": [
// In this example, any <webview partition="static"> or
// <webview partition="persist:static"> will have access to
// header.html, footer.html, and static.png.
{
"name": "static",
"accessible_resources": ["header.html", "footer.html", "static.png"]
},
// Also, any webview with a partition name beginning with "trusted"
// (e.g., "trusted-foo", "persist:trusted-bar") will have access to
// ".html" files beginning with "local_", as well as ".png" and ".js"
// files.
{
"name": "trusted*",
"accessible_resources": ["local_*.html", "*.png", "*.js"]
}
// In addition, any <webview partition="trusted-audio"> or
// <webview partition="persist:trusted-audio"> will have access to
// ".mp3" files. Note that this is in addition to "local_*.html",
// "*.png", "*.js" because "trusted-audio" also matches the "trusted*"
// pattern.
{
"name": "trusted-audio",
"accessible_resources": ["*.mp3"]
}
// Webviews in any other partition are denied access to packaged
// resources.
]
}
}