Add additional documentation to background script

This commit is contained in:
hensm
2021-04-25 01:49:41 +01:00
parent c1b6c6734e
commit 16c2c797d3

View File

@@ -20,20 +20,24 @@ import { initWhitelist } from "./whitelist";
const _ = browser.i18n.getMessage; const _ = browser.i18n.getMessage;
/**
* On install, set the default options before initializing the
* extension. On update, handle any unset values and set to
* the new defaults.
*/
browser.runtime.onInstalled.addListener(async details => { browser.runtime.onInstalled.addListener(async details => {
switch (details.reason) { switch (details.reason) {
// Set default options
case "install": { case "install": {
// Set defaults
await options.setAll(defaultOptions); await options.setAll(defaultOptions);
// Call after default options have been set // Extension initialization
init(); init();
break; break;
} }
// Set newly added options
case "update": { case "update": {
// Set new defaults
await options.update(defaultOptions); await options.update(defaultOptions);
break; break;
} }
@@ -41,33 +45,10 @@ browser.runtime.onInstalled.addListener(async details => {
}); });
async function initBrowserAction () { /**
logger.info("init (browser action)"); * Sets up media overlay content script and handles toggling
* on options change.
/** */
* 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 => {
if (tab.id === undefined) {
throw logger.error("Tab ID not found in browser action handler.");
}
const selection = await ReceiverSelectorManager.getSelection(tab.id);
if (selection) {
loadSender({
tabId: tab.id
, frameId: 0
, selection
});
}
});
}
async function initMediaOverlay () { async function initMediaOverlay () {
logger.info("init (media overlay)"); logger.info("init (media overlay)");
@@ -97,6 +78,7 @@ async function initMediaOverlay () {
registerMediaOverlayContentScript(); registerMediaOverlayContentScript();
// Update if toggled
options.addEventListener("changed", async ev => { options.addEventListener("changed", async ev => {
const alteredOpts = ev.detail; const alteredOpts = ev.detail;
@@ -108,7 +90,12 @@ async function initMediaOverlay () {
} }
async function checkBridgeCompat () { /**
* Checks whether the bridge can be reached and is compatible
* with the current version of the extension. If not, triggers
* a notification with the appropriate info.
*/
async function notifyBridgeCompat () {
logger.info("checking for bridge..."); logger.info("checking for bridge...");
let info: BridgeInfo; let info: BridgeInfo;
@@ -166,7 +153,7 @@ async function init () {
logger.info("init"); logger.info("init");
isInitialized = true; isInitialized = true;
await checkBridgeCompat(); await notifyBridgeCompat();
await StatusManager.init(); await StatusManager.init();
await ShimManager.init(); await ShimManager.init();
@@ -174,7 +161,29 @@ async function init () {
await initMenus(); await initMenus();
await initWhitelist(); await initWhitelist();
await initMediaOverlay(); await initMediaOverlay();
await initBrowserAction();
/**
* 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 => {
if (tab.id === undefined) {
throw logger.error("Tab ID not found in browser action handler.");
}
const selection = await ReceiverSelectorManager.getSelection(tab.id);
if (selection) {
loadSender({
tabId: tab.id
, frameId: 0
, selection
});
}
});
/** /**