2

i am trying my hands at developing chrome extensions. i am trying to send a message to a content script from the background script. i have tried the following code:

manifest

{
    ....
    "background": {
        "scripts": ["background.js"]
    },
    "content_scripts": [
        {
            "matches": ["http://myurl.com", "http://myurl.com"],
            "js": ["content.js"],
            "run_at": "document_end"
        }
    ]
....
}

background script:

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

content script:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log("From Background Script: " + console.greeting);
    return true;
  });

i checked the console. the "inside" and "Response" are getting logged, but not the "From Background Script..."

what can be the problem?

Gaurav Sood
  • 680
  • 4
  • 17
  • 38
  • try console.log("From Background Script: " + request.greeting); –  Jul 23 '14 at 05:54
  • i tried that. not working. i checked both the Javascript console and the "Inspect Views" console from the Extensions page – Gaurav Sood Jul 23 '14 at 05:58
  • this should be logged on page where content script is running... when do you fire your chrome.tabs.query? –  Jul 23 '14 at 06:08
  • i want to send the message when the content script loads. tried enclosing the code for sending message inside window.onload = function(){} but it did not work – Gaurav Sood Jul 23 '14 at 07:16
  • 1
    I've seen this question countless times.. Where did you get that code snippet? The problem is that when the background page is loaded, there is usually no page where your extension has a content script (either because there is no open window, or because the tab is `chrome://extensions/`). – Rob W Jul 23 '14 at 07:18
  • i wrote the code myself and referred the chrome extension documentation for sending and receiving messages. the extension is actually trying to determine the url of the current page. according to the manifest, if the tab opens with http://myurl.com, the content script should run. i want to print the current url in it. i read that the way to do that would be to write a background script for accessing the tabs permission and then sending that as a message to the current script – Gaurav Sood Jul 23 '14 at 07:28
  • i tried console.log(url) in the background script and it is "chrome://extensions/" – Gaurav Sood Jul 23 '14 at 08:02
  • I have a _very_ detailed answer [here](http://stackoverflow.com/questions/23895377/sending-message-from-a-background-script-to-a-content-script-then-to-a-injected/23895822#23895822) regarding the problem @RobW is explaining. Maybe this should be closed as duplicate. – Xan Jul 23 '14 at 12:46

1 Answers1

0

your chrome.tabs.query is executed on very session start, and sending message in that moment...you need to wrap it in some event like

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (tab.url == "http://www.myurl.com") {
        chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) {
            console.log("Response");
        });
    } 
}); 

and actually you don't need tabs.querry...you got ID from chrome.tabs.onUpdated listener to send msg