Skip to content

Instantly share code, notes, and snippets.

@johnschimmel
Last active October 18, 2018 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnschimmel/fad562a4352e93353a91 to your computer and use it in GitHub Desktop.
Save johnschimmel/fad562a4352e93353a91 to your computer and use it in GitHub Desktop.
Chrome App to Webpage communication
// be sure to update manifest to allow external connections,
// https://developer.chrome.com/extensions/messaging#external-webpage
/*
"externally_connectable" : {
"matches": ["*://diyability-capacita.appspot.com/*"]
}
*/
// Listen for incoming long lasting PORT connection from Webpage or extension
chrome.runtime.onConnectExternal.addListener(function(port) {
console.log("PORT!", port);
// say hello every second once connection is made
setInterval(function() {
port.postMessage("hi from chrome app");
console.log("trying to say hi")
}, 1000);
port.onMessage.addListener(function(msg) {
// See other examples for sample onMessage handlers.
console.log("msg received: ", msg);
console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
});
});
// put this in your webpage js
var extId = "gdbjlnnacemphcbgjhlcjjjcnijafpgi"; // chrome app id
var port = chrome.runtime.connect(extId);
port.postMessage("hi from webpage");
port.onMessage.addListener(function(msg) {
// See other examples for sample onMessage handlers.
console.log("web page received: ", msg);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment