Google Extension Messaging


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.