Sending variable from Options Page to Content Script

1,695 views
Skip to first unread message

alexandrojv

unread,
Jul 24, 2010, 1:08:39 AM7/24/10
to Chromium-extensions, nat.a...@gmail.com
I know I can't use localstorage to transfer the variables, but instead
use Message Passing (http://code.google.com/chrome/extensions/
messaging.html) but I am really confused on how to do this

Say my variable is called last_name and I want to pass it from the
options page where the user inputs their last name, and receive the
variable in the content script called name.js

How would I go about doing this?

Ben

unread,
Jul 24, 2010, 1:46:43 AM7/24/10
to alexandrojv, Chromium-extensions, nat.a...@gmail.com

Before any of the below, last name should be set in localStorage from
the options or background page.


In name.js:

chrome.extension.sendRequest({greeting:"last name"},
function(response) {
// Do something with response.last_name here; for instance:
console.log(response.last_name) // Print last_name to the console
}
);


In background page (not options page):

var last_name = localStorage.getItem('last name'); // or whatever

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.greeting == "last name") {
sendResponse({"last_name": last_name});
// First last_name is quoted so that bad things don't happen;
// I would assume this is bad practice.
// Using a different name would probably be better. ;-)
} else {
sendResponse({});
// Important! Always send a response,
// even if the request is unexpected!
}
}
);


I think that's about right. There are other examples floating around in
this discussion group if mine doesn't work for you. And feel free to ask
if you don't understand something I did there.

--
Ben

alexandrojv

unread,
Jul 24, 2010, 1:50:32 AM7/24/10
to Chromium-extensions
oh so I have to have a background page in order to be the intermediate
point right? or is there a way to just go from the options page to the
content script?

Ausmarton Fernandes

unread,
Jul 24, 2010, 8:42:41 AM7/24/10
to Chromium-extensions
I'm pretty new to this, but have you tried using localStorage to store
your data?

Ben

unread,
Jul 24, 2010, 11:08:38 AM7/24/10
to alexandrojv, Chromium-extensions
I'm pretty sure you need a background page. It did work for me at one
point using just the options page but some users said it wasn't working
for them, and I noticed later that it only worked as long as the options
page was open; your mileage may vary. ;-)

--
Ben

Brian Kennish

unread,
Jul 24, 2010, 8:15:53 PM7/24/10
to benj...@gmail.com, alexandrojv, Chromium-extensions
On Sat, Jul 24, 2010 at 8:08 AM, Ben <benj...@gmail.com> wrote:
> I'm pretty sure you need a background page. It did work for me at one
> point using just the options page but some users said it wasn't working
> for them, and I noticed later that it only worked as long as the options
> page was open; your mileage may vary. ;-)

You don't always need a background page to handle options, but you do
in most cases. The reason is -- as Ben hinted at -- when an option
page is closed, it goes out of scope and is no longer capable of
passing messages (or doing anything else).

Christoph Hagenbrock

unread,
Jul 24, 2010, 2:55:30 PM7/24/10
to Chromium-extensions
That's right! Depending on your task maybe a simple scripttag
injection is enough (can be done by the content_script).
This approch has the advantage because it is able to talk to the
current page (call fanctions and so on) But if your code needs to stay
alive ex. after leaving or changeing the side you need to have a
background page.

You can combine the solutions as you want since the background page is
not viewable you can lose nothing if you create one
Maybe it feels a bit complicated but try to handle the hole thing like
a client server communication where the backgroundpage is your server.

Let me know if i am mistaken :)

Teo

unread,
Jul 25, 2010, 10:35:13 AM7/25/10
to Chromium-extensions
What you could do is use this:
http://code.google.com/chrome/extensions/extension.html#method-getBackgroundPage

Through it you can access any variable in a classic OO way:
e.g.
chrome.extension.getBackgroundPage().someVariableInTheBackgroundPage
I believe you can do this with functions too, so you could write a
getter function which returns the desired variable (first reading it
from localStorage, in which you store it from your options page).

Brian Kennish

unread,
Jul 25, 2010, 5:47:45 PM7/25/10
to Teo, Chromium-extensions
On Sun, Jul 25, 2010 at 7:35 AM, Teo <teomi...@gmail.com> wrote:
> What you could do is use this:
> http://code.google.com/chrome/extensions/extension.html#method-getBackgroundPage

Content scripts can't execute "chrome.extension.getBackgroundPage"
(for security reasons, they have limited access to the extension API).

Teo

unread,
Jul 26, 2010, 3:05:09 AM7/26/10
to Chromium-extensions
Ah sorry, didn't know that, it seems i'm using it in options.html
myself...

On Jul 26, 12:47 am, Brian Kennish <bkenn...@chromium.org> wrote:
> On Sun, Jul 25, 2010 at 7:35 AM, Teo <teomina...@gmail.com> wrote:
> > What you could do is use this:
> >http://code.google.com/chrome/extensions/extension.html#method-getBac...

Alexandro Jimenez

unread,
Aug 8, 2010, 6:17:30 PM8/8/10
to Teo, Chromium-extensions
Thank you all for your help, I kept trying to do this, but could never get it to work. But anyways, it was going to be for a Google Wave related extension, but since this: http://googleblog.blogspot.com/2010/08/update-on-google-wave.html I guess there is no point on making it any more.


--
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.


PhistucK

unread,
Aug 9, 2010, 1:42:30 AM8/9/10
to Alexandro Jimenez, Teo, Chromium-extensions
Sad. :(

☆PhistucK

Alexandro Jimenez

unread,
Aug 9, 2010, 2:48:15 PM8/9/10
to PhistucK, Teo, Chromium-extensions
Yeah I know, but just fyi if you want to try to save Google Wave you can vote at: www.SaveGoogleWave.com who knows if it will make an impact, but we hope it does

- Alex

PhistucK

unread,
Aug 9, 2010, 3:37:49 PM8/9/10
to Alexandro Jimenez, Teo, Chromium-extensions
The only thing that will make an impact, is if lots of lots of users start using it everyday.

☆PhistucK

Alexandro Jimenez

unread,
Aug 9, 2010, 4:04:24 PM8/9/10
to PhistucK, Teo, Chromium-extensions
Well that's the thing, unless they don't know it's closing, I doubt people would bother trying to learn how to use it

- Alex

PhistucK

unread,
Aug 9, 2010, 4:25:48 PM8/9/10
to Alexandro Jimenez, Teo, Chromium-extensions
Unfortunately, I believe that even when they know it is going to be shut down, they will not suddenly use it.
Though, it would be really awesome if suddenly (or gradually, until the end of the year, when it really peaks) lots of people start using it, but I cannot get my hopes up.

☆PhistucK

Goodwine

unread,
Aug 13, 2010, 2:48:42 PM8/13/10
to Chromium-extensions
I'm having the same problem, here is a test case:
script.js
------------------------
var x;
chrome.extension.sendRequest({method: "hello"}, function(response)
{x=response.data});
alert (x);

bgpage.html
--------------------------
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.method == "hello" && sender.tab) {
sendResponse({data: localStorage['foo']});
}
else {
sendResponse({}); // snub them.
}
}
);

------------------
alert would show "undefined"
------------------

If what I try to do is wrong, could you please give me some advice or
tell me how to do that?

to "fix" it I did something like:

chrome.extension.sendRequest({'key' : key}, function (value) {
localStorage.setItem(key, value);
});

which works because it is done on body load i'm requesting the keys I
need and save them to localStorage for later use
however there should be another way D:

Ben

unread,
Aug 13, 2010, 3:04:35 PM8/13/10
to Goodwine, Chromium-extensions
On Fri, 2010-08-13 at 11:48 -0700, Goodwine wrote:
> I'm having the same problem, here is a test case:
> script.js
> ------------------------
> var x;
> chrome.extension.sendRequest({method: "hello"}, function(response)
> {x=response.data});
> alert (x);
>
> bgpage.html
> --------------------------
> chrome.extension.onRequest.addListener(
> function(request, sender, sendResponse) {
> if (request.method == "hello" && sender.tab) {
> sendResponse({data: localStorage['foo']});
> }
> else {
> sendResponse({}); // snub them.
> }
> }
> );
>
> ------------------
> alert would show "undefined"
> ------------------

That's because the alert(x) is outside the callback, where 'x' may not
have been set yet. If you put the alert(x) inside the callback, you
should see that x=response.data .

>
> If what I try to do is wrong, could you please give me some advice or
> tell me how to do that?
>
> to "fix" it I did something like:
>
> chrome.extension.sendRequest({'key' : key}, function (value) {
> localStorage.setItem(key, value);
> });
>
> which works because it is done on body load i'm requesting the keys I
> need and save them to localStorage for later use
> however there should be another way D:
>

--
Ben

Goodwine

unread,
Aug 14, 2010, 5:32:55 PM8/14/10
to Chromium-extensions
But I need to use the X outside D:
is there a way to "pause" until the X is assigned?
currently I'm loading it at the very start so the X is assigned before
it is needed
I guess I could use an ugly "while (x == null);" but... that would
look ugly and I guess it will cost extra resources xD

Teo

unread,
Aug 15, 2010, 12:49:56 AM8/15/10
to Goodwine, Chromium-extensions
If i'm getting your problem right, why not use a function?

var x;
chrome.extension.sendRequest({method: "hello"}, function(response)
{x=response.data; xIsAssigned();});

function xIsAssigned(){
  alert (x); //you could even pass it as a parameter instead of having it global
}

--
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.




--
Teo
Site | Blog | Facebook | Twitter
Romania, Europe (GMT +2)

Goodwine Grim

unread,
Aug 15, 2010, 1:59:21 AM8/15/10
to Teo, Chromium-extensions
Thanks for your reply,
I really need the X outside, I'm using an options page to set some localStorage variables, then according to the value, some image will be displayed (like if I store circle, then a circle is shown)
What I did to avoid this race was to call this at the very beggining and then put
chrome.extension.sendRequest({method: "hello"}, function(response)
{sessionStorage.setItem(response);});
and like 200 lines later, I would use them
Idk if that was correct or not xP
Reply all
Reply to author
Forward
0 new messages