Disable browser action unless there are available receivers

This commit is contained in:
hensm
2019-07-29 08:59:31 +01:00
parent 8c9ac7b1d5
commit e8ef099370

View File

@@ -21,6 +21,7 @@ import ReceiverSelectorManager
import createMenus from "./background/createMenus";
import ShimManager from "./background/ShimManager";
import StatusManager from "./background/StatusManager";
const _ = browser.i18n.getMessage;
@@ -48,6 +49,39 @@ browser.runtime.onInstalled.addListener(async details => {
function initBrowserAction () {
browser.browserAction.disable();
function onServiceChange () {
if (StatusManager.getReceivers().length) {
browser.browserAction.enable();
} else {
browser.browserAction.disable();
}
}
StatusManager.addEventListener("serviceUp", onServiceChange);
StatusManager.addEventListener("serviceDown", onServiceChange);
/**
* When the browser action is clicked, open a receiver
* selector and load a sender for the response. The
* mirroring sender is loaded into the current tab at the
* top-level frame.
*/
browser.browserAction.onClicked.addListener(async tab => {
const selection = await ReceiverSelectorManager.getSelection();
loadSender({
tabId: tab.id
, frameId: 0
, selection
});
});
}
async function initMenus () {
console.info("fx_cast (Debug): init (menus)");
@@ -265,9 +299,13 @@ async function init () {
await initRequestListener();
await initWhitelist();
await StatusManager.init();
await ShimManager.init();
await initBrowserAction();
/**
* When a message port connection with the name "shim" is
* established, pass it to createShim to handle the setup
@@ -278,22 +316,6 @@ async function init () {
ShimManager.createShim(port);
}
});
/**
* When the browser action is clicked, open a receiver
* selector and load a sender for the response. The
* mirroring sender is loaded into the current tab at the
* top-level frame.
*/
browser.browserAction.onClicked.addListener(async tab => {
const selection = await ReceiverSelectorManager.getSelection();
loadSender({
tabId: tab.id
, frameId: 0
, selection
});
});
}
init();