Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { MessageType } from "./shared/messages";
import { VideoMetadata } from "./core/types";
import { VideoMetadata, VideoFormat } from "./core/types";
import { DetectionManager } from "./core/detection/detection-manager";
import { normalizeUrl } from "./core/utils/url-utils";
import { logger } from "./core/utils/logger";
Expand Down Expand Up @@ -68,7 +68,7 @@ function removeDetectedVideo(url: string): void {
*/
function addDetectedVideo(video: VideoMetadata) {
// Reject unknown formats - don't show them in UI
if (video.format === "unknown") {
if (video.format === VideoFormat.UNKNOWN) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/detection/detection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @module DetectionManager
*/

import { VideoMetadata } from "../types";
import { VideoMetadata, VideoFormat } from "../types";
import { logger } from "../utils/logger";
import { detectFormatFromUrl } from "../utils/url-utils";
import { DirectDetectionHandler } from "./direct/direct-detection-handler";
Expand Down Expand Up @@ -68,12 +68,12 @@ export class DetectionManager {
const format = detectFormatFromUrl(url);

switch (format) {
case "direct":
case VideoFormat.DIRECT:
logger.debug("[Media Bridge] Direct video detected", { url });
this.directHandler.handleNetworkRequest(url);
break;

case "hls":
case VideoFormat.HLS:
logger.debug("[Media Bridge] HLS video detected", { url });
this.hlsHandler.handleNetworkRequest(url);
break;
Expand Down
4 changes: 2 additions & 2 deletions src/core/detection/direct/direct-detection-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @module DirectDetectionHandler
*/

import { VideoMetadata } from "../../types";
import { VideoMetadata, VideoFormat } from "../../types";
import { detectFormatFromUrl } from "../../utils/url-utils";
import { extractThumbnail } from "../../utils/thumbnail-utils";

Expand Down Expand Up @@ -339,7 +339,7 @@ export class DirectDetectionHandler {
const format = detectFormatFromUrl(url);

// Reject unknown formats
if (format === "unknown") {
if (format === VideoFormat.UNKNOWN) {
return null;
}

Expand Down
10 changes: 5 additions & 5 deletions src/core/detection/hls/hls-detection-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @module HlsDetectionHandler
*/

import { VideoMetadata } from "../../types";
import { VideoMetadata, VideoFormat } from "../../types";
import {
isMasterPlaylist,
isMediaPlaylist,
Expand Down Expand Up @@ -168,7 +168,7 @@ export class HlsDetectionHandler {
// A media playlist without #EXT-X-ENDLIST is a live stream
const isLive = !playlistText.includes("#EXT-X-ENDLIST");
logger.info("[Media Bridge] Detected standalone M3U8 media playlist", url);
return await this.addDetectedVideo(url, "m3u8", playlistText, isLive);
return await this.addDetectedVideo(url, VideoFormat.M3U8, playlistText, isLive);
}

/**
Expand Down Expand Up @@ -197,7 +197,7 @@ export class HlsDetectionHandler {
// Determine liveness by fetching the first variant playlist and checking for #EXT-X-ENDLIST
const isLive = await this.checkMasterIsLive(levels);

return await this.addDetectedVideo(url, "hls", playlistText, isLive);
return await this.addDetectedVideo(url, VideoFormat.HLS, playlistText, isLive);
}

/**
Expand Down Expand Up @@ -268,7 +268,7 @@ export class HlsDetectionHandler {
*/
private async addDetectedVideo(
url: string,
format: "hls" | "m3u8",
format: VideoFormat.HLS | VideoFormat.M3U8,
playlistText: string,
isLive: boolean = false,
): Promise<VideoMetadata | null> {
Expand Down Expand Up @@ -372,7 +372,7 @@ export class HlsDetectionHandler {
*/
private async extractMetadata(
url: string,
format: "hls" | "m3u8" = "hls",
format: VideoFormat.HLS | VideoFormat.M3U8 = VideoFormat.HLS,
playlistText: string,
isLive: boolean = false,
): Promise<VideoMetadata | null> {
Expand Down
8 changes: 4 additions & 4 deletions src/core/downloader/download-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class DownloadManager {
);

// Validate format from metadata (should already be set by detection)
if (metadata.format === "unknown") {
if (metadata.format === VideoFormat.UNKNOWN) {
const error = new Error(`Video format is unknown for URL: ${url}`);
return await this.createFailedState(
state.id,
Expand All @@ -136,7 +136,7 @@ export class DownloadManager {
const actualVideoUrl = metadata.url;

// Route to appropriate download handler based on format
if (format === "direct") {
if (format === VideoFormat.DIRECT) {
// Use direct download handler with Chrome downloads API
// AbortSignal is used to cancel the HEAD request for extension detection
// The actual Chrome download is cancelled via chrome.downloads.cancel
Expand All @@ -146,7 +146,7 @@ export class DownloadManager {
state.id,
abortSignal,
);
} else if (format === "hls") {
} else if (format === VideoFormat.HLS) {
// Use HLS download handler
await this.hlsDownloadHandler.download(
actualVideoUrl,
Expand All @@ -156,7 +156,7 @@ export class DownloadManager {
abortSignal,
metadata.pageUrl,
);
} else if (format === "m3u8") {
} else if (format === VideoFormat.M3U8) {
// Use M3U8 download handler
await this.m3u8DownloadHandler.download(
actualVideoUrl,
Expand Down
7 changes: 6 additions & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
* Type definitions for Media Bridge Extension
*/

export type VideoFormat = "direct" | "hls" | "m3u8" | "unknown";
export enum VideoFormat {
DIRECT = "direct",
HLS = "hls",
M3U8 = "m3u8",
UNKNOWN = "unknown",
}

export interface VideoMetadata {
url: string;
Expand Down
14 changes: 7 additions & 7 deletions src/core/utils/url-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ export function normalizeUrl(url: string): string {
export function detectFormatFromUrl(url: string): VideoFormat {
// Handle blob URLs - these are already video blobs, treat as direct
if (url.startsWith("blob:")) {
return "direct";
return VideoFormat.DIRECT;
}

// Handle data URLs - treat as direct
if (url.startsWith("data:")) {
return "direct";
return VideoFormat.DIRECT;
}

let urlObj: URL;
try {
urlObj = new URL(url);
} catch (error) {
return "unknown";
return VideoFormat.UNKNOWN;
}

const pathnameLower = urlObj.pathname.toLowerCase();

// Check for HLS playlist files (.m3u8) on pathname only
if (pathnameLower.endsWith(".m3u8")) {
return "hls";
return VideoFormat.HLS;
}

// Check for common video extensions on pathname only
Expand All @@ -61,14 +61,14 @@ export function detectFormatFromUrl(url: string): VideoFormat {
".wmv",
];
if (videoExtensions.some((ext) => pathnameLower.endsWith(ext))) {
return "direct";
return VideoFormat.DIRECT;
}

// If no clear format detected but it looks like a video URL, assume direct
// Many video CDNs don't include file extensions
if (urlObj.pathname.match(/\/(video|stream|media|v|embed)\//i)) {
return "direct";
return VideoFormat.DIRECT;
}

return "unknown";
return VideoFormat.UNKNOWN;
}
Loading