Created
February 19, 2017 06:10
-
-
Save samjarman/a39e344539a521b428317ff5d2f9cf25 to your computer and use it in GitHub Desktop.
Example of basic JS messaging for Chrome extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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