Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

workaround to access local storage from content_scripts

35 views
Skip to first unread message

quaz...@gmail.com

unread,
Jun 17, 2016, 8:05:30 PM6/17/16
to
I can't seem to access local storage from webextension. I don't get an error but the result is always empty.
Probably due to bug https://bugzilla.mozilla.org/show_bug.cgi?id=1197346

Someone in the bug conversation mentioned a workaround using messaging but I can't figure it out. I need an example of how to pull objects like values and arrays out of local storage from a content script via a background script using messaging, unless there is a better way.

quaz...@gmail.com

unread,
Jun 17, 2016, 8:41:41 PM6/17/16
to
oh and by the way ... in case anybody reading this can fix ... this link ...
To learn much more about this, see the guide to messaging.
... on this page ...
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#Communicating_with_background_scripts
... is broken

Dave Royal

unread,
Jun 18, 2016, 2:49:42 AM6/18/16
to
quaz...@gmail.com Wrote in message:
> I can't seem to access local storage from webextension. I don't get an error but the result is always empty.
> Probably due to bug https://bugzilla.mozilla.org/show_bug.cgi?id=1197346
>
That bug is reported to give an error. And it's reported fixed in
48, so not long to wait.
Are you using the same type of 'local storage'
as that bug?
There's the new webextensions storage.local and the old
localStorage, which does work in content scripts.

(Do these access the same data, BTW?)
--
(Remove any numerics from my email address.)

quaz...@gmail.com

unread,
Jun 18, 2016, 11:46:30 PM6/18/16
to
I am using version 48 ... chrome.storage.local.get ... how do I use the new one? I'm using the ones in the webextension documentation.

I only have one set of data ... so I need to access it from options script, background script, and content script.


Dave Royal

unread,
Jun 19, 2016, 2:21:53 AM6/19/16
to
quaz...@gmail.com Wrote in message:
> I am using version 48 ... chrome.storage.local.get ... how do I use the new one? I'm using the ones in the webextension documentation.
>
> I only have one set of data ... so I need to access it from options script, background script, and content script.
>
Comment #14
in that bug suggests there might be another bug for
use in options.

quaz...@gmail.com

unread,
Jun 19, 2016, 10:30:09 AM6/19/16
to
The storage is working in options. Is there a separate storage for options and a separate storage for background and a separate storage for content_scripts?

Dave Royal

unread,
Jun 19, 2016, 12:32:10 PM6/19/16
to
I have a Google Chrome extension which does chrome.storage.local.set in
options.js and then gets it in a content script. The get is async so I
had to put most of the subsequent code into the callback function. If you
don't do that I guess the option might be empty.

I haven't implemented this in Fx yet - I want Android support - so I
don't know if Fx would be the same, but I expect so.

quaz...@gmail.com

unread,
Jun 19, 2016, 11:45:11 PM6/19/16
to

> I have a Google Chrome extension which does chrome.storage.local.set in
> options.js and then gets it in a content script. The get is async so I
> had to put most of the subsequent code into the callback function. If you
> don't do that I guess the option might be empty.

That's exactly what I need to do. I have confirmed the data I need is stored properly, I just can't seem to retrieve it from content_script. Would you care to share that part of your code?

Dave Royal

unread,
Jun 20, 2016, 3:07:02 AM6/20/16
to
chrome.storage.local.get('MCnewPostScroll',function(items) {
newPostScroll = items['MCnewPostScroll'];
code that uses newPostScroll ;
});

Unlike my Fx (SDK) version of this the whole content script is within the
callback function.

quaz...@gmail.com

unread,
Jun 20, 2016, 9:40:53 AM6/20/16
to
> chrome.storage.local.get('MCnewPostScroll',function(items) {
> newPostScroll = items['MCnewPostScroll'];
> code that uses newPostScroll ;
> });

Wow! That worked! Thanks!
0 new messages