diff --git a/src/entries/background/mv2.ts b/src/entries/background/mv2.ts index 4732258..438a3c0 100644 --- a/src/entries/background/mv2.ts +++ b/src/entries/background/mv2.ts @@ -5,11 +5,13 @@ import { storageDelete, storageGet, storageSet } from '~/lib/settings'; import { getMatch } from '~/lib/match'; chrome.webRequest.onBeforeSendHeaders.addListener( + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore async (details) => { const referer: { domain: string } | undefined = await storageGet('referer'); if (referer === undefined) return; - details.requestHeaders.push({ + details.requestHeaders!.push({ name: 'Referer', value: `https://${referer.domain}/` }); @@ -26,8 +28,8 @@ chrome.webRequest.onBeforeRedirect.addListener( async (details) => { // check if redirects origins from a previous redirect if ((await storageGet('redirect')) === undefined) { - let match: Match; - if ((match = await getMatch(new URL(details.url).hostname)) !== undefined) { + let match: Match | null; + if ((match = await getMatch(new URL(details.url).hostname)) !== null) { await storageSet('redirect', match.id); } } else { diff --git a/src/entries/contentScript/main.ts b/src/entries/contentScript/main.ts index da6c42f..0f56c76 100644 --- a/src/entries/contentScript/main.ts +++ b/src/entries/contentScript/main.ts @@ -3,7 +3,7 @@ import { getMatch } from '~/lib/match'; import { Other, Redirect } from '~/lib/settings'; async function main() { - let match: Match; + let match: Match | null; let redirect = false; if ((match = await getMatch(window.location.host)) === null) { if ((match = await Redirect.get()) === null) { @@ -20,7 +20,12 @@ async function main() { await Redirect.delete(); } - const url = await match.match(re); + let url: string; + try { + url = await match.match(re); + } catch (e) { + return; + } // send the url to the ff2mpv (https://github.com/woodruffw/ff2mpv) application if (await Other.getFf2mpv()) { diff --git a/src/entries/player/player.ts b/src/entries/player/player.ts index 8ca53fd..730d169 100644 --- a/src/entries/player/player.ts +++ b/src/entries/player/player.ts @@ -22,9 +22,9 @@ async function playHls(url: string, videoElem: HTMLVideoElement) { export async function play(videoElem: HTMLVideoElement) { const urlQuery = new URLSearchParams(window.location.search); - const id = urlQuery.get('id'); - const url = decodeURIComponent(urlQuery.get('url')); - const domain = urlQuery.get('domain'); + const id = urlQuery.get('id') as string; + const url = decodeURIComponent(urlQuery.get('url') as string); + const domain = urlQuery.get('domain') as string; const match = matches[id]; if (match === undefined) { diff --git a/src/entries/popup/main.ts b/src/entries/popup/main.ts index e055e41..5230165 100644 --- a/src/entries/popup/main.ts +++ b/src/entries/popup/main.ts @@ -1,5 +1,5 @@ import App from './Popup.svelte'; new App({ - target: document.getElementById('app') + target: document.getElementById('app') as Element }); diff --git a/src/lib/match.ts b/src/lib/match.ts index 96914cb..cdc290b 100644 --- a/src/lib/match.ts +++ b/src/lib/match.ts @@ -56,7 +56,7 @@ export const DropLoad: Match = { match: async (match: RegExpMatchArray) => { const unpacked = await unpack(match[0]); - return unpacked.match(/(?<=file:").*(?=")/)[0]; + return unpacked.match(/(?<=file:").*(?=")/)![0]; } }; @@ -68,7 +68,7 @@ export const Filemoon: Match = { match: async (match: RegExpMatchArray) => { const unpacked = await unpack(match[0]); - return unpacked.match(/(?<=file:").*(?=")/)[0]; + return unpacked.match(/(?<=file:").*(?=")/)![0]; } }; @@ -91,7 +91,7 @@ export const Kwik: Match = { match: async (match: RegExpMatchArray) => { const unpacked = await unpack(match[0]); - return unpacked.match(/(?<=source=').*(?=')/)[0]; + return unpacked.match(/(?<=source=').*(?=')/)![0]; } }; @@ -103,7 +103,7 @@ export const Mixdrop: Match = { match: async (match: RegExpMatchArray) => { const unpacked = await unpack(match[0]); - const url = unpacked.match(/(?<=MDCore.wurl=").*(?=")/)[0]; + const url = unpacked.match(/(?<=MDCore.wurl=").*(?=")/)![0]; return `https:${url}`; } }; @@ -117,7 +117,7 @@ export const Mp4Upload: Match = { match: async (match: RegExpMatchArray) => { const unpacked = await unpack(match[0]); - return unpacked.match(/(?<=player.src\(").*(?=")/)[0]; + return unpacked.match(/(?<=player.src\(").*(?=")/)![0]; } }; @@ -179,7 +179,7 @@ export const SuperVideo: Match = { match: async (match: RegExpMatchArray) => { const unpacked = await unpack(match[0]); - return unpacked.match(/(?<=file:").*(?=")/)[0]; + return unpacked.match(/(?<=file:").*(?=")/)![0]; } }; @@ -191,7 +191,7 @@ export const Upstream: Match = { match: async (match: RegExpMatchArray) => { const unpacked = await unpack(match[0]); - return unpacked.match(/(?<=file:").*(?=")/)[0]; + return unpacked.match(/(?<=file:").*(?=")/)![0]; } }; diff --git a/src/lib/settings.ts b/src/lib/settings.ts index d0c0e3a..9d9ebae 100644 --- a/src/lib/settings.ts +++ b/src/lib/settings.ts @@ -3,11 +3,11 @@ import { matches } from './match'; export const Hosters = { getDisabled: async () => { - const disabled = await storageGet('hosters.disabled', []); + const disabled = (await storageGet('hosters.disabled', [])) as string[]; return disabled.map((id) => matches[id]).filter((m) => m !== undefined); }, disable: async (match: Match) => { - const disabled = await storageGet('hosters.disabled', []); + const disabled = (await storageGet('hosters.disabled', [])) as string[]; const index = disabled.indexOf(match.id); if (index === -1) { disabled.push(match.id); @@ -15,7 +15,7 @@ export const Hosters = { } }, enable: async (match: Match) => { - const disabled = await storageGet('hosters.disabled', []); + const disabled = (await storageGet('hosters.disabled', [])) as string[]; const index = disabled.indexOf(match.id); if (index !== -1) { disabled.splice(index, 1); @@ -35,7 +35,7 @@ export const Hosters = { export const Redirect = { get: async (): Promise => { - return matches[await storageGet('redirect')] || null; + return matches[(await storageGet('redirect')) as string] || null; }, set: async (match: Match) => { await storageSet('redirect', match.id); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index ee12d2d..5db546f 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -19,7 +19,7 @@ export async function unpack(packed: string): Promise { }' `; - const res: string = await runInPageContext(toExecute); + const res = (await runInPageContext(toExecute)) as string; return res .replace(/;/g, ';\n') .replace(/{/g, '\n{\n') @@ -29,7 +29,7 @@ export async function unpack(packed: string): Promise { } // Adapted from: https://github.com/arikw/extension-page-context -async function runInPageContext(toExecute: string): Promise { +async function runInPageContext(toExecute: string): Promise { // test that we are running with the allow-scripts permission try { window.sessionStorage; @@ -44,7 +44,8 @@ async function runInPageContext(toExecute: string): Promise { const scriptElm = document.createElement('script'); scriptElm.setAttribute('type', 'application/javascript'); - const code = ` + // inject the script + scriptElm.textContent = ` ( async function () { @@ -63,9 +64,6 @@ async function runInPageContext(toExecute: string): Promise { )(); `; - // inject the script - scriptElm.textContent = code; - // run the script document.documentElement.appendChild(scriptElm); diff --git a/src/manifest.ts b/src/manifest.ts index 41cf12d..fc2d258 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -39,7 +39,7 @@ const ManifestV2 = { scripts: ['src/entries/background/mv2.ts'], persistent: true }, - content_scripts: [{ ...sharedManifest.content_scripts[0], matches: [''] }], + content_scripts: [{ ...sharedManifest.content_scripts![0], matches: [''] }], browser_action: browserAction, permissions: [...sharedManifest.permissions, 'webRequest', 'webRequestBlocking', ''] }; @@ -67,7 +67,7 @@ export function getManifest( ...manifest, ...ManifestV2, manifest_version: manifestVersion - }; + } as chrome.runtime.ManifestV2; } if (manifestVersion === 3) { @@ -78,7 +78,7 @@ export function getManifest( name: `${manifest.name} Lite`, ...ManifestV3, manifest_version: manifestVersion - }; + } as chrome.runtime.ManifestV3; } throw new Error(`Missing manifest definition for manifestVersion ${manifestVersion}`);