Skip to content

Instantly share code, notes, and snippets.

@atmd83
Last active October 31, 2022 14:11
Show Gist options
  • Save atmd83/ebbe0f9820ac9521e1fd to your computer and use it in GitHub Desktop.
Save atmd83/ebbe0f9820ac9521e1fd to your computer and use it in GitHub Desktop.
Demo for cross-extension messaging in chrome extensions
chrome.runtime.onMessageExternal.addListener(function(request, sender, sendResponse) {
console.log('incomming');
if (request.getTargetData){
console.log('We have target data');
sendResponse({targetData: {}});
} else {
if (request.activateLasers) {
var success = true;
console.log('lazers active');
sendResponse({activateLasers: success});
}
}
});
var id = "[ID FOR APP ONE]";
console.log('target data sent');
chrome.runtime.sendMessage(id, {getTargetData: true}, function(response) {
if (response.targetData)
console.log('activate the weapons');
chrome.runtime.sendMessage(id, {activateLasers: true});
});
{
"manifest_version": 2,
"name": "comunication app",
"description": "Testing chrome cross extension comunication",
"version": "2.0.20",
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html",
"default_title": "Click here!"
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/",
"tabs",
"http://*/*", "https://*/*", "background"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment