Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google chrome extension: local storage

I'm developing an extension for Google Chrome, and have run into some trouble.I created an options.html page and added it to the manifest.json file.The page shows properly.

I saved the options, and then went back to the page on which the extension is supposed to run.

Unfortunately, the Local storage for the options was returning a 'null' instead of the option. If I set the local storage option directly from the extension's JS script, it works fine but not if it was set from the options page.

Any idea how i can access the options.html local storage values from my Javascript file in the extension?

like image 684
Rohan Avatar asked Jun 13 '10 20:06

Rohan


People also ask

Do Chrome extensions have local storage?

Items in local storage are local to the machine the extension is installed on. The browser may restrict the amount of data that an extension can store in the local storage area. For example: In Chrome, an extension is limited to storing 5MB of data using this API unless it has the "unlimitedStorage" permission.

How do I view Chrome extensions in local storage?

With the extension's background page open, just go to the developer tools by pressing F12, then go to the Application tab. In the Storage section expand Local Storage. After that, you'll see all your browser's local storage there.

Can Chrome extensions access local files?

For security reasons, by default the Chrome browser does not allow extensions to access local files. If you want the accelerator to access local files (locations of "file:///...", instead of "http://" or "https://"), you must configure Chrome to allow the access.

What is Chrome local storage?

The localStorage read-only property of the window interface allows you to access a Storage object for the Document 's origin; the stored data is saved across browser sessions.


2 Answers

You can set them using either

localStorage["var"]=data;

or

localStorage.var=data;

Load them using

var myvar = localStorage.var;

You can read about the API here.

like image 157
leegeorg07 Avatar answered Sep 22 '22 12:09

leegeorg07


The correct method is:

if (typeof(localStorage) == ‘undefined’ ) {
  alert(‘Your browser does not support HTML5 localStorage. Try upgrading.’);
}
else {
  try {
    localStorage.setItem(“name”, “Hello World!”); //saves to the database, “key”, “value”
  }
  catch (e) {
    if (e == QUOTA_EXCEEDED_ERR) {
      alert(‘Quota exceeded!’); //data wasn’t successfully saved due to quota exceed so throw an error
    }
  }
  document.write(localStorage.getItem(“name”)); //Hello World!
  localStorage.removeItem(“name”); //deletes the matching item from the database
}

REFERENCE: http://html5tutorial.net/tutorials/working-with-html5-localstorage.html

like image 33
DJ' Avatar answered Sep 22 '22 12:09

DJ'