--- title: storage slug: Mozilla/Add-ons/WebExtensions/API/storage tags: - API - 擴充套件 translation_of: Mozilla/Add-ons/WebExtensions/API/storage ---
讓套件可以存讀資料以及監聽儲存項目的更動。
儲存系統基於 Web Storage API,有一些不同,包括:
String
。這包括了: 陣列
、物件
。但只有在內容可以被以JSON表示的時候,這表示不包含DOM節點。你不需要特地把值轉爲JSON Strings
來儲存它們,它們在內部就是以JSON來表示的。要使用這個 API 你必須在 manifest.json 裡面加入 "storage" 的權限。
每個套件都有自己的儲存空間,它們可以被切分爲不同種類的儲存。
雖然這個 API 與{{domxref("Window.localStorage")}}很相似,建議你不要在套件裡使用 Window.localStorage
儲存套件相關資料。Firefox 在用戶由於隱私問題清除歷史記錄與資料時會清除 localStorage API 儲存的資料,而 storage.local
API 儲存的則會留著。
storage
有三個屬性,各自表示不同種類的儲存空間。
sync
儲存空間。sync
儲存空間裡的項目會被瀏覽器同步,所以可以跨裝置在所有已登入瀏覽器實例裡面使用。local
儲存空間。local
儲存空間裡的項目會被侷限在安裝套件的機器上。managed
儲存空間。managed
儲存空間的項目由網域管理者設置而且對套件唯讀,修改這項會導致錯誤。{{Compat("webextensions.api.storage")}}
{{WebExtExamples("h2")}}
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.
// 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.