Use enum to specify media type

This commit is contained in:
bytedream 2025-04-03 23:56:18 +02:00
parent f6fcfd354a
commit 50f400b8b9
3 changed files with 23 additions and 19 deletions

View File

@ -1,6 +1,5 @@
import type { Match } from '~/lib/match'; import {getMatch, type Match, MatchMediaType} from '~/lib/match';
import { getMatch } from '~/lib/match'; import {Other, Redirect} from '~/lib/settings';
import { Other, Redirect } from '~/lib/settings';
async function main() { async function main() {
let match: Match | null; let match: Match | null;
@ -35,19 +34,19 @@ async function main() {
} }
let url: string | null; let url: string | null;
let urlType: 'hls' | 'native'; let urlType: MatchMediaType | null;
try { try {
const matchResult = await match.match(re); const matchResult = await match.match(re);
if (matchResult && typeof matchResult === 'string') { if (matchResult && typeof matchResult === 'string') {
url = matchResult; url = matchResult;
urlType = url.includes('.m3u8') ? 'hls' : 'native'; urlType = url.includes('.m3u8') ? MatchMediaType.Hls : MatchMediaType.Native;
} else if (matchResult && typeof matchResult === 'object') { } else if (matchResult && typeof matchResult === 'object') {
if ('hls' in matchResult) { if (MatchMediaType.Hls in matchResult) {
url = matchResult['hls']; url = matchResult[MatchMediaType.Hls];
urlType = 'hls'; urlType = MatchMediaType.Hls;
} else if ('native' in matchResult) { } else if (MatchMediaType.Native in matchResult) {
url = matchResult['native']; url = matchResult[MatchMediaType.Native];
urlType = 'native'; urlType = MatchMediaType.Native;
} }
} }
} catch { } catch {
@ -65,7 +64,7 @@ async function main() {
await chrome.runtime.sendMessage({ action: 'ff2mpv', url: url }); await chrome.runtime.sendMessage({ action: 'ff2mpv', url: url });
} }
if (match.replace && urlType !== 'hls') { if (match.replace && urlType != MatchMediaType.Hls) {
// this destroys all intervals that may spawn popups or events // this destroys all intervals that may spawn popups or events
let intervalId = window.setInterval(() => {}, 0); let intervalId = window.setInterval(() => {}, 0);
while (intervalId--) { while (intervalId--) {

View File

@ -1,6 +1,6 @@
import { matches } from '~/lib/match'; import {matches, MatchMediaType} from '~/lib/match';
import Hls from 'hls.js'; import Hls from 'hls.js';
import { UrlReferer } from '~/lib/settings'; import {UrlReferer} from '~/lib/settings';
async function playNative(url: string, domain: string, videoElem: HTMLVideoElement) { async function playNative(url: string, domain: string, videoElem: HTMLVideoElement) {
await UrlReferer.set(new URL(url).hostname, domain); await UrlReferer.set(new URL(url).hostname, domain);
@ -31,7 +31,7 @@ export async function play(videoElem: HTMLVideoElement) {
const id = urlQuery.get('id') as string; const id = urlQuery.get('id') as string;
const url = decodeURIComponent(urlQuery.get('url') as string); const url = decodeURIComponent(urlQuery.get('url') as string);
const domain = urlQuery.get('domain') as string; const domain = urlQuery.get('domain') as string;
const urlType = urlQuery.get('urlType') as string; const urlType = urlQuery.get('urlType') as MatchMediaType;
const match = matches[id]; const match = matches[id];
if (match === undefined) { if (match === undefined) {
@ -39,9 +39,9 @@ export async function play(videoElem: HTMLVideoElement) {
} }
document.title = `Stream Bypass (${domain})`; document.title = `Stream Bypass (${domain})`;
if (urlType === 'hls') { if (urlType === MatchMediaType.Hls) {
await playHls(url, domain, videoElem); await playHls(url, domain, videoElem);
} else if (urlType === 'native') { } else if (urlType === MatchMediaType.Native) {
await playNative(url, domain, videoElem); await playNative(url, domain, videoElem);
} }
} }

View File

@ -10,7 +10,12 @@ export interface Match {
regex: RegExp[]; regex: RegExp[];
notice?: string; notice?: string;
match(match: RegExpMatchArray): Promise<string | { hls: string } | { native: string } | null>; match(match: RegExpMatchArray): Promise<string | {[MatchMediaType.Hls]: string} | {[MatchMediaType.Native]: string} | null>;
}
export enum MatchMediaType {
Hls = 'hls',
Native = 'native',
} }
export const Doodstream: Match = { export const Doodstream: Match = {
@ -129,7 +134,7 @@ export const LoadX: Match = {
const videoSource: string = responseJson['videoSource']; const videoSource: string = responseJson['videoSource'];
// extension of extracted url is '.txt', so we have to manually specify that it's a hls // extension of extracted url is '.txt', so we have to manually specify that it's a hls
return {hls: videoSource.replace('\\/', '/')}; return {[MatchMediaType.Hls]: videoSource.replace('\\/', '/')};
} }
}; };