Using Browser Storage In SUSI Firefox Add-On
Browser storage API enables extensions to store and retrieve data, and listen for changes to the stored item. In SUSI Firefox add-on the browser storage is implemented to store the chat history in the browser memory. This allows the extension to maintain persistence when the popup is closed or opened. This article gives a brief idea about how to implement browser storage using SUSI Firefox add-on as a use case. Adding Permission To use this API you need to include the "storage" permission in your manifest.json file. “permissions”: [ “storage” ] Adding this in the manifest.json file will grant your extension the permission to use the storage API Properties of Storage There are three properties of storage which allows the developer to use different types of storage areas. One can choose a suitable property to meet the requirements. Storage.sync - Represents the sync storage area. Data stored in sync storage are synced by the browser and are available across all instances of that browser (even on different devices) that the user is logged in. Note - Currently storage.sync is supported by modern browsers and recent versions of Firefox. Please refer the documentation for more details. Storage.local - Represents the local storage area. Items in local storage are local to the machine the extension was installed on. Storage.managed - Represents the managed storage area. Items in managed storage are set by the domain administrator and are read-only for the extension. Trying to modify this namespace results in an error. In SUSI Firefox add-on Storage.sync is being used to leverage the benefit of storage sync across all instances of the browser. So the chat history and settings are synced across the browsers logged in by the user. Storing Data browser.storage.<storageType>.set(keys) - This function allows one or more item to be stored in the browser storage. If there in key-value already existing then the value will be updated to the new value supplied in the function. The function returns a promise. Note - <storageType> will be one of the writable storage types — storage.sync or storage.local This is a snippet taken from SUSI Firefox add-on where the theme value from settings page is being stored in the browser storage. if(response.settings.theme){ browser.storage.sync.set({ theme: response.settings.theme }); } Retrieving Data browser.storage.<storageType>.get(keys) - Retrieves one or more items from the storage area. A key or array of keys is used to identify the item(s) to be retrieved from storage. If an empty string, object or array is passed, an empty object will be retrieved. If you pass null, or an undefined value, the entire storage contents will be retrieved. The function returns a promise. Note - <storageType> will be one of the writable storage types — storage.sync or storage.local This is a snippet taken from SUSI Firefox add-on where the settings preferences are being persisted by retrieving setting values from storage. var buffer = browser.storage.sync.get(null); buffer.then(function(res){ if(res["theme"]){ if(res["theme"]=="dark"){ $("#theme").val("dark"); } else{ $("#theme").val("light"); } } if(res["loggedUser"]){ loginForm.style.display="none"; logoutButton.style.display="block"; } else{ loginForm.style.display="block"; logoutButton.style.display="none"; } }); Null values are being…
