From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../api/cookies/cookiestore/index.html | 80 ++++++++++ .../add-ons/webextensions/api/cookies/index.html | 163 +++++++++++++++++++++ .../webextensions/api/cookies/onchanged/index.html | 118 +++++++++++++++ .../api/cookies/onchangedcause/index.html | 82 +++++++++++ .../mozilla/add-ons/webextensions/api/index.html | 53 +++++++ .../add-ons/webextensions/api/storage/index.html | 103 +++++++++++++ .../webextensions/api/storage/local/index.html | 84 +++++++++++ .../api/storage/storagearea/get/index.html | 122 +++++++++++++++ .../api/storage/storagearea/index.html | 85 +++++++++++ 9 files changed, 890 insertions(+) create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/cookies/cookiestore/index.html create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/cookies/index.html create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchangedcause/index.html create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/index.html create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/storage/index.html create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/storage/local/index.html create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/storage/storagearea/get/index.html create mode 100644 files/zh-tw/mozilla/add-ons/webextensions/api/storage/storagearea/index.html (limited to 'files/zh-tw/mozilla/add-ons/webextensions/api') diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/cookiestore/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/cookiestore/index.html new file mode 100644 index 0000000000..b8ab73dc3f --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/cookiestore/index.html @@ -0,0 +1,80 @@ +--- +title: cookies.CookieStore +slug: Mozilla/Add-ons/WebExtensions/API/cookies/CookieStore +translation_of: Mozilla/Add-ons/WebExtensions/API/cookies/CookieStore +--- +
{{AddonSidebar()}}
+ +

{{WebExtAPIRef("cookies")}} API 的 CookieStore 型別代表瀏覽器中的 cookie 存放空間。

+ +

不同瀏覽模式(browsing mode)的視窗,有不同的 cookie 存放空間:例如隱私/隱身模式的視窗,會使用來自非隱私/隱身模式視窗的個別 cookie 存放空間。

+ +

型別

+ +

此型別的值都是物件,並包含以下屬性:

+ +
+
id
+
string,代表 cookie 存放空間內的唯一識別號(identifier)。
+
tabIds
+
integersarray,識別所有分享此 cookie 存放空間的瀏覽頁籤。
+
+ +

瀏覽器相容性

+ + + +

{{Compat("webextensions.api.cookies.CookieStore")}}

+ +

示例

+ +

在以下程式碼片段內,{{WebExtAPIRef("cookies.getAllCookieStores()")}} 用來查找瀏覽器內,所有目前能用 cookie 存放空間,並列出每個 cookie 存放空間的 ID、還有分享此 cookie 存放空間的頁籤。

+ +
function logStores(cookieStores) {
+  for(store of cookieStores) {
+    console.log(`Cookie store: ${store.id}\n Tab IDs: ${store.tabIds}`);
+  }
+}
+
+var getting = browser.cookies.getAllCookieStores();
+getting.then(logStores);
+ +

{{WebExtExamples}}

+ +
致謝 + +

此 API 基於 Chromium 的 chrome.cookies API 而來,文件改作自 Chromium 程式碼裡的 cookies.json

+ +

Microsoft Edge 的相容資訊來自微軟公司,原文以創用 CC 姓名標示 3.0 美國版條款授權大眾使用。

+
+ + diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/index.html new file mode 100644 index 0000000000..34da08932c --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/index.html @@ -0,0 +1,163 @@ +--- +title: cookies +slug: Mozilla/Add-ons/WebExtensions/API/cookies +tags: + - API + - Add-ons + - Cookies + - Extensions + - Interface + - Non-standard + - Reference + - WebExtensions + - 介面 + - 參考文件 + - 擴充套件 + - 附加元件 + - 非標準 +translation_of: Mozilla/Add-ons/WebExtensions/API/cookies +--- +
{{AddonSidebar}}
+ +
讓擴充套件可以取得、設定 cookies 資訊,並監控其變動。
+ +
 
+ +

使用此 API 前,必須先在 manifest.json 檔案中加入「cookies」這項 API 權限宣告,也必須以 host 權限宣告將要存取 Cookies 的網站列入。參見 Cookie 權限一節。

+ +

型別

+ +
+
{{WebExtAPIRef("cookies.Cookie")}}
+
代表一個 HTTP cookie 的相關資訊。
+
{{WebExtAPIRef("cookies.CookieStore")}}
+
代表瀏覽器中的 cookie 存放空間。
+
{{WebExtAPIRef("cookies.OnChangedCause")}}
+
代表觸發 cookie 資料變動的原因。
+
+ +

方法

+ +
+
{{WebExtAPIRef("cookies.get()")}}
+
取回單一 cookie 的相關資訊。
+
{{WebExtAPIRef("cookies.getAll()")}}
+
取回符合設定條件的所有 cookies 資訊。
+
{{WebExtAPIRef("cookies.set()")}}
+
為 cookie 設定資料。如果目前已有相同的 cookies,則會覆寫原本的 cookie 資料。
+
{{WebExtAPIRef("cookies.remove()")}}
+
刪除某特定名稱的 cookie。
+
{{WebExtAPIRef("cookies.getAllCookieStores()")}}
+
列出目前所有的 cookie 存放空間。
+
+ +

事件處理程序

+ +
+
{{WebExtAPIRef("cookies.onChanged")}}
+
當 cookie 設定或刪除時觸發。
+
+ +

權限

+ +

使用此 API 前,擴充套件應於 manifest.json 設定檔中指明需要「cookies」API 權限,亦須以 host 權限宣告指明需要存取 cookies 的網站清單。此後,符合 host 權限宣告的 URL 所能讀寫的任何 cookies,該擴充套件即可讀取。比方說:

+ +
+
http://*.example.com/
+
+

若套件有這樣的 host 權限宣告,即可:

+ +
    +
  • 讀取 www.example.com 任何路徑下的非安全 cookie。
  • +
  • 寫入 www.example.com 任何路徑下的安全或非安全 cookie。
  • +
+ +

不能

+ +
    +
  • 讀取 www.example.com 下的安全 cookie。
  • +
+
+
http://www.example.com/
+
+

若套件有這樣的 host 權限宣告,即可:

+ +
    +
  • 讀取 www.example.com 任何路徑下的非安全 cookie。
  • +
  • 讀取 .example.com 任何路徑下的非安全 cookie。
  • +
  • 寫入 www.example.com 任何路徑下的安全或非安全 cookie。
  • +
  • 寫入 .example.com 任何路徑下的安全或非安全 cookie。
  • +
+ +

不能

+ +
    +
  • 寫入 foo.example.com 的 cookie。
  • +
  • 寫入 foo.www.example.com 的 cookie。
  • +
+
+
*://*.example.com/
+
+

若套件有這樣的 host 權限宣告,即可:

+ +
    +
  • 讀、寫 www.example.com 任何路徑下的安全或非安全 cookie。
  • +
+
+
+ +

瀏覽器相容性

+ +

{{Compat("webextensions.api.cookies")}}

+ + + +

Edge 不相容資訊

+ +

Edge 不支援 promises,請使用回呼(callback)函式處理。

+ +

 {{WebExtExamples("h2")}}

+ +
致謝 + +

此 API 基於 Chromium 的 chrome.cookies API 而來,文件改作自 Chromium 程式碼裡的 cookies.json

+ +

Microsoft Edge 的相容資訊來自微軟公司,原文以創用 CC 姓名標示 3.0 美國版條款授權大眾使用。

+
+ + diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html new file mode 100644 index 0000000000..7558105bcc --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchanged/index.html @@ -0,0 +1,118 @@ +--- +title: cookies.onChanged +slug: Mozilla/Add-ons/WebExtensions/API/cookies/onChanged +translation_of: Mozilla/Add-ons/WebExtensions/API/cookies/onChanged +--- +
{{AddonSidebar()}}
+ +

{{WebExtAPIRef("cookies")}} API 的 onChanged 事件會在 cookie 設定或刪除時觸發。

+ +

請注意,更新 cookie 的屬性要透過以下兩個步驟實做:

+ +
    +
  1. 首先,要更新的 cookie 會先被刪掉,並產生一個 overwrite 的 {{WebExtAPIRef("cookies.OnChangedCause")}} 提醒。
  2. +
  3. 接著,帶著更新數值的新 cookie 會被寫進去,並產生第二個 explicit 的 {{WebExtAPIRef("cookies.OnChangedCause")}} 提醒。
  4. +
+ +

語法

+ +
browser.cookies.onChanged.addListener(listener)
+browser.cookies.onChanged.removeListener(listener)
+browser.cookies.onChanged.hasListener(listener)
+
+ +

此 API 也能以 browser.cookies.onChanged.* 運行。

+ +

此事件有以下函式:

+ +
+
addListener(callback)
+
給此事件添加監聽器(listener)。
+
removeListener(listener)
+
停止監聽此事件。listener 參數是要移除的監聽器。
+
hasListener(listener)
+
檢查此事件的 listener 是否被監聽了。若有監聽,回傳 true,否則回傳 false
+
+ +

addListener 語法

+ +

參數

+ +
+
callback
+
+

能被呼叫的 callback 函式會在此事件發生的時候觸發。函式會 passed 以下參數:

+ +
+
changeInfo
+
一個含有觸發事件資訊的 object。它有兩個屬性:
+
+
+
removed
+
一個 boolean。如果 cookie 被刪除則為 true,否則為 false
+
cookie
+
一個 {{WebExtAPIRef('cookies.Cookie')}} 物件。含有被設定、或被刪除的 cookie 資訊。
+
cause
+
一個 {{WebExtAPIRef('cookies.OnChangedCause')}} 數值。含有 cookie被改變的潛在原因。
+
+
+
+
+
+ +

瀏覽器相容性

+ + + +

{{Compat("webextensions.api.cookies.onChanged")}}

+ +

示例

+ +

本範例監聽 onChanged 事件並紀錄由 changeInfo 參數傳來的資訊:

+ +
browser.cookies.onChanged.addListener(function(changeInfo) {
+  console.log('Cookie changed: ' +
+              '\n * Cookie: ' + JSON.stringify(changeInfo.cookie) +
+              '\n * Cause: ' + changeInfo.cause +
+              '\n * Removed: ' + changeInfo.removed);
+});
+ +

{{WebExtExamples}}

+ +
致謝 + +

此 API 基於 Chromium 的 chrome.cookies API 而來,文件改作自 Chromium 程式碼裡的 cookies.json

+ +

Microsoft Edge 的相容資訊來自微軟公司,原文以創用 CC 姓名標示 3.0 美國版條款授權大眾使用。

+
+ + diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchangedcause/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchangedcause/index.html new file mode 100644 index 0000000000..592aaf7d6d --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/cookies/onchangedcause/index.html @@ -0,0 +1,82 @@ +--- +title: cookies.OnChangedCause +slug: Mozilla/Add-ons/WebExtensions/API/cookies/OnChangedCause +translation_of: Mozilla/Add-ons/WebExtensions/API/cookies/OnChangedCause +--- +
{{AddonSidebar()}}
+ +

{{WebExtAPIRef("cookies")}} API 的 OnChangedCause 型別,代表觸發 cookie 資料變動的原因。

+ +

型別

+ +

此型別的所有值都是字串(string)。可用值包括:

+ +
+
evicted
+
由於垃圾回收(garbage collection)而被刪除的 cookie。
+
expired
+
由於過期而被刪除的 cookie。
+
explicit
+
透過顯式呼叫(explicit call){{WebExtAPIRef("cookies.remove()")}} 而被插入或刪除的 cookie。
+
expired_overwrite
+
被已過期(already-expired expiration date)cookie 所覆寫的 cookie。
+
overwrite
+
A call to {{WebExtAPIRef("cookies.set()")}} overwrote this cookie with a different one.
+
+ +

瀏覽器相容性

+ + + +

{{Compat("webextensions.api.cookies.OnChangedCause")}}

+ +

示例

+ +

你可以在 cookie 變更的時候監聽被通知的 {{WebExtAPIRef("cookies.onChanged")}} 事件。此監聽器 passed 含有 cause 屬性,值為 OnChangeCaused 字串的 changeInfo 物件:

+ +
browser.cookies.onChanged.addListener(function(changeInfo) {
+  console.log('Cookie changed: ' +
+              '\n * Cookie: ' + JSON.stringify(changeInfo.cookie) +
+              '\n * Cause: ' + changeInfo.cause +
+              '\n * Removed: ' + changeInfo.removed);
+});
+ +

{{WebExtExamples}}

+ +
致謝 + +

此 API 基於 Chromium 的 chrome.cookies API 而來,文件改作自 Chromium 程式碼裡的 cookies.json

+ +

Microsoft Edge 的相容資訊來自微軟公司,原文以創用 CC 姓名標示 3.0 美國版條款授權大眾使用。

+
+ + diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/index.html new file mode 100644 index 0000000000..955086de10 --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/index.html @@ -0,0 +1,53 @@ +--- +title: JavaScript APIs +slug: Mozilla/Add-ons/WebExtensions/API +tags: + - NeedsTranslation + - TopicStub + - WebExtensions +translation_of: Mozilla/Add-ons/WebExtensions/API +--- +
{{AddonSidebar}}
+ +
+

JavaScript APIs for WebExtensions can be used inside the extension's background scripts and in any other documents bundled with the extension, including browser action or page action popups, sidebars, options pages, or new tab pages. A few of these APIs can also be accessed by an extension's content scripts (see the list in the content script guide).

+ +

To use the more powerful APIs you need to request permission in your extension's manifest.json.

+ +

You can access the APIs using the browser namespace:

+ +
function logTabs(tabs) {
+  console.log(tabs);
+}
+
+browser.tabs.query({currentWindow: true}, logTabs);
+
+ +
+

Many of the APIs are asynchronous, returning a Promise:

+ +
function logCookie(c) {
+  console.log(c);
+}
+
+function logError(e) {
+  console.error(e);
+}
+
+var setCookie = browser.cookies.set(
+  {url: "https://developer.mozilla.org/"}
+);
+setCookie.then(logCookie, logError);
+
+ +
+

Note that this is different from Google Chrome's extension system, which uses the chrome namespace instead of browser, and which uses callbacks instead of promises for asynchronous functions. As a porting aid, the Firefox implementation of WebExtensions APIs supports chrome and callbacks as well as browser and promises. Mozilla has also written a polyfill which enables code that uses browser and promises to work unchanged in Chrome: https://github.com/mozilla/webextension-polyfill.

+ +

Firefox also implements these APIs under the chrome namespace using callbacks. This allows code written for Chrome to run largely unchanged in Firefox for the APIs documented here.

+ +

Microsoft Edge uses the browser namespace, but doesn't yet support promise-based asynchronous APIs. In Edge, for the time being, asynchronous APIs must use callbacks.

+ +

Not all browsers support all the APIs: for the details, see Browser support for JavaScript APIs.

+
+ +
{{SubpagesWithSummaries}}
diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/storage/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/storage/index.html new file mode 100644 index 0000000000..8e1d68894d --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/storage/index.html @@ -0,0 +1,103 @@ +--- +title: storage +slug: Mozilla/Add-ons/WebExtensions/API/storage +tags: + - API + - 擴充套件 +translation_of: Mozilla/Add-ons/WebExtensions/API/storage +--- +
+
{{AddonSidebar}}
+ +

讓套件可以存讀資料以及監聽儲存項目的更動。

+ +

儲存系統基於 Web Storage API,有一些不同,包括:

+ + + +

要使用這個 API 你必須在 manifest.json 裡面加入 "storage" 的權限

+ +

每個套件都有自己的儲存空間,它們可以被切分爲不同種類的儲存。

+ +

雖然這個 API 與{{domxref("Window.localStorage")}}很相似,建議你不要在套件裡使用 Window.localStorage 儲存套件相關資料。Firefox 在用戶由於隱私問題清除歷史記錄與資料時會清除 localStorage API 儲存的資料,而 storage.local API 儲存的則會留著。

+ +
儲存空間不會被加密,所以你不應該把它們用來儲存用戶的機密資料。
+ +

型別

+ +
+
{{WebExtAPIRef("storage.StorageArea")}}
+
表示儲存空間的物件。
+
{{WebExtAPIRef("storage.StorageChange")}}
+
表示儲存空間變化的物件。
+
+ +

屬性

+ +

storage 有三個屬性,各自表示不同種類的儲存空間。

+ +
+
{{WebExtAPIRef("storage.sync")}}
+
表示 sync 儲存空間。sync 儲存空間裡的項目會被瀏覽器同步,所以可以跨裝置在所有已登入瀏覽器實例裡面使用。
+
{{WebExtAPIRef("storage.local")}}
+
表示 local 儲存空間。local 儲存空間裡的項目會被侷限在安裝套件的機器上。
+
{{WebExtAPIRef("storage.managed")}}
+
表示 managed 儲存空間。managed 儲存空間的項目由網域管理者設置而且對套件唯讀,修改這項會導致錯誤。
+
+ +

事件

+ +
+
{{WebExtAPIRef("storage.onChanged")}}
+
當儲存空間裡的一個或更多項目被修改時觸發。
+
+ +

瀏覽器兼容性

+
+ +

{{Compat("webextensions.api.storage")}}

+ +

{{WebExtExamples("h2")}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.storage API. This documentation is derived from storage.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/storage/local/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/storage/local/index.html new file mode 100644 index 0000000000..3cdc3ab140 --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/storage/local/index.html @@ -0,0 +1,84 @@ +--- +title: storage.local +slug: Mozilla/Add-ons/WebExtensions/API/storage/local +translation_of: Mozilla/Add-ons/WebExtensions/API/storage/local +--- +
{{AddonSidebar()}}
+ +

代表 local 儲存空間。通常 local 裡面的東西,會放在套件安裝的地方。

+ +

瀏覽器可能會限制套件本地可儲存的資料數量:

+ + + +

如果套件被移除、相關的儲存資料也會一併移除。

+ +

在 Firefox 內,你可以透過 about:config 內設定 keepUuidOnUninstall 與 keepStorageOnUninstall 為 true 以避免瀏覽器在移除套件時,一併移除相關的儲存資料。這個功能是為了方便開發者除錯,套件本身無法改變這個設定。

+ +

雖然這 API 與 {{domxref("Window.localStorage")}} 相似,但不建議在套件內使用 Window.localStorage。在某些情況下,用戶會出於隱私上的理由,要求 Firefox 清理瀏覽紀錄與資料,這其中就包含使用 localStorage API 的資料。另一方面,storage.local API 的資料,在這種情況下會予以保留。

+ +

方法

+ +

local 物件實做了定義於 {{WebExtAPIRef("storage.StorageArea")}} 類別的方法:

+ +
+
{{WebExtAPIRef("storage.StorageArea.get()")}}
+
取得一個或多個源自儲存空間的項目。
+
{{WebExtAPIRef("storage.StorageArea.getBytesInUse()")}}
+
取得儲存空間內,一個或多個已為項目所使用的容量。單位為 byte。
+
{{WebExtAPIRef("storage.StorageArea.set()")}}
+
Stores one or more items in the storage area. If the item already exists, its value will be updated. When you set a value, the {{WebExtAPIRef("storage.onChanged")}} event will fire.
+
{{WebExtAPIRef("storage.StorageArea.remove()")}}
+
刪除一個或多個儲存空間內的項目。
+
{{WebExtAPIRef("storage.StorageArea.clear()")}}
+
刪除所有儲存空間內的項目。
+
+ +

瀏覽器相容性

+ + + +

{{Compat("webextensions.api.storage.local")}}

+ +

{{WebExtExamples}}

+ +
致謝 + +

This API is based on Chromium's chrome.storage API. This documentation is derived from storage.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/storage/storagearea/get/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/storage/storagearea/get/index.html new file mode 100644 index 0000000000..744cc6ed8b --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/storage/storagearea/get/index.html @@ -0,0 +1,122 @@ +--- +title: StorageArea.get() +slug: Mozilla/Add-ons/WebExtensions/API/storage/StorageArea/get +translation_of: Mozilla/Add-ons/WebExtensions/API/storage/StorageArea/get +--- +
{{AddonSidebar()}}
+ +

從儲存空間內檢查一個或多個單元(item)。

+ +

這個非同步函式會回傳 Promise

+ +

語法

+ +
let gettingItem = browser.storage.<storageType>.get(
+  keys    // null, string, object or array of strings
+)
+
+ +

<storageType> 會是以下可覆寫的儲存類型之一:{{WebExtAPIRef("storage.sync")}} 或 {{WebExtAPIRef("storage.local")}}。

+ +

參數

+ +
+
keys
+
用來識別要檢查單元的 key(單個為字串;多個為陣列、或指定預設值的物件)。如果把這裡留空(空字串、空陣列、空物件都可以),就會取得空物件。如果是 null 或 undefined,則會取得所有儲存的內容。
+
+ +

回傳值

+ +

Promise that will be fulfilled with a results object containing every object in keys that was found in the storage area. If the operation failed, the promise will be rejected with an error message.

+ +
+

When used within a content script in Firefox versions prior to 52, the Promise returned by browser.storage.local.get() is fulfilled with an Array containing one Object. The Object in the Array contains the keys found in the storage area, as described above. The Promise is correctly fulfilled with an Object when used in the background context (background scripts, popups, options pages, etc.). When this API is used as chrome.storage.local.get(), it correctly passes an Object to the callback function.

+
+ +

瀏覽器相容性

+ +

{{Compat("webextensions.api.storage.StorageArea.get")}}

+ +

示例

+ +

假設儲存空間有以下單元:

+ +
// 兩個單元:「kitten」與「monster」
+browser.storage.local.set({
+  kitten:  {name:"Mog", eats:"mice"},
+  monster: {name:"Kraken", eats:"people"}
+});
+ +

Define success and failure handlers for the promise:

+ +
function onGot(item) {
+  console.log(item);
+}
+
+function onError(error) {
+  console.log(`Error: ${error}`);
+}
+ +

With no keys argument, retrieve everything:

+ +
let gettingItem = browser.storage.local.get();
+gettingItem.then(onGot, onError);
+
+// -> Object { kitten: Object, monster: Object }
+ +

With an empty keys argument, return nothing:

+ +
// with an empty array, retrieve nothing
+let gettingItem = browser.storage.local.get([]);
+gettingItem.then(onGot, onError);
+
+// -> Object { }
+ +

With the name of an object, retrieve the match:

+ +
let gettingItem = browser.storage.local.get("kitten");
+gettingItem.then(onGot, onError);
+
+// -> Object { kitten: Object }
+ +

With an array of object names, retrieve all matches:

+ +
let gettingItem = browser.storage.local.get(["kitten", "monster", "grapefruit"]);
+gettingItem.then(onGot, onError);
+
+// -> Object { kitten: Object, monster: Object } 
+ +

With an object with object names as keys and the default value as value:

+ +
let gettingItem = browser.storage.local.get({
+  kitten: "no kitten",
+  monster: "no monster",
+  grapefruit: {
+    name: "Grape Fruit",
+    eats: "Water"
+  }
+});
+
+// -> Object { kitten: Object, monster: Object, grapefruit: Object }
+
+ +

{{WebExtExamples}}

+ +

Chrome 示例

+ +
chrome.storage.local.get("kitten", function(items){
+  console.log(items.kitten);  // -> {name:"Mog", eats:"mice"}
+});
+ +

Or with an arrow function

+ +
chrome.storage.local.get("kitten", items=>{
+  console.log(items.kitten); // -> {name:"Mog", eats:"mice"}
+});
+ +
致謝 + +

This API is based on Chromium's chrome.storage API. This documentation is derived from storage.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
diff --git a/files/zh-tw/mozilla/add-ons/webextensions/api/storage/storagearea/index.html b/files/zh-tw/mozilla/add-ons/webextensions/api/storage/storagearea/index.html new file mode 100644 index 0000000000..088e8b5a79 --- /dev/null +++ b/files/zh-tw/mozilla/add-ons/webextensions/api/storage/storagearea/index.html @@ -0,0 +1,85 @@ +--- +title: storage.StorageArea +slug: Mozilla/Add-ons/WebExtensions/API/storage/StorageArea +tags: + - API + - Add-ons + - Extensions + - NeedsTranslation + - Non-standard + - Reference + - Storage + - StorageArea + - TopicStub + - Type + - WebExtensions +translation_of: Mozilla/Add-ons/WebExtensions/API/storage/StorageArea +--- +
{{AddonSidebar()}}
+ +

StorageArea is an object representing a storage area.

+ +

Type

+ +

Values of this type are objects.

+ +

Methods

+ +
+
{{WebExtAPIRef("storage.StorageArea.get()")}}
+
Retrieves one or more items from the storage area.
+
{{WebExtAPIRef("storage.StorageArea.getBytesInUse()")}}
+
Gets the amount of storage space (in bytes) used one or more items being stored in the storage area.
+
{{WebExtAPIRef("storage.StorageArea.set()")}}
+
Stores one or more items in the storage area. If an item already exists, its value will be updated.
+
{{WebExtAPIRef("storage.StorageArea.remove()")}}
+
Removes one or more items from the storage area.
+
{{WebExtAPIRef("storage.StorageArea.clear()")}}
+
Removes all items from the storage area.
+
+ +

Browser compatibility

+ + + +

{{Compat("webextensions.api.storage.StorageArea")}}

+ +

{{WebExtExamples}}

+ +
Acknowledgements + +

This API is based on Chromium's chrome.storage API. This documentation is derived from storage.json in the Chromium code.

+ +

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.

+
+ + -- cgit v1.2.3-54-g00ecf