mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 01:59:58 +00:00
Enable strict mode for extension build
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import bridge from "../lib/bridge";
|
||||
import loadSender from "../lib/loadSender";
|
||||
import logger from "../lib/logger";
|
||||
import options from "../lib/options";
|
||||
|
||||
import { Message } from "../types";
|
||||
@@ -87,6 +88,11 @@ export default new class ShimManager {
|
||||
private async createShimFromContent (
|
||||
contentPort: browser.runtime.Port): Promise<Shim> {
|
||||
|
||||
if (contentPort.sender?.tab?.id === undefined
|
||||
|| contentPort.sender?.frameId === undefined) {
|
||||
throw logger.error("Content shim created with an invalid port context.");
|
||||
}
|
||||
|
||||
/**
|
||||
* If there's already an active shim for the sender
|
||||
* tab/frame ID, disconnect it.
|
||||
@@ -135,6 +141,11 @@ export default new class ShimManager {
|
||||
}
|
||||
|
||||
private async handleContentMessage (shim: Shim, message: Message) {
|
||||
if (shim.contentTabId === undefined
|
||||
|| shim.contentFrameId === undefined) {
|
||||
throw logger.error("Shim associated with content sender missing tab/frame ID");
|
||||
}
|
||||
|
||||
const [ destination ] = message.subject.split(":/");
|
||||
if (destination === "bridge") {
|
||||
shim.bridgePort.postMessage(message);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
import bridge from "../lib/bridge";
|
||||
import logger from "../lib/logger";
|
||||
|
||||
import { TypedEventTarget } from "../lib/typedEvents";
|
||||
import { Message, Receiver, ReceiverStatus } from "../types";
|
||||
@@ -38,7 +39,7 @@ interface EventMap {
|
||||
export default new class StatusManager
|
||||
extends TypedEventTarget<EventMap> {
|
||||
|
||||
private bridgePort: browser.runtime.Port;
|
||||
private bridgePort: (browser.runtime.Port | null) = null;
|
||||
private receivers = new Map<string, Receiver>();
|
||||
|
||||
constructor () {
|
||||
@@ -131,6 +132,10 @@ export default new class StatusManager
|
||||
|
||||
const receiver = this.receivers.get(id);
|
||||
|
||||
if (!receiver) {
|
||||
throw logger.error(`Could not find receiver (${id}) specified in status message.`);
|
||||
}
|
||||
|
||||
// Merge with existing
|
||||
this.receivers.set(id, {
|
||||
...receiver
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import defaultOptions from "../defaultOptions";
|
||||
import loadSender from "../lib/loadSender";
|
||||
import logger from "../lib/logger";
|
||||
import options from "../lib/options";
|
||||
|
||||
import { getChromeUserAgent } from "../lib/userAgents";
|
||||
@@ -67,6 +68,10 @@ function initBrowserAction () {
|
||||
* 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) {
|
||||
@@ -140,6 +145,10 @@ async function initMenus () {
|
||||
browser.menus.onClicked.addListener(async (info, tab) => {
|
||||
if (info.parentMenuItemId === menuIdWhitelist) {
|
||||
const pattern = whitelistChildMenuPatterns.get(info.menuItemId);
|
||||
if (!pattern) {
|
||||
throw logger.error(`Whitelist pattern not found for menu item ID ${info.menuItemId}.`);
|
||||
}
|
||||
|
||||
const whitelist = await options.get("userAgentWhitelist");
|
||||
|
||||
// Add to whitelist and update options
|
||||
@@ -150,6 +159,17 @@ async function initMenus () {
|
||||
}
|
||||
|
||||
|
||||
if (tab?.id === undefined) {
|
||||
throw logger.error("Menu handler tab ID not found.");
|
||||
}
|
||||
if (info.frameId === undefined) {
|
||||
throw logger.error("Menu handler frame ID not found.");
|
||||
}
|
||||
if (!info.pageUrl) {
|
||||
throw logger.error("Menu handler page URL not found.");
|
||||
}
|
||||
|
||||
|
||||
const availableMediaTypes = getMediaTypesForPageUrl(info.pageUrl);
|
||||
|
||||
switch (info.menuItemId) {
|
||||
@@ -157,6 +177,11 @@ async function initMenus () {
|
||||
const selection = await ReceiverSelectorManager.getSelection(
|
||||
tab.id, info.frameId);
|
||||
|
||||
// Selection cancelled
|
||||
if (!selection) {
|
||||
break;
|
||||
}
|
||||
|
||||
loadSender({
|
||||
tabId: tab.id
|
||||
, frameId: info.frameId
|
||||
@@ -447,6 +472,10 @@ function initWhitelist () {
|
||||
const { os } = await browser.runtime.getPlatformInfo();
|
||||
const chromeUserAgent = getChromeUserAgent(os);
|
||||
|
||||
if (!details.requestHeaders) {
|
||||
throw logger.error("OnBeforeSendHeaders handler details missing requestHeaders.");
|
||||
}
|
||||
|
||||
const host = details.requestHeaders.find(
|
||||
header => header.name === "Host");
|
||||
|
||||
@@ -457,7 +486,7 @@ function initWhitelist () {
|
||||
* so pretend to be an old version of Chrome to get the old
|
||||
* site.
|
||||
*/
|
||||
if (host.value === "www.youtube.com") {
|
||||
if (host?.value === "www.youtube.com") {
|
||||
header.value = getChromeUserAgent(os, true);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ export default class NativeReceiverSelector
|
||||
extends TypedEventTarget<ReceiverSelectorEvents>
|
||||
implements ReceiverSelector {
|
||||
|
||||
private bridgePort: browser.runtime.Port;
|
||||
private bridgePort: (browser.runtime.Port | null) = null;
|
||||
private wasReceiverSelected: boolean = false;
|
||||
private _isOpen: boolean = false;
|
||||
|
||||
|
||||
@@ -4,10 +4,11 @@ import ReceiverSelector, {
|
||||
ReceiverSelectorEvents
|
||||
, ReceiverSelectorMediaType } from "./ReceiverSelector";
|
||||
|
||||
import logger from "../../lib/logger";
|
||||
import options from "../../lib/options";
|
||||
|
||||
import { TypedEventTarget } from "../../lib/typedEvents";
|
||||
import { getWindowCenteredProps } from "../../lib/utils";
|
||||
import { getWindowCenteredProps, WindowCenteredProps } from "../../lib/utils";
|
||||
import { Message, Receiver } from "../../types";
|
||||
|
||||
|
||||
@@ -15,19 +16,19 @@ export default class PopupReceiverSelector
|
||||
extends TypedEventTarget<ReceiverSelectorEvents>
|
||||
implements ReceiverSelector {
|
||||
|
||||
private windowId: number;
|
||||
private windowId?: number;
|
||||
|
||||
private messagePort: browser.runtime.Port;
|
||||
private messagePortDisconnected: boolean;
|
||||
private messagePort?: browser.runtime.Port;
|
||||
private messagePortDisconnected?: boolean;
|
||||
|
||||
private receivers: Receiver[];
|
||||
private defaultMediaType: ReceiverSelectorMediaType;
|
||||
private availableMediaTypes: ReceiverSelectorMediaType;
|
||||
private receivers?: Receiver[];
|
||||
private defaultMediaType?: ReceiverSelectorMediaType;
|
||||
private availableMediaTypes?: ReceiverSelectorMediaType;
|
||||
|
||||
private wasReceiverSelected: boolean = false;
|
||||
|
||||
private _isOpen: boolean = false;
|
||||
private requestedAppId: string;
|
||||
private requestedAppId?: string;
|
||||
|
||||
|
||||
constructor () {
|
||||
@@ -97,9 +98,22 @@ export default class PopupReceiverSelector
|
||||
this.defaultMediaType = defaultMediaType;
|
||||
this.availableMediaTypes = availableMediaTypes;
|
||||
|
||||
// Calculate centered size/position based on current window
|
||||
const centeredProps = getWindowCenteredProps(
|
||||
await browser.windows.getCurrent(), 350, 200);
|
||||
|
||||
let centeredProps: WindowCenteredProps = {
|
||||
left: 100
|
||||
, top: 100
|
||||
, width: 350
|
||||
, height: 200
|
||||
};
|
||||
|
||||
try {
|
||||
// Calculate centered size/position based on current window
|
||||
centeredProps = getWindowCenteredProps(
|
||||
await browser.windows.getCurrent()
|
||||
, centeredProps.width, centeredProps.height);
|
||||
} catch {
|
||||
// Shouldn't ever hit this, but defaults are provided in case
|
||||
}
|
||||
|
||||
const popup = await browser.windows.create({
|
||||
url: "ui/popup/index.html"
|
||||
@@ -107,6 +121,10 @@ export default class PopupReceiverSelector
|
||||
, ...centeredProps
|
||||
});
|
||||
|
||||
if (popup?.id === undefined) {
|
||||
throw logger.error("Failed to create receiver selector popup.");
|
||||
}
|
||||
|
||||
this._isOpen = true;
|
||||
this.windowId = popup.id;
|
||||
|
||||
@@ -132,7 +150,7 @@ export default class PopupReceiverSelector
|
||||
}
|
||||
|
||||
this._isOpen = false;
|
||||
this.requestedAppId = null;
|
||||
this.requestedAppId = undefined;
|
||||
|
||||
if (this.messagePort && !this.messagePortDisconnected) {
|
||||
this.messagePort.disconnect();
|
||||
@@ -183,10 +201,11 @@ export default class PopupReceiverSelector
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
this.windowId = null;
|
||||
this.messagePort = null;
|
||||
this.receivers = null;
|
||||
this.defaultMediaType = null;
|
||||
this.windowId = undefined;
|
||||
this.messagePort = undefined;
|
||||
this.receivers = undefined;
|
||||
this.defaultMediaType = undefined;
|
||||
this.availableMediaTypes = undefined;
|
||||
this.wasReceiverSelected = false;
|
||||
}
|
||||
|
||||
@@ -203,7 +222,9 @@ export default class PopupReceiverSelector
|
||||
browser.windows.onFocusChanged.removeListener(
|
||||
this.onWindowsFocusChanged);
|
||||
|
||||
browser.windows.remove(this.windowId);
|
||||
if (this.windowId) {
|
||||
browser.windows.remove(this.windowId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
import options from "../../lib/options";
|
||||
import logger from "../../lib/logger";
|
||||
|
||||
import ShimManager from "../ShimManager";
|
||||
import StatusManager from "../StatusManager";
|
||||
@@ -38,7 +39,11 @@ let sharedSelector: ReceiverSelector;
|
||||
|
||||
async function getSelector () {
|
||||
if (!sharedSelector) {
|
||||
sharedSelector = await createSelector();
|
||||
try {
|
||||
sharedSelector = await createSelector();
|
||||
} catch (err) {
|
||||
throw logger.error("Failed to create receiver selector.");
|
||||
}
|
||||
}
|
||||
|
||||
return sharedSelector;
|
||||
@@ -59,7 +64,7 @@ async function getSelection (
|
||||
contextTabId: number
|
||||
, contextFrameId = 0
|
||||
, withMediaSender = false)
|
||||
: Promise<ReceiverSelection> {
|
||||
: Promise<ReceiverSelection | null> {
|
||||
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let currentShim = ShimManager.getShim(
|
||||
@@ -71,7 +76,7 @@ async function getSelection (
|
||||
*/
|
||||
if (currentShim?.requestedAppId ===
|
||||
await options.get("mirroringAppId")) {
|
||||
currentShim = null;
|
||||
currentShim = undefined;
|
||||
}
|
||||
|
||||
let defaultMediaType = ReceiverSelectorMediaType.Tab;
|
||||
@@ -164,8 +169,7 @@ async function getSelection (
|
||||
Array.from(StatusManager.getReceivers())
|
||||
, defaultMediaType
|
||||
, availableMediaTypes
|
||||
, currentShim?.requestedAppId
|
||||
?? (withMediaSender && DEFAULT_MEDIA_RECEIVER_APP_ID));
|
||||
, currentShim?.requestedAppId ?? DEFAULT_MEDIA_RECEIVER_APP_ID);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user