Working with storage
For an extension to store data that can persist across different web pages and domains you have two options: Use a remote server, or store it locally in the browser.
Anything you store from your extension content scripts is shared with (and attached to) the user's current web domain. So...
- ✅ Easy to view with Dev Tools (Application > Local Storage)
- ❌ Data does not persist across web pages; attached to the specific domain
Alternately, from an extension background script....
-
To share data across different web pages using localStorage
you must use messaging between your content and background pages (manifest v3 requires all backend scripts to use service workers), which have a chrome:// URI (thus linked to the extension, not the domain).
-
⚠️ Difficult to view with Dev Tools
-
⚠️ Browser extension ✅ service workers can store data (but content scripts require messaging)
Additionally, for extension development, keep in mind that:
- ⚠️ Data must be serialized / deserialized using
JSON.stringify()
- ⚠️ Synchonous (large amounts of data can create latency)
- ❌ Data will not persist if users clear browsing history and data for privacy reasons
Example
localStorage.setItem('myObject', JSON.stringify({"message":"hello"}));
chrome.storage
lets extensions to store data in a location that isn't accessible to normal web pages.
- Can be used from (both!) Browser extension ✅ content scripts and ✅ service workers
- ✅ Asynchronous (Promise-based)
- ✅ Data will persist if users clear browsing history and data for privacy reasons
One caveat, in Chrome, JavaScript APIs are accessed under the chrome
namespace. In Firefox and Edge, they are accessed under the browser
namespace. But, Firefox supports both the chrome and browser namespaces. You can fix it with
if (typeof browser === "undefined") {
var browser = chrome;
}
Here is an example of chrome.storage
let obj = { message: 123 }
chrome.storage.local.set({obj})
let results = chrome.storage.local.get('abc')
console.log(results)
Development
Quick method
- From chrome://extensions open the service worker for your extension
- In DevTools, run the following in the console
chrome.storage.local.get(console.log)
For ongoing development
- Install https://github.com/jusio/storage-area-explorer
- Get your extension id at chrome://extensions
- Open
chrome-extension://<your_extension_id>/manifest.json
IndexedDB
Another option - complex to use unless you have a go-between
https://github.com/localForage/localForage