Posted By

the_vinci on 05/05/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

icecreamboyy


Google Extension Messaging


 / Published in: JavaScript
 

  1. // contentscript.js - get inserted into the browser page
  2. chrome.extension.sendRequest({greeting: "hello"}, function(response) {
  3. console.log(response.farewell);
  4. });
  5.  
  6. // background.html - opens a listener to listen for requests
  7. chrome.extension.onRequest.addListener(
  8. function(request, sender, sendResponse) {
  9. console.log(sender.tab ?
  10. "from a content script:" + sender.tab.url :
  11. "from the extension");
  12. if (request.greeting == "hello")
  13. sendResponse({farewell: "goodbye"});
  14. else
  15. sendResponse({}); // snub them.
  16. });

Report this snippet  

You need to login to post a comment.