[crx] Getting a variable from popup.html, to content script.

4,802 views
Skip to first unread message

bryan

unread,
May 10, 2010, 1:51:08 PM5/10/10
to Chromium-extensions
I am working on an extension, where the user inputs something into a
field in the popup.html.
I want to store this with localStorage and then use the value in the
content script.
I know I can't directly communicate between the content_script and
background.html, and the documentation on code.google isn't clear ( at
least to me).
Any insight on how to pass a variable from the background.html to the
content.js?
I tried setting up a variable myVar = 'test'; and passing it with a
sendRequest but I'm not very clear on how to actually write this.
Any help would be much appreciated!

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

Mohamed Mansour

unread,
May 10, 2010, 6:46:10 PM5/10/10
to bryan, Chromium-extensions
Hi Bryan,

To pass a variable to the content script (and vice versa), you would need to use Message Passing:

There are numerous examples on that page (and on this group) about that.

-
Mohamed Mansour
m...@chromium.org

TwisterMc

unread,
May 11, 2010, 6:42:56 AM5/11/10
to Chromium-extensions
I'm having issues with this too. Can someone please provide an example
that can obtain the tab URL and place it in a variable that I can use
throughout my extension?

I looked at the examples and was able to change the text in a DIV
based on ID, but I'm not sure how to get a variable that I can re-use
over and over again in my HTML.

Thanks
> > 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<chromium-extensions%2Bunsubscr...@chromium.org>
> > .
> > For more options, visit this group at
> >http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
> 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.
> For more options, visit this group athttp://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

Derek 囧

unread,
May 11, 2010, 7:30:19 PM5/11/10
to Chromium-extensions
Just enter something like this:

//popup
var txt="TEXT"
chrome.extension.getBackgroundPage().chrome.tabs.getSelected(null,
function(tab) {
chrome.tabs.sendRequest(tab.id, {greeting: txt}, function(response)
{});
});

//content script
var recetxt=""
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
recetxt=request.greeting
});
alert(recetxt)
Reply all
Reply to author
Forward
0 new messages