Chui-Wen Chiu(Arick)
2007.05.31 建立
簡介
Google Gears 採用 BSD 授權的開放原始碼專案, 主要是讓開發人員建立離線瀏覽的網路應用程式。目前支援的作業系統和瀏覽器如下:
- Apple Mac OS X (10.2 or higher)
- Firefox 1.5 or higher
- Linux
- Firefox 1.5 or higher
- Microsoft Windows (XP or higher)
- Firefox 1.5 or higher
- Internet Explorer 6 or higher
提供的主要功能有:
- 用戶端伺服器:快取和提供應用程式資源(HTML, JavaScript, images, etc.)
- 資料庫:提供瀏覽器上的資料儲存和讀取
- Thread Pool:讓應用程式可在背景執行運算,使操作上有有更好的回應
使用 Google Gears
這部份我依循官方的教學文件走一遍,首先系統必須先安裝 Google Gears,然後下載下面檔案
編輯 manifest 檔案
用文字編輯器打開 tutorial_manifest.json,首先 version 填寫上你的應用程式版本描述,如:"version": "version 1.0"。接著設定可以離線瀏覽的檔案清單,
"entries": [
{ "url": "go_offline.html"},
{ "url": "go_offline.js"},
{ "url": "gears_init.js"},
{ "url": "resources/logo.gif" },
{ "url": "http://www.example.com/index.html" }
]
範例的內容為
{
"betaManifestVersion": 1,
"version": "version 1.0",
"entries": [
{ "url": "go_offline.html"},
{ "url": "go_offline.js"},
{ "url": "gears_init.js"}
]
}
主程式源碼剖析
go_offline.html - UI |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript" src="gears_init.js"></script> <!-- Google Gears 初始化 --> <script type="text/javascript" src="go_offline.js"></script> <title>Enable Offline Usage</title> <style type="text/css"> <!-- .style1 { color: #003399; font-style: italic; font-weight: bold; font-size: large; } .style3 { color: #009933; font-weight: bold; font-style: italic; } --> </style> </head> <body onload="init()"> <h2>Getting offline-enabled documents with Google Gears </h2> <p> </p> <div> <p class="style1">Status Message: <span id="textOut" class="style3"></span></p> </div> <p><strong>Q:</strong> I want to see these documents when I'm not online! What must I do?<br /> <strong>A:</strong> <a href="http://gears.google.com/">Install Google Gears</a> on your computer and then click "Capture" to store the documents to your computer. You can then access the URLs without a network connection. </p> <p> <button onclick="createStore()" > Capture </button> </p> <p><strong>Q:</strong> I want to remove my offline access to these documents. What must I do?<br /> <strong>A: </strong>Click "Erase" below to removed the "captured" documents from your computer. The documents will no longer be available without a network connection. </p> <p> <button onclick="removeStore()" > Erase </button> </p> </body> </html> |
gears_init.js - Google Gears 初始化程式 |
(function() { // 檢查是否已經定義 Google Gear // We are already defined. Hooray! if (window.google && google.gears) { return; } // 依據不同的瀏覽器,採用不同方式產生 GearFactory var factory = null; // Firefox if (typeof GearsFactory != 'undefined') { factory = new GearsFactory(); } else { // IE try { factory = new ActiveXObject('Gears.Factory'); } catch (e) { // Safari if (navigator.mimeTypes["application/x-googlegears"]) { factory = document.createElement("object"); factory.style.display = "none"; factory.width = 0; factory.height = 0; factory.type = "application/x-googlegears"; document.documentElement.appendChild(factory); } } } // *Do not* define any objects if Gears is not installed. This mimics the // behavior of Gears defining the objects in the future. if (!factory) { return; } // 保存 Google Gear // Now set up the objects, being careful not to overwrite anything. if (!window.google) { window.google = {}; } if (!google.gears) { google.gears = {factory: factory}; } })(); |
go_offline.js - 程式邏輯 |
// 定義 Managed Store 的名稱,這個名稱可用於 createManagedStore, removeManagedStore 和 openManagedStore 三個 API var STORE_NAME = "my_offline_docset"; // 保存資源列表的 manifest 檔案名稱 var MANIFEST_FILENAME = "tutorial_manifest.json"; // Local Server var localServer; // Managed Store var store; /** * 網頁內容載入完成後執行此程序 * */ function init() { // 檢查 Google Gears 是否初始化 if (!window.google || !google.gears) { textOut("NOTE: You must install Google Gears first."); } else { // 建立 Local Server localServer = google.gears.factory.create("beta.localserver","1.0"); // 建立儲存空間 store = localServer.createManagedStore(STORE_NAME); textOut("Yeay, Google Gears is already installed."); } } /** * 資源保存在 Managed Resource * */ function createStore() { if (!window.google || !google.gears) { alert("You must install Google Gears first."); return; } // 指定 manifest 檔案 store.manifestUrl = MANIFEST_FILENAME; // 檢查資料是否過期,如果過期,下載新的資料 store.checkForUpdate(); // 等待資料保存 var timerId = window.setInterval(function() { // 假如 currentVersion 屬性有值表示 manifest 中的檔案已經都下載完成。 if (store.currentVersion) { window.clearInterval(timerId); textOut("The documents are now available offline.n" + "With your browser offline, load the document at " + "its normal online URL to see the locally stored " + "version. The version stored is: " + store.currentVersion); // 假如下載資源失敗 } else if (store.updateStatus == 3) { textOut("Error: " + store.lastErrorMessage); } }, 500); } /** * 移除在 Managed Store 中的資源 * */ function removeStore() { if (!window.google || !google.gears) { alert("You must install Google Gears first."); return; } localServer.removeManagedStore(STORE_NAME); textOut("Done. The local store has been removed." + "You will now see online versions of the documents."); } /** * 顯示文字 * */ function textOut(s) { var elm = document.getElementById("textOut"); while (elm.firstChild) { elm.removeChild(elm.firstChild); } elm.appendChild(document.createTextNode(s)); } |
測試
1. 上傳上述四個檔案到 HTTP Server
2. 用瀏覽器觀看 go_offline.html,如:http://192.168.10.120:2527/GoogleGears/go_offline.html
4. 將網頁資源移除
如果你點選 "Capture" 將資料保存在 Managed Store 中,此時網路連線切斷,並將 IE 的 Cache 清空,則重新整理該頁面,則仍然能夠看到頁面,如果此時你點選"Erase" 將資源從 Managed Store 移除,再重新瀏覽頁面會發現找不到網頁。
參考資料
[1] RSS Bling goes Offline with Google Gears
[2] Audible Ajax Episode 21: Dojo Offline on Google Gears
[3] Google Gears
[4] Google Gears - Offline Functionality for Web Apps
[5] Dojo Offline
[6] Google Reader 離線瀏覽新功能