Maintaining Extension State in SUSI.AI Chrome Bot Using Chrome Storage API
SUSI Chrome Bot is a browser action chrome extension which is used to communicate with SUSI AI.The browser action extension in chrome is like any other web app that you visit. It will store all your preferences like theme settings and speech synthesis settings and data till you are interacting with it, but once you close it, it forgets all of your data unless you are saving it in some database or you are using cookies for the session. We want to be able to save the chats and other preferences like theme settings the user makes when interacting with SUSI AI through Susi Chrome Bot. In this blog, we’ll explore Chrome’s chrome.storage API for storing data. What options do we have for storing data offline? IndexedDB: IndexedDB is a low-level API for client-side storage of data. IndexedDB allows us to store a large amount of data and works like RDBMS but IndexedDB is javascript based Object-oriented database. localStorage API: localStorage allows us to store data in key/value pairs which is much more effective than storing data in cookies. localStorage data persists even if the user closes and reopens the browser. Chrome.storage: Chrome provides us with chrome.storage. It provides the same storage capabilities as localStorage API with some advantages. For susi_chromebot we will use chrome.storage because of the following advantages it has over the localstorage API: User data can be automatically synced with Chrome sync if the user is logged in. The extension's content scripts can directly access user data without the need for a background page. A user's extension settings can be persisted even when using incognito mode. It's asynchronous so bulk read and write operations are faster than the serial and blocking localStorage API. User data can be stored as objects whereas the localStorage API stores data in strings. Integrating chrome.storage to susi_chromebot for storing chat data To use chrome.storage we first need to declare the necessary permission in the extension’s manifest file. Add “storage” in the permissions key inside the manifest file. "permissions": [ "storage" ] We want to store the chat user has made with SUSI. We will use a Javascript object to store the chat data. var storageObj = { senderClass: "", content: "" }; The storageObj object has two keys namely senderClass and content. The senderClass key represents the sender of the message(user or susi) whereas the content key holds the actual content of the message. We will use chrome.storage.get and chrome.storage.set methods to store and retrieve data. var susimessage = newDiv.innerHTML; storageObj.content = susimessage; storageObj.senderClass = "susinewmessage"; chrome.storage.sync.get("message",(items) => { if(items.message){ storageArr = items.message; } storageArr.push(storageObj); chrome.storage.sync.set({"message":storageArr},() => { console.log("saved"); }); }); In the above code snippet, susimessage contains the actual message content sent by the SUSI server. We then set the correct properties of the storageObj object that we declared earlier. Now we can use chrome.storage.set to save the storageObj object but that would overwrite the current data that we have inside chrome’s StorageArea. To prevent the old message data…
