1

I would like to pass a saved varliable from the content script to the popup page, so I would be able to simply output it. For example, if my content script has the following code:

var x = 'abc'; 

I would like the popup title to be this varliable.

2
  • Popup's aren't always open, the background page on the contrary is always available. What exactly do you mean with 'popup title'? Commented Mar 21, 2011 at 12:41
  • 1
    It doesn't have to be it's title. It can be any type of text... I just want to input this varlible as a string inside the popup. Commented Mar 21, 2011 at 12:55

1 Answer 1

2

Couple ways to do this, I used the first one in my extension:

  1. localStorage. Use sendRequest({}) to pass a message with your variable to the background page, which will then save the variable to localStorage. Once that's done, your popup can access the variable like localStorage["x"]. (It has to go through the background page because at this time, content scripts cannot access localStorage)

  2. Plain requests. Pray that the popup is open, and try sending it a message from the content script with the variable in it. Note that this is not a good solution simply because the popup might not be open when you try to send it the variable.

Code Example for #1:

//sendRequests look like this: sendRequest(message - Object, [callback - Function]); //API Docs: //onRequest Listener: http://code.google.com/chrome/extensions/extension.html#event-onRequest //sendRequest Method: http://code.google.com/chrome/extensions/extension.html#method-sendRequest //localStorage: http://www.html5rocks.com/features/storage //From the Content Script //Send request to background.html, no callback chrome.extension.sendRequest({ type: "popup_var", /* In my extensions, because I could often be different types of reqeusts, I use a type variable to identify them */ my_variable: "Sally sold seashells by the seashore" /* Whatever variable you are trying to send */ }); //In background.html chrome.extension.onRequest.addListener( function(request, sender, sendResponse){ if(request.type == "popup_var"){ /* The type of message has been identified as the variable for our popup, let's save it to localStorage */ localStorage["popup_var"] = request.my_variable; } } ); //In popup.html console.log( "Variable from Content Script: "+localStorage["popup_var"] ); 
Sign up to request clarification or add additional context in comments.

7 Comments

Can you please show me a code example? I'm not sure how to use the sendRequest(). By the way - Isn't it possible to access the sendRequest directly from the popup?
Sure, hold on while I edit the answer. And yes, that is what I meant by "Plain requests".
Thanks. Offtopic: You are really helping me process :)
No problem ;) I am here to help.
Hey, I still need some help - localStorage is messing up. Is there any way I can speak with you in a more fluent way? (Chat?)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.