| OLD | NEW |
| 1 <h1 id="add-webview"> | 1 <h1 id="add-webview"> |
| 2 <span class="h1-step">Step 4:</span> | 2 <span class="h1-step">Step 4:</span> |
| 3 Open External Links With a Webview | 3 Open External Links With a Webview |
| 4 </h1> | 4 </h1> |
| 5 | 5 |
| 6 <p class="note"> | 6 <p class="note"> |
| 7 <strong>Want to start fresh from here?</strong> | 7 <strong>Want to start fresh from here?</strong> |
| 8 Find the previous step's code in the <a href="https://github.com/mangini/io13-
codelab/archive/master.zip">reference code zip</a> under <strong><em>cheat_code
> solution_for_step3</strong></em>. | 8 Find the previous step's code in the <a href="https://github.com/mangini/io13-
codelab/archive/master.zip">reference code zip</a> under <strong><em>cheat_code
> solution_for_step3</strong></em>. |
| 9 </p> | 9 </p> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 <p>Some applications need to present external web content directly to the user b
ut | 25 <p>Some applications need to present external web content directly to the user b
ut |
| 26 keep them inside the application experience. For example, a news aggregator | 26 keep them inside the application experience. For example, a news aggregator |
| 27 might want to embed the news from external sites with all the formatting, images
, | 27 might want to embed the news from external sites with all the formatting, images
, |
| 28 and behavior of the original site. For these and other usages, Chrome Apps have | 28 and behavior of the original site. For these and other usages, Chrome Apps have |
| 29 a custom HTML tag called <a href="/apps/tags/webview">webview</a>.</p> | 29 a custom HTML tag called <a href="/apps/tags/webview">webview</a>.</p> |
| 30 | 30 |
| 31 <figure> | 31 <figure> |
| 32 <img src="{{static}}/images/app_codelab/webview-example.png" alt="The Todo app
using a webview"> | 32 <img src="{{static}}/images/app_codelab/webview-example.png" alt="The Todo app
using a webview"> |
| 33 </figure> | 33 </figure> |
| 34 | 34 |
| 35 <p class="note">A webview is a sandboxed process. For example, the enclosing Chr
ome App | 35 <p class="note"><strong>Webviews are sandboxed processes:</strong> |
| 36 (also known as the "embedder page") cannot easily access the webview's loaded DO
M. | 36 The enclosing Chrome App (also known as the "embedder page") |
| 37 cannot easily access the webview's loaded DOM. |
| 37 You can only interact with the webview using its API.</p> | 38 You can only interact with the webview using its API.</p> |
| 38 | 39 |
| 39 <h2 id="implement-webview">Implement the webview tag</h2> | 40 <h2 id="implement-webview">Implement the webview tag</h2> |
| 40 | 41 |
| 41 <p>Let's update our Todo app to search for URLs in the todo item text | 42 <p>Update the Todo app to search for URLs in the todo item text and create a hyp
erlink. |
| 42 in order to create a hyperlink. The link, when clicked, will open a new Chrome A
pp window | 43 The link, when clicked, opens a new Chrome App window |
| 43 (not a browser tab) with a webview presenting the content.</p> | 44 (not a browser tab) with a webview presenting the content.</p> |
| 44 | 45 |
| 45 <h3 id="update-permissions">Update permissions</h3> | 46 <h3 id="update-permissions">Update permissions</h3> |
| 46 | 47 |
| 47 <p>In <strong><em>manifest.json</em></strong>, request the <code>webview</code>
permission:</p> | 48 <p>In <strong><em>manifest.json</em></strong>, request the <code>webview</code>
permission:</p> |
| 48 | 49 |
| 49 <pre data-filename="manifest.json"> | 50 <pre data-filename="manifest.json"> |
| 50 "permissions": ["storage", "alarms", "notifications"<b>, "webview"</b>], | 51 "permissions": ["storage", "alarms", "notifications"<b>, "webview"</b>], |
| 51 </pre> | 52 </pre> |
| 52 | 53 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 79 <b>Controller.prototype._parseForURLs = function (text) {</b> | 80 <b>Controller.prototype._parseForURLs = function (text) {</b> |
| 80 <b> var re = /(https?:\/\/[^\s"<>,]+)/g;</b> | 81 <b> var re = /(https?:\/\/[^\s"<>,]+)/g;</b> |
| 81 <b> return text.replace(re, '<a href="$1" data-src="$1">$1</a>');
</b> | 82 <b> return text.replace(re, '<a href="$1" data-src="$1">$1</a>');
</b> |
| 82 <b>};</b> | 83 <b>};</b> |
| 83 | 84 |
| 84 // Export to window | 85 // Export to window |
| 85 window.app.Controller = Controller; | 86 window.app.Controller = Controller; |
| 86 })(window); | 87 })(window); |
| 87 </pre> | 88 </pre> |
| 88 | 89 |
| 89 <p>Whenever a string starting with "http://" or "https://" is found, a HTML anch
or tag will be created to wrap around the URL.</p> | 90 <p>Whenever a string starting with "http://" or "https://" is found, a HTML anch
or tag is created to wrap around the URL.</p> |
| 90 | 91 |
| 91 <h3 id="render-links">Render hyperlinks in the todo list</h3> | 92 <h3 id="render-links">Render hyperlinks in the todo list</h3> |
| 92 | 93 |
| 93 <p>Find <code>showAll()</code> in <em>controller.js</em>. Update <code>showAll()
</code> | 94 <p>Find <code>showAll()</code> in <em>controller.js</em>. Update <code>showAll()
</code> |
| 94 to parse for links by using the <code>_parseForURLs()</code> method that we adde
d previously:</p> | 95 to parse for links by using the <code>_parseForURLs()</code> method added previo
usly:</p> |
| 95 | 96 |
| 96 <pre data-filename="controller.js"> | 97 <pre data-filename="controller.js"> |
| 97 /** | 98 /** |
| 98 * An event to fire on load. Will get all items and display them in the | 99 * An event to fire on load. Will get all items and display them in the |
| 99 * todo-list | 100 * todo-list |
| 100 */ | 101 */ |
| 101 Controller.prototype.showAll = function () { | 102 Controller.prototype.showAll = function () { |
| 102 this.model.read(function (data) { | 103 this.model.read(function (data) { |
| 103 <strike>this.$todoList.innerHTML = this.view.show(data);</strike> | 104 <strike>this.$todoList.innerHTML = this.view.show(data);</strike> |
| 104 <b>this.$todoList.innerHTML = this._parseForURLs(this.view.show(data));</b> | 105 <b>this.$todoList.innerHTML = this._parseForURLs(this.view.show(data));</b> |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // ORM. If this were a real DB this would save a lot of time and would avoid | 158 // ORM. If this were a real DB this would save a lot of time and would avoid |
| 158 // a spinner gif. | 159 // a spinner gif. |
| 159 <strike>input.value = label.innerHTML;</strike> | 160 <strike>input.value = label.innerHTML;</strike> |
| 160 <b>input.value = label.innerText;</b> | 161 <b>input.value = label.innerText;</b> |
| 161 ... | 162 ... |
| 162 } | 163 } |
| 163 </pre> | 164 </pre> |
| 164 | 165 |
| 165 <h3 id="open-webview">Open new window containing webview</h3> | 166 <h3 id="open-webview">Open new window containing webview</h3> |
| 166 | 167 |
| 167 <p>Add a <code>_doShowUrl()</code> method to <em>controller.js</em>. This method
will open a new Chrome App window | 168 <p>Add a <code>_doShowUrl()</code> method to <em>controller.js</em>. This method
opens a new Chrome App window |
| 168 via <a href="/apps/app_window.html#method-create">chrome.app.window.create()</a> | 169 via <a href="/apps/app_window.html#method-create">chrome.app.window.create()</a> |
| 169 with <em>webview.html</em> as the window source:</p> | 170 with <em>webview.html</em> as the window source:</p> |
| 170 | 171 |
| 171 <pre data-filename="controller.js"> | 172 <pre data-filename="controller.js"> |
| 172 Controller.prototype._parseForURLs = function (text) { | 173 Controller.prototype._parseForURLs = function (text) { |
| 173 var re = /(https?:\/\/[^\s"<>,]+)/g; | 174 var re = /(https?:\/\/[^\s"<>,]+)/g; |
| 174 return text.replace(re, '<a href="$1" data-src="$1">$1</a>'); | 175 return text.replace(re, '<a href="$1" data-src="$1">$1</a>'); |
| 175 }; | 176 }; |
| 176 | 177 |
| 177 <b>Controller.prototype._doShowUrl = function(e) {</b> | 178 <b>Controller.prototype._doShowUrl = function(e) {</b> |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 227 |
| 227 <h2 id="launch">Launch your finished Todo app</h2> | 228 <h2 id="launch">Launch your finished Todo app</h2> |
| 228 | 229 |
| 229 <p>You are done Step 4! If you reload your app and add a todo item with a full U
RL | 230 <p>You are done Step 4! If you reload your app and add a todo item with a full U
RL |
| 230 starting with http:// or https://, you should see something like this:</p> | 231 starting with http:// or https://, you should see something like this:</p> |
| 231 | 232 |
| 232 <figure> | 233 <figure> |
| 233 <img src="{{static}}/images/app_codelab/step4-completed.gif" alt="The Todo app
with webview"/> | 234 <img src="{{static}}/images/app_codelab/step4-completed.gif" alt="The Todo app
with webview"/> |
| 234 </figure> | 235 </figure> |
| 235 | 236 |
| 236 <h2 id="recap">Recap APIs referenced in this step</h2> | 237 <h2 id="recap">For more information</h2> |
| 237 | 238 |
| 238 <p>For more detailed information about some of the APIs introduced in this step,
refer to:</p> | 239 <p>For more detailed information about some of the APIs introduced in this step,
refer to:</p> |
| 239 | 240 |
| 240 <ul> | 241 <ul> |
| 241 <li> | 242 <li> |
| 242 <a href="/apps/declare_permissions" title="Read 'Declare Permissions' in the
Chrome developer docs">Declare Permissions</a> | 243 <a href="/apps/declare_permissions" title="Read 'Declare Permissions' in the
Chrome developer docs">Declare Permissions</a> |
| 243 <a href="#update-permissions" class="anchor-link-icon" title="This feature m
entioned in 'Update app permissions'">↑</a> | 244 <a href="#update-permissions" class="anchor-link-icon" title="This feature m
entioned in 'Update app permissions'">↑</a> |
| 244 </li> | 245 </li> |
| 245 <li> | 246 <li> |
| 246 <a href="/apps/tags/webview" title="Read '<webview> Tag' in the Chrome
developer docs"><webview> Tag</a> | 247 <a href="/apps/tags/webview" title="Read '<webview> Tag' in the Chrome
developer docs"><webview> Tag</a> |
| 247 <a href="#overview" class="anchor-link-icon" title="This feature mentioned i
n 'Learn about the webview tag'">↑</a> | 248 <a href="#overview" class="anchor-link-icon" title="This feature mentioned i
n 'Learn about the webview tag'">↑</a> |
| 248 <a href="#create-webview" class="anchor-link-icon" title="This feature menti
oned in 'Create a webview embedder page'">↑</a> | |
| 249 </li> | 249 </li> |
| 250 <li> | 250 <li> |
| 251 <a href="/apps/app_window.html#method-create" title="Read 'chrome.app.window
.create()' in the Chrome developer docs">chrome.app.window.create()</a> | 251 <a href="/apps/app_window.html#method-create" title="Read 'chrome.app.window
.create()' in the Chrome developer docs">chrome.app.window.create()</a> |
| 252 <a href="#open-webview" class="anchor-link-icon" title="This feature mention
ed in 'Open new window containing webview'">↑</a> | 252 <a href="#open-webview" class="anchor-link-icon" title="This feature mention
ed in 'Open new window containing webview'">↑</a> |
| 253 </li> | 253 </li> |
| 254 <li> | |
| 255 <a href="/apps/tags/webview#tag" title="Read '<webview> Tag attributes
' in the Chrome developer docs"><webview> Tag attributes</a> | |
| 256 <a href="#open-webview" class="anchor-link-icon" title="This feature mention
ed in 'Open new window containing webview'">↑</a> | |
| 257 </li> | |
| 258 | 254 |
| 259 </ul> | 255 </ul> |
| 260 | 256 |
| 261 <p>Ready to continue onto the next step? Go to <a href="app_codelab_images.html"
>Step 5 - Add images from the web »</a></p> | 257 <p>Ready to continue onto the next step? Go to <a href="app_codelab_images.html"
>Step 5 - Add images from the web »</a></p> |
| OLD | NEW |