Using localStorage

455 views
Skip to first unread message

Pedro Ferreira

unread,
Sep 6, 2010, 5:32:12 PM9/6/10
to Chromium-extensions
Hi, I'm currently working on a extension for chrome, and until now
I've been working on it as a local file(and opening with chrome).

I'm trying now as an extension, I've loaded it and it opens fine, the
extension saves data to localStorage, but when I reopen the extension
everything is gone, nothing is saved, but it works fine when I open it
as a file.

I've been reading a bit and it seems I have to have a background.html
page, where I'm guessing here is where localStorage is allowed?

Could someone point me to, or explain how to make this working.
thanks

Jesselnz

unread,
Sep 6, 2010, 10:45:40 PM9/6/10
to Chromium-extensions
You probably want to store any persistent data in the background
page's localStorage, which you can access from other extension pages
through chrome.extension.getBackgroundPage().localStorage. So for
example, if you have an options page you can do "var bg =
chrome.extension.getBackgroundPage()" and use bg.localStorage to store
the options (the same can be done to retrieve them in another view).

See: http://code.google.com/chrome/extensions/overview.html

Dan Silivestru

unread,
Sep 7, 2010, 12:22:38 PM9/7/10
to Chromium-extensions
Hi Pedro,

In the file:/// scheme in Chrome, localStorage is per file. so if you
got to file:///foo.html and save something, it won't be available in
file:///bar.html.

You can consider using SQLite storage, that database is the same for
the entire file:/// scheme.

the background page local storage is also a good option, but keep in
mind that you don't have access to save to the background page's local
storage from inside a content script.

Hope this helps,

Dan.

On Sep 6, 5:32 pm, Pedro Ferreira <darkiii...@gmail.com> wrote:

Pedro Ferreira

unread,
Sep 7, 2010, 1:10:11 PM9/7/10
to Chromium-extensions
Well, I don't know how to set a sql database, is it like using the
localStorage?

What do you mean with, inside content script?

Basically what I'm trying to do is save/load from localStorage from
the popup page (on load and on unload).
Could it work by accessing from the popup to the background and save/
load into the background one?

(so I could just change the code to add: 'var bg =
chrome.extension.getBackgroundPage()' , and then do as I'm doing
already?)

Akshay Dua

unread,
Sep 7, 2010, 2:25:20 PM9/7/10
to Pedro Ferreira, Chromium-extensions
On Tue, Sep 7, 2010 at 10:10 AM, Pedro Ferreira <darki...@gmail.com> wrote:
> Well, I don't know how to set a sql database, is it like using the
> localStorage?
>

Using an SQL Db is not similar to using the 50MB localStorage per file
(which is way easier to use) but it does provide you with much more
storage. Even unlimited storage, as long as you use the permission
'unlimited_storage' in your manifest file. Notice that official Google
extensions API suggests the keyword unlimitedStorage, but this does
not work for some reason.

> What do you mean with, inside content script?
>

At a high-level, an extension can have three separate units: code that
runs in the background (like in background.html), a popup that
activates when your extension's icon on the browser is clicked, and a
content script that has access to the page DOM. Each of these units
can communicate with each other using the messaging API (see official
developer's guide at
http://code.google.com/chrome/extensions/devguide.html). The popup
does not have access to any other page but itself, the content script
only has access to those pages that it takes permissions for in the
manifest file, and the background code has access to the extension but
not any other pages e.g. in a browser tab (this is what the content
script is for).

The only unit of your extension that can access local storage (or
database) is the background code, so if other units of your extension
e.g. popup, or a content script that activates when some URL is
visited, wants to store some data, they need to send a message with
that data to the background page.

> Basically what I'm trying to do is save/load from localStorage from
> the popup page (on load and on unload).
> Could it work by accessing from the popup to the background and save/
> load into the background one?
>

Yes, that's how it will work. Send a message from popup to background.

> (so I could just change the code to add: 'var bg =
> chrome.extension.getBackgroundPage()' , and then do as I'm doing
> already?)
>
>

Nope. You need to use the messaging API.
http://code.google.com/chrome/extensions/messaging.html

--Daku

>
> On Sep 7, 5:22 pm, Dan Silivestru <dan.silives...@gmail.com> wrote:
>> Hi Pedro,
>>
>> In the file:/// scheme in Chrome, localStorage is per file. so if you
>> got to file:///foo.html and save something, it won't be available in
>> file:///bar.html.
>>
>> You can consider using SQLite storage, that database is the same for
>> the entire file:/// scheme.
>>
>> the background page local storage is also a good option, but keep in
>> mind that you don't have access to save to the background page's local
>> storage from inside a content script.
>>
>> Hope this helps,
>>
>> Dan.
>>
>> On Sep 6, 5:32 pm, Pedro Ferreira <darkiii...@gmail.com> wrote:
>>
>>
>>
>>
>>
>>
>>
>> > Hi, I'm currently working on a extension for chrome, and until now
>> > I've been working on it as a local file(and opening with chrome).
>>
>> > I'm trying now as an extension, I've loaded it and it opens fine, the
>> > extension saves data to localStorage, but when I reopen the extension
>> > everything is gone, nothing is saved, but it works fine when I open it
>> > as a file.
>>
>> > I've been reading a bit and it seems I have to have a background.html
>> > page, where I'm guessing here is where localStorage is allowed?
>>
>> > Could someone point me to, or explain how to make this working.
>> > thanks
>

> --
> You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
> To post to this group, send email to chromium-...@chromium.org.
> To unsubscribe from this group, send email to chromium-extens...@chromium.org.
> For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.
>
>

Jesselnz

unread,
Sep 7, 2010, 3:27:13 PM9/7/10
to Chromium-extensions

> > (so I could just change the code to add: 'var bg =
> > chrome.extension.getBackgroundPage()' , and then do as I'm doing
> > already?)
>
> Nope. You need to use the messaging API.http://code.google.com/chrome/extensions/messaging.html

This is incorrect. Any pages in your extension with the exception of
content scripts (if you don't know what they are, then I'm assuming
you're not using them) run in the same process and can access each
others' variables and functions. If you retrieve the background
page's window object with chrome.extension.getBackgroundPage(), you
can use it to access the background page's localStorage object.

Pedro Ferreira

unread,
Sep 7, 2010, 3:28:18 PM9/7/10
to Chromium-extensions
thanks, I think I'm understanding how it works. Now its time to try a
bit, and see how it goes. :)

On Sep 7, 7:25 pm, Akshay Dua <aks...@cs.pdx.edu> wrote:
> On Tue, Sep 7, 2010 at 10:10 AM, Pedro Ferreira <darkiii...@gmail.com> wrote:
> > Well, I don't know how to set a sql database, is it like using the
> > localStorage?
>
> Using an SQL Db is not similar to using the 50MB localStorage per file
> (which is way easier to use) but it does provide you with much more
> storage. Even unlimited storage, as long as you use the permission
> 'unlimited_storage' in your manifest file. Notice that official Google
> extensions API suggests the keyword unlimitedStorage, but this does
> not work for some reason.
>
> > What do you mean with, inside content script?
>
> At a high-level, an extension can have three separate units: code that
> runs in the background (like in background.html), a popup that
> activates when your extension's icon on the browser is clicked, and a
> content script that has access to the page DOM. Each of these units
> can communicate with each other using the messaging API (see official
> developer's guide athttp://code.google.com/chrome/extensions/devguide.html). The popup
> does not have access to any other page but itself, the content script
> only has access to those pages that it takes permissions for in the
> manifest file, and the background code has access to the extension but
> not any other pages e.g. in a browser tab (this is what the content
> script is for).
>
> The only unit of your extension that can access local storage (or
> database) is the background code, so if other units of your extension
> e.g. popup, or a content script that activates when some URL is
> visited, wants to store some data, they need to send a message with
> that data to the background page.
>
> > Basically what I'm trying to do is save/load from localStorage from
> > the popup page (on load and on unload).
> > Could it work by accessing from the popup to the background and save/
> > load into the background one?
>
> Yes, that's how it will work. Send a message from popup to background.
>
> > (so I could just change the code to add: 'var bg =
> > chrome.extension.getBackgroundPage()' , and then do as I'm doing
> > already?)
>
> Nope. You need to use the messaging API.http://code.google.com/chrome/extensions/messaging.html
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to chromium-extensions+unsubscr...@chromium.org.

Pedro Ferreira

unread,
Sep 7, 2010, 3:33:43 PM9/7/10
to Chromium-extensions
Yep, I think I don't need content scripts.

I think I didn't explain correctly above. When you write 'var
background = chrome.extension.getBackgroundPage();' , you're getting
sort of a pointer to the background page, so to use its localStorage I
could: 'background.localStorage.setItem(key, value);'

I have to change my code to read localStorage through the pointer, if
I'm getting it. Got to try a bit now.

Pedro Ferreira

unread,
Sep 7, 2010, 6:17:29 PM9/7/10
to Chromium-extensions
Managed to get the localStorage working on the background page, thanks
for the help!

Just a little doubt, I read that you could access the functions of the
background page from others(like the popup), but how exactly do you do
that?
I tried something like:

var background = chrome.extension.getBackgroundPage();
background.someFunctionName();

but no good :p

Aaron Boodman

unread,
Sep 9, 2010, 3:12:15 AM9/9/10
to Pedro Ferreira, Chromium-extensions
On Tue, Sep 7, 2010 at 3:17 PM, Pedro Ferreira <darki...@gmail.com> wrote:
> var background = chrome.extension.getBackgroundPage();
> background.someFunctionName();

That should work.

Also, everyone, in the case of local storage, there is no need to get
the background page's local storage. getBackgroundPage().localStorage
is equivalent to just localStorage because localStorage is
per-extension.

- a

Pedro Ferreira

unread,
Sep 9, 2010, 9:55:19 AM9/9/10
to Chromium-extensions
yep, it works. I just needed to reload the extension, my bad.

But you still need to have included the background page for
localStorage to work.

On Sep 9, 8:12 am, Aaron Boodman <a...@google.com> wrote:

marek chovanec

unread,
Sep 9, 2010, 10:12:46 AM9/9/10
to Chromium-extensions
no you do not need to include background in popup in order to access
extension's localStorage

as mentioned above localStorage is shared per extension and is
accessible from background, popup, options and browser action page.

it is content script ONLY which has to pass messages to background in
order to access local storage http://code.google.com/chrome/extensions/messaging.html#simple

hope that helps

Akshay Dua

unread,
Sep 9, 2010, 12:12:28 PM9/9/10
to Jesselnz, Chromium-extensions
Thanks for the correction. My apologies on the misinformation.

-- Akshay
http://web.cecs.pdx.edu/~akshay/

PhistucK

unread,
Sep 9, 2010, 2:19:53 PM9/9/10
to marek chovanec, Chromium-extensions
I believe it does not even have to pass messages to the background page, but to any page of that extension that is active at the moment (typically, that would be the background page, but not limited to it)
.
PhistucK



Reply all
Reply to author
Forward
0 new messages