1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
---
title: storage
slug: Mozilla/Add-ons/WebExtensions/API/storage
tags:
- API
- 擴充套件
translation_of: Mozilla/Add-ons/WebExtensions/API/storage
---
<div>
<div>{{AddonSidebar}}</div>
<p>讓套件可以存讀資料以及監聽儲存項目的更動。</p>
<p>儲存系統基於 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API">Web Storage API</a>,有一些不同,包括:</p>
<ul>
<li>非同步</li>
<li>值的作用域在套件而不是某個特定的網域(後端的所有腳本與內容腳本都可用同樣的鍵值)。</li>
<li>儲存的值可以是任何的 JSON-ifiable 值而並非只能是 <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>。這包括了: <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">陣列</a></code>、<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">物件</a></code>。但只有在內容可以被以JSON表示的時候,這表示不包含DOM節點。你不需要特地把值轉爲JSON <code>Strings</code>來儲存它們,它們在內部就是以JSON來表示的。</li>
<li>同一個API呼叫中可以設置或取得複數鍵值。</li>
</ul>
<p>要使用這個 API 你必須在 <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json">manifest.json</a> 裡面加入 "storage" 的<a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions">權限</a>。</p>
<p>每個套件都有自己的儲存空間,它們可以被切分爲不同種類的儲存。</p>
<p>雖然這個 API 與{{domxref("Window.localStorage")}}很相似,建議你不要在套件裡使用 <code>Window.localStorage</code> 儲存套件相關資料。Firefox 在用戶由於隱私問題清除歷史記錄與資料時會清除 localStorage API 儲存的資料,而 <code><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/local">storage.local</a></code> API 儲存的則會留著。</p>
<div class="note">儲存空間不會被加密,所以你不應該把它們用來儲存用戶的機密資料。</div>
<h2 id="型別">型別</h2>
<dl>
<dt>{{WebExtAPIRef("storage.StorageArea")}}</dt>
<dd>表示儲存空間的物件。</dd>
<dt>{{WebExtAPIRef("storage.StorageChange")}}</dt>
<dd>表示儲存空間變化的物件。</dd>
</dl>
<h2 id="屬性">屬性</h2>
<p><code>storage</code> 有三個屬性,各自表示不同種類的儲存空間。</p>
<dl>
<dt>{{WebExtAPIRef("storage.sync")}}</dt>
<dd>表示 <code>sync</code> 儲存空間。<code>sync</code> 儲存空間裡的項目會被瀏覽器同步,所以可以跨裝置在所有已登入瀏覽器實例裡面使用。</dd>
<dt>{{WebExtAPIRef("storage.local")}}</dt>
<dd>表示 <code>local</code> 儲存空間。<code>local</code> 儲存空間裡的項目會被侷限在安裝套件的機器上。</dd>
<dt>{{WebExtAPIRef("storage.managed")}}</dt>
<dd>表示 <code>managed</code> 儲存空間。<code>managed</code> 儲存空間的項目由網域管理者設置而且對套件唯讀,修改這項會導致錯誤。</dd>
</dl>
<h2 id="事件">事件</h2>
<dl>
<dt>{{WebExtAPIRef("storage.onChanged")}}</dt>
<dd>當儲存空間裡的一個或更多項目被修改時觸發。</dd>
</dl>
<h2 id="瀏覽器兼容性">瀏覽器兼容性</h2>
</div>
<p>{{Compat("webextensions.api.storage")}}</p>
<p>{{WebExtExamples("h2")}}</p>
<div class="note"><strong>Acknowledgements</strong>
<p>This API is based on Chromium's <a href="https://developer.chrome.com/extensions/storage"><code>chrome.storage</code></a> API. This documentation is derived from <a href="https://chromium.googlesource.com/chromium/src/+/master/extensions/common/api/storage.json"><code>storage.json</code></a> in the Chromium code.</p>
<p>Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.</p>
</div>
<div class="hidden">
<pre>// Copyright 2015 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>
</div>
|