Skip to content

Instantly share code, notes, and snippets.

@samjarman
Created February 19, 2017 06:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samjarman/a39e344539a521b428317ff5d2f9cf25 to your computer and use it in GitHub Desktop.
Save samjarman/a39e344539a521b428317ff5d2f9cf25 to your computer and use it in GitHub Desktop.
Example of basic JS messaging for Chrome extensions
// In the extension JS
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { // Fetch the current tab
chrome.tabs.sendMessage(tabs[0].id, {message: "preach", preachText: usrMessage});
});
// In the context script
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.message == "preach"){ // Filter out other messages
alert(request.preachText, 5000);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment