Message passing from Background script to Content script behaving weidr

745 views
Skip to first unread message

Piyush Khemka

unread,
Dec 15, 2015, 2:55:58 PM12/15/15
to Chromium-Extensions-Announce

I am trying to implement message passing from background script to content script

manifest.json


"background": {
       
"scripts": ["background.js"],
       
"persistent": false
   
},


   
"content_scripts" : [
   
{
       
"matches" : [ "<all_urls>" ],
       
"js": ["hello.js"],
       
"css": [ "hello.css" ]
   
}



background.js



   
function showpopup()
{


  chrome
.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome
.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console
.log(response.farewell);
     
});
   
});
}


chrome
.alarms.onAlarm.addListener(showpopup);



content script js aka hello.js



chrome
.runtime.onMessage.addListener(
 
function(request, sender, sendResponse) {
    console
.log(sender.tab ?
               
"from a content script:" + sender.tab.url :
               
"from the extension");
   
if (request.greeting == "hello") {

      alert
("Hello World");
      sendResponse
({farewell: "goodbye"});
}
 
});





I am getting the error Error in event handler for (unknown): TypeError: Cannot read property 'farewell' of undefined

 I am setting an alarm in the background page using a chrome.alarms.create command. I noticed something interesting. Some pages are throwing a popup while others aren't. 
Seems like I am able to get a popup on the page if I refresh a particular tab.(I have around 10 open) On opening or refreshing a new tab, I am getting a popup whereas an old tab doesn't show a popup. Does anyone know how to correct this? 

Also is there a way to match chrome:// and newtab pages as well in the manifest? I tried including chrome:// but that didn't work as intended.

PhistucK

unread,
Dec 15, 2015, 3:53:14 PM12/15/15
to Piyush Khemka, Chromium-Extensions-Announce
If you change -

 chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console.log(response.farewell);
To -

 chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
        console.log(arguments);

Does it print anything interesting?


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/43bbf7e6-6b46-4062-8730-54dc37e11680%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Piyush Khemka

unread,
Dec 16, 2015, 3:11:38 PM12/16/15
to Chromium-Extensions-Announce, piyushk...@gmail.com
Prints

[ ] for old tabs
[object] for newly opened tabs or refreshed tabs


PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
Reply all
Reply to author
Forward
0 new messages