browser.runtime.onMessage.addListener(request => {
if (request.type === C.is_development) {
return new Promise(resolve => resolve({ e: "d"}));
}
});
Return a promise instead of sendResponse for new W3C specSender Info
let windowId = sender.tab.windowId; //windowId is instance of chrome, so multiple tabs in the same window have the same windowId
let tabId = sender.tab.id;
Gotchas
Do not add an async listener browser.runtime.addListener(async (req) => ....) as it will consume all the messages blocking other listeners, instead use promises inside ft
Message Passing Between Extensions
Lets you make a "public api" that other chrome extensions can use
Similar interface to regular Messages except need chrome id which you can find here
Send Message
// The ID of the extension we want to talk to.
var laserExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";
// Make a simple request:
chrome.runtime.sendMessage(laserExtensionId, {getTargetData: true},
function(response) {
if (targetInRange(response.targetData))
chrome.runtime.sendMessage(laserExtensionId, {activateLasers: true});
});
if (chrome) {
// Make a simple request:
debug("CE", conf.extension_id);
chrome.runtime.sendMessage(conf.extension_id, { x: 1 }, response => {
debug("M RESPONSE", response);
});
}
Any script in extension
//returning promise is now sendResponse, so async function means responses with anything returned
browser.runtime.onMessageExternal.addListener(async (msg, sender) => {
debug("External message recieved", msg, sender);
debug("From sender", sender.url);
return { x: 1 };
});
Doesn't work in Firefox, instead you could do postmessage/custom events and listen in the content script
Long lived Connections
Background
var port = chrome.runtime.connect({name: "knockknock"});
port.postMessage({joke: "Knock knock"});
port.onMessage.addListener(function(msg) {
if (msg.question == "Who's there?")
port.postMessage({answer: "Madame"});
else if (msg.question == "Madame who?")
port.postMessage({answer: "Madame... Bovary"});
});
content
chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "knockknock");
port.onMessage.addListener(function(msg) {
if (msg.joke == "Knock knock")
port.postMessage({question: "Who's there?"});
else if (msg.answer == "Madame")
port.postMessage({question: "Madame who?"});
else if (msg.answer == "Madame... Bovary")
port.postMessage({question: "I don't get it."});
});
});