0

I wanna send a variable from background.js to popup.js using message passing

background.js file:

chrome.tabs.query({
active: true,lastFocusedWindow: true
    }, function(array_of_Tabs) {
    var tab = array_of_Tabs[0];
    var url = tab.url;
    chrome.runtime.sendMessage({url},function(response){
      });
});

popup.js file:

   chrome.runtime.onMessage.addListener(function(message,sender,sendResponse)    {
        document.write(message);
        console.log(message);
   });

I have given necessary permissions in manifest file.After executing this it doesn't show any result. Please help.

  • 1
    1. Rightclick the popup and choose Inspect to see its console - [Google Chrome / Firefox do not see extension output in console](//stackoverflow.com/a/38920982) 2. You can use chrome.tabs in your browserAction popup, no messaging needed. 3. document.write won't work because the document is not opened, don't use it, it's bad anyway. – wOxxOm Feb 21 '17 at 12:03
  • Also, `{url}` isn't valid syntax in your sendMessage. – Teepeemm Feb 21 '17 at 14:59
  • @Teepeemm, it's ES2015 [shorthand property](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer) syntax. – wOxxOm Feb 21 '17 at 17:29

0 Answers0