Skip to content

The only downloader you need. 下载器的集大成者。

feat(extension): 委托 yt-dlp 时传递登录 Cookie

XiaoYouChR6a1fb084259a
diff --git a/browser_extension/app/src/background/resource-bridge.ts b/browser_extension/app/src/background/resource-bridge.ts
index 9de6993d..9d0add9d 100644
--- a/browser_extension/app/src/background/resource-bridge.ts
+++ b/browser_extension/app/src/background/resource-bridge.ts
@@ -901,21 +901,57 @@ export function createResourceBridge(options: {
return { ok: false, message: "未知的下载请求类型" };
}
- // yt-dlp and other external tools take the page URL, not a captured direct URL — the
- // desktop runs the tool, which extracts the media itself.
+ // The desktop's yt-dlp extracts the media from the page URL; forward login cookies for gated videos.
async function dispatchExternalDownload(
- selection: { pageUrl: string; tool: string },
+ selection: { pageUrl: string },
title: string,
fallbackPageUrl: string,
): Promise<DesktopRequestResult> {
return options.sendDesktopRequest<DesktopRequestResult>({
type: "create_task",
- source: selection.tool,
+ source: "ytdlp",
title: title || "",
- payload: { url: selection.pageUrl, pageUrl: fallbackPageUrl || selection.pageUrl, pageTitle: title || "" },
+ payload: {
+ url: selection.pageUrl,
+ pageUrl: fallbackPageUrl || selection.pageUrl,
+ pageTitle: title || "",
+ headers: resolvePageHeaders(selection.pageUrl),
+ },
});
}
+ function hostOf(url: string): string | null {
+ try {
+ return new URL(url).host;
+ } catch {
+ return null;
+ }
+ }
+
+ // SPA watch URLs often have no snapshot of their own; fall back to the freshest same-host
+ // request that carried a cookie. Only cookie + user-agent are forwarded.
+ function resolvePageHeaders(pageUrl: string): Record<string, string> {
+ const host = hostOf(pageUrl);
+ if (host === null) {
+ return {};
+ }
+
+ let snapshot = resolveHeaderSnapshot(pageUrl);
+ if (!snapshot?.headers.cookie) {
+ let freshest: HeaderSnapshot | null = null;
+ for (const candidate of headerSnapshotsByUrl.values()) {
+ if (!candidate.headers.cookie || hostOf(candidate.url) !== host) { continue; }
+ if (!freshest || candidate.capturedAt > freshest.capturedAt) { freshest = candidate; }
+ }
+ snapshot = freshest;
+ }
+
+ const headers: Record<string, string> = {};
+ if (snapshot?.headers.cookie) { headers.cookie = snapshot.headers.cookie; }
+ if (snapshot?.headers["user-agent"]) { headers["user-agent"] = snapshot.headers["user-agent"]; }
+ return headers;
+ }
+
async function dispatchMergeResources(resources: CapturedResource[]): Promise<DesktopRequestResult> {
if (resources.length !== 2) {
return { ok: false, message: "在线合并暂时只支持选中 2 个资源" };
diff --git a/browser_extension/app/src/page-media/strategies/youtube.ts b/browser_extension/app/src/page-media/strategies/youtube.ts
index 82297e00..399010a8 100644
--- a/browser_extension/app/src/page-media/strategies/youtube.ts
+++ b/browser_extension/app/src/page-media/strategies/youtube.ts
@@ -1,9 +1,8 @@
import type {ResolveContext} from "../strategy";
import type {Resolution} from "../types";
-// YouTube serves media via SABR/cipher-gated requests the page player signs itself, so the
-// captured URLs aren't re-downloadable. We hand the page URL to the desktop's yt-dlp instead
-// of resolving a direct URL — hence ctx's attributed URLs are deliberately ignored.
+// YouTube's media URLs are SABR/cipher-gated and not re-downloadable, so we delegate the page
+// URL to the desktop's yt-dlp and ignore captured URLs.
export function resolveYouTube(ctx: ResolveContext): Resolution {
- return { kind: "selection", selection: { kind: "external", pageUrl: ctx.pageUrl.href, tool: "ytdlp" } };
+ return { kind: "selection", selection: { kind: "external", pageUrl: ctx.pageUrl.href } };
}
diff --git a/browser_extension/app/src/page-media/types.ts b/browser_extension/app/src/page-media/types.ts
index abf0a22c..0ef744a3 100644
--- a/browser_extension/app/src/page-media/types.ts
+++ b/browser_extension/app/src/page-media/types.ts
@@ -46,9 +46,8 @@ export type Selection =
| { kind: "single"; url: string; formKind: VideoSessionFormKind }
| { kind: "stream"; url: string }
| { kind: "merge"; video: string; audio: string }
- // Not a captured direct URL — the page URL handed to a desktop external tool (yt-dlp)
- // that extracts the media itself. For SABR/cipher-gated sites like YouTube.
- | { kind: "external"; pageUrl: string; tool: "ytdlp" };
+ // The page URL handed to the desktop's yt-dlp, which extracts the media itself (YouTube/SABR).
+ | { kind: "external"; pageUrl: string };
export type Resolution =
| { kind: "selection"; selection: Selection }