Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since the variable you want is in the background page, and the popup is only loaded when you open it, you could use <a href="http://developer.chrome.com/extensions/messaging.html" rel="nofollow">message passing</a> to do this. In this case it would be best to send a message from the popup to the background page like this:</p> <p><strong>popup.js</strong></p> <pre><code>chrome.runtime.sendMessage({method:"getWord"},function(response){ //here response will be the word you want console.log(response); }); </code></pre> <p><strong>background.js</strong></p> <pre><code>chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){ 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 sendResponse(word); // 2. It needs to be retrieved asynchronously, in that case we do this getWord(sendResponse); 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 } }); </code></pre> <p>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.</p> <p><strong>manifest.json</strong></p> <pre><code>"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"] }], </code></pre> <p>I removed the duplicate permission, and you were trying to inject a css file in the <code>js</code> section.</p> <p><strong>background.js</strong></p> <pre><code>chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { if(msg.check) word = msg.check; if(msg.method == "getWord") sendResponse(word); }); </code></pre> <p><strong>script.js</strong></p> <pre><code>//get the word you want to send chrome.runtime.sendMessage({check: word}); </code></pre> <p>You had multiple <code>onMessage</code> listeners, I combined them. </p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload