Take the 2-minute tour ×

I want to send a variable "word" from background.js to popup.js where it will be processed in a function. The problem is i cannot send that variable and start the popup.js in any way.

I tried:

chrome.runtime.sendMessage({valoare: word},function atribut(word){}); with and without the function attached.

And from what i heard i can access it without a request straight from popup.js but nothing is working under popup.js.

var selection = chrome.extension.getBackgroundPage().word;

I understand that popup.js fires only when i click the page action but nothing happens then.


<script src='jquery-1.9.1.min.js'></script>
<script src='jquery-ui.js'></script>
<script type="text/javascript" src="jquery.googleSuggest.js">
<link class="jsbin" href="jquery-ui.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="suggest.min.css" />
<script src="popup.js"></script>
<link rel="stylesheet" href="mystyle.css" type="text/css" />
<meta charset=utf-8 />
<div id="pop">
     <label for="term">Suggested term: </label>
     <input id="term" style="display: none;"type="text" value=""/>
share|improve this question
You probably need to use chrome.extension.getViews({type: 'popup'}) from background.js. –  方 觉 Apr 12 '13 at 11:50
I used that with no success or at least i don't know what form does it returns if i have something like : var popups = chrome.extension.getViews({type: "popup"}); if (popups.length != 0) { var popup = popups[0]; popup.atribut(word); }; –  Edeph Apr 12 '13 at 12:12

1 Answer 1

up vote 1 down vote accepted

Since the variable you want is in the background page, and the popup is only loaded when you open it, you could use message passing to do this. In this case it would be best to send a message from the popup to the background page like this:


  //here response will be the word you want


  if(message.method == "getWord"){
    //depending on how the word is stored you can do this in one of several ways
    // 1. If it is a global variable, we can just return it directly
    // 2. It needs to be retrieved asynchronously, in that case we do this
    return true;
    // This passes the ability to reply to the function where we get the info
    // Once we have the info we can just use sendResponse(word); like before

Edit: Okay so I took your code and edited some of it, but the initial "Works!" popup worked without any changes. These will just help prevent problems in the future.


"permissions": [
  "tabs", "https://twitter.com/*"
"content_scripts": [{
  "matches": ["https://twitter.com/*"],
  "js": ["jquery-1.9.1.min.js", "myscript.js"],
  "css": ["mystyle.css" , "jquery-ui.css", "suggest.min.css"]

I removed the duplicate permission, and you were trying to inject a css file in the js section.


chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    word = msg.check;
  if(msg.method == "getWord")


//get the word you want to send
chrome.runtime.sendMessage({check: word});

You had multiple onMessage listeners, I combined them.

With these changes, when clicking the page action, the first popup pops up followed by an empty one as word is never set to anything.

share|improve this answer
Thank you for looking over my code, i corrected the bad parts and it still does nothing... I don't understand what is the issue, popup.js never fires. My alert from the background.js actually works very well :/ Is this weird? –  Edeph Apr 16 '13 at 20:55
chrome.runtime.sendMessage({check: word},function resp(word){}); This is the code i use from myscript.js (the contentscript) and i send the word to the background.js with it. I think it is badly written given the function resp(word){} in it. How may i use it properly? –  Edeph Apr 16 '13 at 20:59
What can i do about that function resp ? –  Edeph Apr 16 '13 at 21:31
@Edeph Then you don't need it at all, see the updated code. Note that the response callback is optional. –  BeardFist Apr 16 '13 at 21:59
@Edeph while it is possible to send messages to the popup, it can only receive them while it is open, it is best to store the stuff in the background page as a go between –  BeardFist Apr 16 '13 at 22:02

Your Answer


