Skip to content

Instantly share code, notes, and snippets.

@geekwolverine
Forked from 0xjjpa/communication.js
Created April 14, 2026 14:41
Show Gist options
  • Select an option

  • Save geekwolverine/a62fc741d29400def3b19b439c2fb221 to your computer and use it in GitHub Desktop.

Select an option

Save geekwolverine/a62fc741d29400def3b19b439c2fb221 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