Skip to content

Instantly share code, notes, and snippets.

@0xjjpa
Created December 26, 2012 19:15
Show Gist options
  • Save 0xjjpa/4382370 to your computer and use it in GitHub Desktop.
Save 0xjjpa/4382370 to your computer and use it in GitHub Desktop.
Basic communication between a Background Page and a Content Script inside a Chrome Extension.
// In Background.js
chrome.tabs.sendMessage(tab.id, {content: "message"}, function(response) {
if(response) {
//We do something
}
});
// In ContentScript.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if(request.content) {
sendResponse({content: "response message"});
return true; // This is required by a Chrome Extension
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment