1

I'm trying to access the localStorage object from my chrome extension's background.js file. The localStorage object on a regular webpage shows me items from different webpages (including the ones I'm interested in), but the ones I access from background.js or popup.js are empty. How do I access the regular localStorage (with items set by another webpage) from my Chrome extension?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
tldr
  • 11,924
  • 15
  • 75
  • 120
  • Kind of inverse of the question http://stackoverflow.com/questions/3937000/chrome-extension-accessing-localstorage-in-content-script – Xan Oct 16 '14 at 21:17

1 Answers1

3

The localStorage object is relative to the "local environment", this means that the localStorage object on http://www.google.com is totally different from the one on http://stackoverflow.com, and obviously is totally different from the one in the background page of your extension.

Given that, if you want to store something in your extension, you'll need to use the localStorage object in your background page, not in the page of some site you are injecting content scripts to.

If you want to access web pages' localStorages, then you'll have to send a content script on it, retrieve the localStorage object, and send it to the background.js script with a message (see chrome extensions message passing).

Documentation on localStorage from MDN: here.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 1
    Pages store some interesting data in their `localStorage` sometimes. So that's the point. – Xan Oct 16 '14 at 21:18
  • Emphasis on the fact that `localStorage` is part of DOM, and as such is only accessible from content scripts of open tabs. – Xan Oct 16 '14 at 21:19