mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 18:19:58 +00:00
Improve extension initialization
This commit is contained in:
@@ -41,7 +41,7 @@ browser.runtime.onInstalled.addListener(async details => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Call after default options have been set
|
// Call after default options have been set
|
||||||
createMenus();
|
init();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@@ -65,17 +65,21 @@ const mediaCastTargetUrlPatterns = new Set([
|
|||||||
|
|
||||||
const LOCAL_MEDIA_URL_PATTERN = "file://*/*";
|
const LOCAL_MEDIA_URL_PATTERN = "file://*/*";
|
||||||
|
|
||||||
async function createMenus () {
|
|
||||||
const opts = await options.getAll();
|
|
||||||
|
|
||||||
/**
|
async function initCreateMenus (opts: Options) {
|
||||||
* If options aren't set or menus have already been
|
|
||||||
* created, return.
|
// If menus have already been created, return.
|
||||||
*/
|
if (mirrorCastMenuId
|
||||||
if (!opts || mirrorCastMenuId || mediaCastMenuId) {
|
|| mediaCastMenuId
|
||||||
|
|| whitelistMenuId
|
||||||
|
|| whitelistRecommendedMenuId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If local media casting is enabled, allow the media cast
|
||||||
|
* menu item to appear on file URIs.
|
||||||
|
*/
|
||||||
if (opts.localMediaEnabled) {
|
if (opts.localMediaEnabled) {
|
||||||
mediaCastTargetUrlPatterns.add(LOCAL_MEDIA_URL_PATTERN);
|
mediaCastTargetUrlPatterns.add(LOCAL_MEDIA_URL_PATTERN);
|
||||||
}
|
}
|
||||||
@@ -328,15 +332,11 @@ async function onBeforeSendHeaders (
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates any extension state based on options changes.
|
* Initializes any functionality based on options state.
|
||||||
*/
|
*/
|
||||||
async function onOptionsUpdated (alteredOptions?: Array<(keyof Options)>) {
|
async function initRegisterOptionalFeatures (
|
||||||
const opts = await options.getAll();
|
opts: Options
|
||||||
|
, alteredOptions?: Array<(keyof Options)>) {
|
||||||
// If options aren't set yet, return
|
|
||||||
if (!opts) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a webRequest listener that intercepts and modifies user
|
* Adds a webRequest listener that intercepts and modifies user
|
||||||
@@ -396,10 +396,11 @@ async function onOptionsUpdated (alteredOptions?: Array<(keyof Options)>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener(message => {
|
browser.runtime.onMessage.addListener(async message => {
|
||||||
switch (message.subject) {
|
switch (message.subject) {
|
||||||
case "optionsUpdated": {
|
case "optionsUpdated": {
|
||||||
onOptionsUpdated(message.data.alteredOptions);
|
const opts = await options.getAll();
|
||||||
|
initRegisterOptionalFeatures(opts, message.data.alteredOptions);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -519,10 +520,13 @@ const statusBridgeReceivers = new Map<string, Receiver>();
|
|||||||
/**
|
/**
|
||||||
* Create status bridge, set event handlers and initialize.
|
* Create status bridge, set event handlers and initialize.
|
||||||
*/
|
*/
|
||||||
async function initStatusBridge () {
|
async function initCreateStatusBridge (opts: Options) {
|
||||||
const applicationName = await options.get("bridgeApplicationName");
|
// Status bridge already initialized
|
||||||
|
if (statusBridge) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
statusBridge = nativeMessaging.connectNative(applicationName);
|
statusBridge = nativeMessaging.connectNative(opts.bridgeApplicationName);
|
||||||
statusBridge.onDisconnect.addListener(onStatusBridgeDisconnect);
|
statusBridge.onDisconnect.addListener(onStatusBridgeDisconnect);
|
||||||
statusBridge.onMessage.addListener(onStatusBridgeMessage);
|
statusBridge.onMessage.addListener(onStatusBridgeMessage);
|
||||||
|
|
||||||
@@ -534,8 +538,6 @@ async function initStatusBridge () {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
initStatusBridge();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs once the status bridge has disconnected. Sends
|
* Runs once the status bridge has disconnected. Sends
|
||||||
* serviceDown messages for all receivers to all shims to
|
* serviceDown messages for all receivers to all shims to
|
||||||
@@ -565,8 +567,9 @@ function onStatusBridgeDisconnect () {
|
|||||||
statusBridge = null;
|
statusBridge = null;
|
||||||
|
|
||||||
// After 10 seconds, attempt to reinitialize
|
// After 10 seconds, attempt to reinitialize
|
||||||
window.setTimeout(() => {
|
window.setTimeout(async () => {
|
||||||
initStatusBridge();
|
const opts = await options.getAll();
|
||||||
|
initCreateStatusBridge(opts);
|
||||||
}, 10000);
|
}, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -807,5 +810,15 @@ browser.runtime.onMessage.addListener((message, sender) => {
|
|||||||
|
|
||||||
|
|
||||||
// Misc init
|
// Misc init
|
||||||
createMenus();
|
async function init () {
|
||||||
onOptionsUpdated();
|
const opts = await options.getAll();
|
||||||
|
if (!opts) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
initCreateMenus(opts);
|
||||||
|
initRegisterOptionalFeatures(opts);
|
||||||
|
initCreateStatusBridge(opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
init();
|
||||||
|
|||||||
Reference in New Issue
Block a user