在這個步驟中,您將瞭解以下內容:
- 如何透過安全沙箱機制,在應用程式中顯示外部網頁內容。
完成這個步驟的預估時間:10 分鐘。
如要預覽您將在這個步驟中完成的內容,請向下捲動到本頁底部 ↓。
瞭解 WebView 代碼
某些應用程式需要直接向使用者顯示外部網頁內容,但保留在 應用程式體驗。舉例來說,新聞集結網站可能會想嵌入外部的新聞 原網站的所有格式、圖片和行為適用於這類情況 已使用 webview 自訂 HTML 標記。
導入 WebView 代碼
更新「待辦事項」應用程式,搜尋待辦事項文字中的網址並建立超連結。連結 會開啟新的 Chrome 應用程式視窗 (而非瀏覽器分頁),當中會顯示內容。
更新權限
在 manifest.json 中要求 webview
權限:
"permissions": [
"storage",
"alarms",
"notifications",
"webview"
],
建立 WebView 嵌入器頁面
在專案資料夾的根目錄中建立新檔案,並命名為 webview.html。這是
內含一個 <webview>
標記的基本網頁:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<webview style="width: 100%; height: 100%;"></webview>
</body>
</html>
剖析待辦事項項目中的網址
在 controller.js 結尾,新增名為 _parseForURLs()
的方法:
Controller.prototype._getCurrentPage = function () {
return document.location.hash.split('/')[1];
};
Controller.prototype._parseForURLs = function (text) {
var re = /(https?:\/\/[^\s"<>,]+)/g;
return text.replace(re, '<a href="$1" data-src="$1">$1</a>');
};
// Export to window
window.app.Controller = Controller;
})(window);
當字串開頭為「http://」時或「https://」找到之後,系統會建立 HTML 錨定標記, 。
在待辦事項清單中顯示超連結
在 controller.js 中找到 showAll()
。更新 showAll()
使用
先前新增的 _parseForURLs()
方法:
/**
* An event to fire on load. Will get all items and display them in the
* todo-list
*/
Controller.prototype.showAll = function () {
this.model.read(function (data) {
this.$todoList.innerHTML = this.view.show(data);
this.$todoList.innerHTML = this._parseForURLs(this.view.show(data));
}.bind(this));
};
為 showActive()
和 showCompleted()
執行相同操作:
/**
* Renders all active tasks
*/
Controller.prototype.showActive = function () {
this.model.read({ completed: 0 }, function (data) {
this.$todoList.innerHTML = this.view.show(data);
this.$todoList.innerHTML = this._parseForURLs(this.view.show(data));
}.bind(this));
};
/**
* Renders all completed tasks
*/
Controller.prototype.showCompleted = function () {
this.model.read({ completed: 1 }, function (data) {
this.$todoList.innerHTML = this.view.show(data);
this.$todoList.innerHTML = this._parseForURLs(this.view.show(data));
}.bind(this));
};
最後,將 _parseForURLs()
新增至 editItem()
:
Controller.prototype.editItem = function (id, label) {
...
var onSaveHandler = function () {
...
// Instead of re-rendering the whole view just update
// this piece of it
label.innerHTML = value;
label.innerHTML = this._parseForURLs(value);
...
}.bind(this);
...
}
同樣在 editItem()
中,修正程式碼,使其使用標籤的 innerText
,而非
標籤的 innerHTML
:
Controller.prototype.editItem = function (id, label) {
...
// Get the innerHTML of the label instead of requesting the data from the
// Get the innerText of the label instead of requesting the data from the
// ORM. If this were a real DB this would save a lot of time and would avoid
// a spinner gif.
input.value = label.innerHTML;
input.value = label.innerText;
...
}
開啟含有 WebView 的新視窗
將 _doShowUrl()
方法新增至 controller.js。這個方法會透過以下方式開啟新的 Chrome 應用程式視窗:
chrome.app.window.create(),並將 webview.html 做為視窗來源:
Controller.prototype._parseForURLs = function (text) {
var re = /(https?:\/\/[^\s"<>,]+)/g;
return text.replace(re, '<a href="$1" data-src="$1">$1</a>');
};
Controller.prototype._doShowUrl = function(e) {
// only applies to elements with data-src attributes
if (!e.target.hasAttribute('data-src')) {
return;
}
e.preventDefault();
var url = e.target.getAttribute('data-src');
chrome.app.window.create(
'webview.html',
{hidden: true}, // only show window when webview is configured
function(appWin) {
appWin.contentWindow.addEventListener('DOMContentLoaded',
function(e) {
// when window is loaded, set webview source
var webview = appWin.contentWindow.
document.querySelector('webview');
webview.src = url;
// now we can show it:
appWin.show();
}
);
});
};
// Export to window
window.app.Controller = Controller;
})(window);
在 chrome.app.window.create()
回呼中,請留意 WebView 的網址透過 src
標記設定方式
屬性。
最後,在 Controller
建構函式中加入點擊事件監聽器,以在出現以下情況時呼叫 doShowUrl()
:
使用者按下連結時:
function Controller(model, view) {
...
this.router = new Router();
this.router.init();
this.$todoList.addEventListener('click', this._doShowUrl);
window.addEventListener('load', function () {
this._updateFilterState();
}.bind(this));
...
}
啟動已完成的 Todo 應用程式
您已完成步驟 4!如果您重新載入應用程式並新增待辦事項,並以網址做為開頭 http:// 或 https://,您應該會看到如下內容:
瞭解詳情
如要進一步瞭解這個步驟中導入的部分 API,請參閱:
準備好進行下一個步驟了嗎?前往步驟 5 - 新增網路上的圖片 »