0

I'm creating a Chrome extension that gets the current active textbox and puts a predefined text into it.

I want to use the localStorage, and the question is. Does a extension have his own localStorage or must it use the LS from the website?

And if the extension does not have his own LS, how can I achieve to save some data on the local computer?

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Koen Hollander
  • 1,687
  • 6
  • 27
  • 42

2 Answers2

3

The answer depends on whether the extension is a content script or not.

If it's a content script, it runs within the page's origin, and so uses the page's localStorage.

If it's not a content script, then according to this answer to a question about storage limits, yes, your extension gets its own local storage because its origin is chrome-extension://EXTENSIONIDHERE. localStorage is stored per origin, so your extension will have its own local storage.

As an alternative, as I mentioned in a comment, you can use chrome.storage (available to content scripts and non-content scripts alike), which has options for syncing, can store objects rather than just strings, and various other advantages.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Google Chrome released the storage API: http://developer.chrome.com/extensions/storage.html

Question 1: But I want to use the localStorage, and the question is. Does a extension have his own localStorage (I use LS as abbreviation) or must it use the LS from the website Answer: yes Question 2: And if the extension does not have his own LS, how can I achieve to save some data on the local computer? Answer: it has, so it is passed by.

coenni
  • 437
  • 4
  • 14
  • 2
    That's true, but it doesn't answer the question about `localStorage` (which is why I posted it as a comment instead of an answer as you were posting your answer). – T.J. Crowder Mar 25 '17 at 10:59
  • Yes it has its own local storage. docs: 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. – coenni Mar 25 '17 at 11:04
  • But is that, in fact, the origin used by an extension when it accesses `localStorage`? Extensions are an interesting beastie. I mean, I'd certainly *hope* it was separate... – T.J. Crowder Mar 25 '17 at 11:06
  • And the answer to "does it use google.com" appears to be "no," which is not surprising. – T.J. Crowder Mar 25 '17 at 11:08
  • Question 1: But I want to use the localStorage, and the question is. Does a extension have his own localStorage (I use LS as abbreviation) or must it use the LS from the website Answer: yes Question 2: And if the extension does not have his own LS, how can I achieve to save some data on the local computer? Answer: it has, so it is passed by. – coenni Mar 25 '17 at 11:11
  • You meant `update 2012`? chrome.storage is available since Chrome 20 which was released 5 years ago. What kind of answer is this, seriously? – wOxxOm Mar 25 '17 at 16:33