I want my chrome extension to save some information, and I'm not sure how to start the code... I need it to save strings. For example - The user inputs a string (In a text area over the popup) and this string is shown on the popup. When the user exits it and returns I want the string to remain the same. (I belive it has to save it) I don't want to save it on my server or anything like that, I want it to be saved on the users cache or something.
One way to share data between a background script and the other scripts that make up the extension is to save the data to a location which is accessible to all the scripts in the extension. We can use the browser localStorage API or chrome. storage which is specific to Chrome extensions.
When extensions are installed into Chrome they are extracted into the C:\Users\[login_name]\AppData\Local\Google\Chrome\User Data\Default\Extensions folder. Each extension will be stored in its own folder named after the ID of the extension.
Google Chrome Click “Advanced” at the bottom of the page. Click on the “Site Settings” button under the Privacy and Security section. Click on “Cookies”. Toggle on the setting for “Allow sites to save and read cookie data (recommended)”.
You can now leverage Google Chrome's storage
API to do this as well. Unlike localStorage
, this is accessible from content scripts as well.
// Somewhere in your manifest... { "permissions": [ "storage" ] } // Usage: // PERSISTENT Storage - Globally // Save data to storage across their browsers... chrome.storage.sync.set({ "yourBody": "myBody" }, function(){ // A data saved callback omg so fancy }); chrome.storage.sync.get(/* String or Array */["yourBody"], function(items){ // items = [ { "yourBody": "myBody" } ] }); // LOCAL Storage // Save data to storage locally, in just this browser... chrome.storage.local.set({ "phasersTo": "awesome" }, function(){ // Data's been saved boys and girls, go on home }); chrome.storage.local.get(/* String or Array */["phasersTo"], function(items){ // items = [ { "phasersTo": "awesome" } ] });
More info on how these shenanigans work here: https://developer.chrome.com/extensions/storage#type-StorageArea
Former answer:
Use localStorage. Google Chrome implements some features of HTML5, and it is one of them.
//Pull text from user inputbox var data = document.getElementById("this_input").value; //Save it to the localStorage variable which will always remember what you store in it localStorage["inputText"] = data;
You should note you can only access your storage from the background page (no content scripts) so you'll have to use messaging for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With