webextension-polyfill/test/fixtures/runtime-messaging-extension/background.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

const {name} = browser.runtime.getManifest();
console.log(name, "background page loaded");
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
console.log(name, "background received msg", {msg, sender});
switch (msg) {
case "test - sendMessage with returned Promise reply":
try {
browser.pageAction.show(sender.tab.id);
} catch (err) {
return Promise.resolve(`Unexpected error on pageAction.show: ${err}`);
}
return Promise.resolve("bg page reply 1");
case "test - sendMessage with returned value reply":
// This is supposed to be ignored and the sender should receive
// a reply from the second listener.
return "Unexpected behavior: a plain return value should not be sent as a result";
case "test - sendMessage with synchronous sendResponse":
sendResponse("bg page reply 3");
return "value returned after calling sendResponse synchrously";
case "test - sendMessage with asynchronous sendResponse":
setTimeout(() => sendResponse("bg page reply 4"), 50);
return true;
case "test - second listener if the first does not reply":
// This is supposed to be ignored and the sender should receive
// a reply from the second listener.
return false;
default:
return Promise.resolve(
`Unxpected message received by the background page: ${JSON.stringify(msg)}\n`);
}
});
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
setTimeout(() => {
sendResponse("second listener reply");
}, 100);
return true;
});
console.log(name, "background page ready to receive a content script message...");