4

I have read and re-read this page, as well as run the samples:

http://code.google.com/chrome/extensions/background_pages.html

But I don't seem to grasp how to do the background communication between the background.html, popup.html, and content.js. I want to send messages to trigger functions, get responses, and process those responses. The map sample was sort of close to helping me, but I just need something super simple and not need all that map stuff. (Note, I know jQuery as well as Javascript, so feel free to mix in a bit of jQuery if you want.)

Volomike
  • 23,743
  • 21
  • 113
  • 209
  • Related: [Contexts and methods for communication between the browser action, background scripts, and content scripts of chrome extensions?](http://stackoverflow.com/questions/17246133/contexts-and-methods-for-communication-between-the-browser-action-background-sc) – Rob W Jan 13 '14 at 21:40

1 Answers1

18

All extension pages (background page, popup, infobar, page action all run inside the same extension. Think of it as a webpage with one domain. And that domain is your extension ID. Each one of those extension pages is like a regular page (similar when you develop a website).

All extension pages (mentioned above) can communicate with each other easily, you have multiple ways doing so:

  1. chrome.extension.getBackgroundPage()

    You do it directly! I use this approach whenever I can. Its cleaner in my opinion.

    var bkg = chrome.extension.getBackgroundPage();`  
    bkg.ping();`
    
  2. chrome.extension.onRequest.addListener and chrome.extension.sendRequest

    As shown below, you can use extension messaging to pass information as well. I use this approach when I want it to be event oriented. I rarely use this within extension pages.

    popup.html

    chrome.extension.sendRequest({method: 'ping'}, function(response) {
       // response.result
    });
    

    background_page.html

    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
        if (request.method == 'ping') {
            sendResponse({result: 'pong'});
        }
    });
    

Now there is a difference between "Extension Pages" and "Content Scripts". Please read that document carefully to understand it. A content script is not a extension page, you cannot do what extension pages do. You cannot directly communicate to any of those pages mentioned above. Content Scripts are JavaScript files that run in the context of web pages, not extension pages. Thats an important distinction to recognize.

So in order to communicate between your extension pages and content script, you need to use Messaging. That page has a lot of information, I highly recommend you to read it. It is very similar to how we used messaging (step 2 above), but the only different is how you send a request. You would need to use chrome.tabs.sendRequest, because you need to send a single request to the content script from your extension page (background, popup, page, etc). You would need to know the ID of your tab in order to do that. Please look at the Tab API for more info.

If your extension communicates with your content script very often, you can use Long Lived connections, which is explained pretty well in the Messaging section I liked above.

I have answered many questions and other people too regarding similar questions. Since your experienced in JavaScript, I highly recommend you to read the documentation, it has everything you need. Read the API, and I hope I you now understand the difference between Content Script and Extension Pages, and the communication between them is through Extension Messaging.

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90
  • Something also to note if one's using jQuery as well on this. You'll need to load jQuery in the background.html or you'll get nowhere. Also, I found I didn't have to do chrome.tabs.sendRequest from my content script request of the background.html -- I simply used #2 above and it worked. – Volomike Jan 20 '11 at 09:23
  • There's also some spooky stuff to notice, and it might have been that my Chrome updated in the background or something with a new version. All yesterday, I was able to do an AJAX call to my localhost without getting any errors via content.js. Today -- it now has a cross-domain error, forcing me to use the background method. A really useful piece of code is here: http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/howto/contentscript_xhr/ – Volomike Jan 20 '11 at 09:27
  • You cannot use chrome.tabs.sendRequest nor getBackgroundPage from the content script, you have to use #2 (messaging) to setup your listener in your background page, and pass in the messages through chrome.extension.sendRequest. If you need more help, let me know :) – Mohamed Mansour Jan 21 '11 at 05:00
  • @MohamedMansour How to call a function of background page, Content script function, popup.html function from Google chrome extension's infobar. – Exception Dec 28 '11 at 15:10