Skip to content

Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.

feat(chrome-extension): forward browser cookies to yt-dlp for authenticated downloads

InstaZDLL10142cab20dd
diff --git a/browser-extension/chrome/manifest.json b/browser-extension/chrome/manifest.json
index 8fb26940..c0024853 100644
--- a/browser-extension/chrome/manifest.json
+++ b/browser-extension/chrome/manifest.json
@@ -7,7 +7,20 @@
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0ribk9X5HR4XBloFk2g/V6kjICK8XWYC/QqrGFTOjKkRhRNDJr8Z14JFLggvWBbrj0D/zYdd5CfHJG01bGWMZC0hxRtm71q5Pbu1jtChEfE4QO/xWvM8WfrXH895nJFcUzxi137eoWq+FtSGxqFnSl6IXg5EZdgYH5LN3t6kL0HTwKnJLB3/8d4JD1uF2tQxHzii+MosIozKyDatqtEaBLprY1gfAfCwfY8Iqr9yehFUm7IDH+TGwjE17GJY1ZhfkpNBRmH/h23hwyk6coocSsWpM9GbCFdBX22iUnZjxZqACO1n+8xw2364Lznqtuot4iawznQGF4ALL7CbIvHPOQIDAQAB",
"permissions": [
"nativeMessaging",
- "tabs"
+ "tabs",
+ "cookies"
+ ],
+ "host_permissions": [
+ "*://*.youtube.com/*",
+ "*://*.google.com/*",
+ "*://*.instagram.com/*",
+ "*://*.tiktok.com/*",
+ "*://*.twitter.com/*",
+ "*://*.x.com/*",
+ "*://*.reddit.com/*",
+ "*://*.twitch.tv/*",
+ "*://*.vimeo.com/*",
+ "*://*.bilibili.com/*"
],
"background": {
"service_worker": "src/background.js",
diff --git a/browser-extension/chrome/src/action-click.js b/browser-extension/chrome/src/action-click.js
index 93d735f2..9803f850 100644
--- a/browser-extension/chrome/src/action-click.js
+++ b/browser-extension/chrome/src/action-click.js
@@ -3,6 +3,8 @@ const DEFAULT_LAUNCH_FAILED_CODE = "LAUNCH_FAILED";
export async function handleSupportedActionClick({
tabId,
url,
+ platform,
+ getCookies,
sendNativeMessage,
clearBadge = async () => {},
showSuccessBadge = async () => {},
@@ -13,11 +15,22 @@ export async function handleSupportedActionClick({
await clearBadge(tabId);
}
+ let cookies = null;
try {
- const response = await sendNativeMessage({
- type: "enqueue",
- url,
- });
+ if (getCookies && platform) {
+ cookies = await getCookies(platform);
+ }
+ } catch {
+ // Cookie extraction failed — continue without cookies
+ }
+
+ try {
+ const message = { type: "enqueue", url };
+ if (cookies && cookies.length > 0) {
+ message.cookies = cookies;
+ }
+
+ const response = await sendNativeMessage(message);
if (!response?.ok) {
await openErrorPage({
diff --git a/browser-extension/chrome/src/background.js b/browser-extension/chrome/src/background.js
index ca1e139b..a4fcade9 100644
--- a/browser-extension/chrome/src/background.js
+++ b/browser-extension/chrome/src/background.js
@@ -1,3 +1,4 @@
+import { extractCookiesForPlatform } from "./cookies.js";
import { detectSupportedMediaUrl } from "./detect.js";
import { handleSupportedActionClick } from "./action-click.js";
import { createActionFeedbackController } from "./action-feedback.js";
@@ -73,6 +74,8 @@ chrome.action.onClicked.addListener(async (tab) => {
await handleSupportedActionClick({
tabId: tab?.id,
url: detected.url,
+ platform: detected.platform,
+ getCookies: extractCookiesForPlatform,
sendNativeMessage,
clearBadge: (tabId) => actionFeedback.clearBadge(tabId),
showSuccessBadge: (tabId) => actionFeedback.showSuccessBadge(tabId),
diff --git a/browser-extension/chrome/src/cookies.js b/browser-extension/chrome/src/cookies.js
new file mode 100644
index 00000000..f4fe56cc
--- /dev/null
+++ b/browser-extension/chrome/src/cookies.js
@@ -0,0 +1,40 @@
+const PLATFORM_COOKIE_DOMAINS = Object.freeze({
+ youtube: [".youtube.com", ".google.com"],
+ instagram: [".instagram.com"],
+ tiktok: [".tiktok.com"],
+ twitter: [".twitter.com", ".x.com"],
+ reddit: [".reddit.com"],
+ twitch: [".twitch.tv"],
+ vimeo: [".vimeo.com"],
+ bilibili: [".bilibili.com"],
+});
+
+export async function extractCookiesForPlatform(platform, getAllCookies = defaultGetAllCookies) {
+ const domains = PLATFORM_COOKIE_DOMAINS[platform];
+ if (!domains) return null;
+
+ const allCookies = [];
+ for (const domain of domains) {
+ const cookies = await getAllCookies({ domain });
+ allCookies.push(...cookies);
+ }
+
+ if (allCookies.length === 0) return null;
+
+ return allCookies.map((c) => ({
+ domain: c.domain,
+ httpOnly: c.httpOnly,
+ path: c.path,
+ secure: c.secure,
+ expires: c.expirationDate ? Math.floor(c.expirationDate) : 0,
+ name: c.name,
+ value: c.value,
+ }));
+}
+
+function defaultGetAllCookies(details) {
+ if (typeof chrome === "undefined" || !chrome.cookies?.getAll) {
+ return Promise.resolve([]);
+ }
+ return chrome.cookies.getAll(details);
+}
diff --git a/browser-extension/chrome/tests/action-click.test.mjs b/browser-extension/chrome/tests/action-click.test.mjs
index 1fd36c8b..14634fe0 100644
--- a/browser-extension/chrome/tests/action-click.test.mjs
+++ b/browser-extension/chrome/tests/action-click.test.mjs
@@ -33,6 +33,70 @@ test("clears the previous badge and shows success feedback after a successful se
]);
});
+test("includes cookies in the native message when getCookies returns data", async () => {
+ const calls = [];
+ const fakeCookies = [{ domain: ".youtube.com", name: "LOGIN_INFO", value: "abc" }];
+
+ await handleSupportedActionClick({
+ tabId: 1,
+ url: "https://www.youtube.com/watch?v=123",
+ platform: "youtube",
+ getCookies: async () => fakeCookies,
+ sendNativeMessage: async (message) => {
+ calls.push(["send", message]);
+ return { ok: true };
+ },
+ clearBadge: async () => {},
+ showSuccessBadge: async () => {},
+ openErrorPage: async () => {},
+ mapChromeErrorCode: () => "LAUNCH_FAILED",
+ });
+
+ assert.deepEqual(calls[0][1].cookies, fakeCookies);
+});
+
+test("sends message without cookies when getCookies returns null", async () => {
+ const calls = [];
+
+ await handleSupportedActionClick({
+ tabId: 1,
+ url: "https://www.youtube.com/watch?v=123",
+ platform: "youtube",
+ getCookies: async () => null,
+ sendNativeMessage: async (message) => {
+ calls.push(["send", message]);
+ return { ok: true };
+ },
+ clearBadge: async () => {},
+ showSuccessBadge: async () => {},
+ openErrorPage: async () => {},
+ mapChromeErrorCode: () => "LAUNCH_FAILED",
+ });
+
+ assert.equal(calls[0][1].cookies, undefined);
+});
+
+test("sends message without cookies when getCookies throws", async () => {
+ const calls = [];
+
+ await handleSupportedActionClick({
+ tabId: 1,
+ url: "https://www.youtube.com/watch?v=123",
+ platform: "youtube",
+ getCookies: async () => { throw new Error("permission denied"); },
+ sendNativeMessage: async (message) => {
+ calls.push(["send", message]);
+ return { ok: true };
+ },
+ clearBadge: async () => {},
+ showSuccessBadge: async () => {},
+ openErrorPage: async () => {},
+ mapChromeErrorCode: () => "LAUNCH_FAILED",
+ });
+
+ assert.equal(calls[0][1].cookies, undefined);
+});
+
test("does not show success feedback when the native host returns an error response", async () => {
const calls = [];
diff --git a/browser-extension/chrome/tests/cookies.test.mjs b/browser-extension/chrome/tests/cookies.test.mjs
new file mode 100644
index 00000000..25200a24
--- /dev/null
+++ b/browser-extension/chrome/tests/cookies.test.mjs
@@ -0,0 +1,131 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+import { extractCookiesForPlatform } from "../src/cookies.js";
+
+function mockGetAllCookies(cookieStore) {
+ return async ({ domain }) => cookieStore[domain] ?? [];
+}
+
+const FAKE_COOKIES = {
+ ".youtube.com": [
+ {
+ domain: ".youtube.com",
+ httpOnly: true,
+ path: "/",
+ secure: true,
+ expirationDate: 1735689600,
+ name: "LOGIN_INFO",
+ value: "fake-login-info",
+ },
+ {
+ domain: ".youtube.com",
+ httpOnly: false,
+ path: "/",
+ secure: true,
+ expirationDate: 1735689600,
+ name: "PREF",
+ value: "tz=Europe.Paris",
+ },
+ ],
+ ".google.com": [
+ {
+ domain: ".google.com",
+ httpOnly: true,
+ path: "/",
+ secure: true,
+ expirationDate: 1735689600,
+ name: "SID",
+ value: "fake-sid",
+ },
+ ],
+};
+
+test("extracts cookies for a known platform", async () => {
+ const cookies = await extractCookiesForPlatform(
+ "youtube",
+ mockGetAllCookies(FAKE_COOKIES)
+ );
+
+ assert.ok(cookies);
+ assert.equal(cookies.length, 3);
+ assert.equal(cookies[0].name, "LOGIN_INFO");
+ assert.equal(cookies[0].httpOnly, true);
+ assert.equal(cookies[2].name, "SID");
+ assert.equal(cookies[2].domain, ".google.com");
+});
+
+test("converts expirationDate to integer expires field", async () => {
+ const cookies = await extractCookiesForPlatform(
+ "youtube",
+ mockGetAllCookies(FAKE_COOKIES)
+ );
+
+ for (const c of cookies) {
+ assert.equal(typeof c.expires, "number");
+ assert.equal(c.expires, Math.floor(c.expires));
+ }
+});
+
+test("returns null for unknown platforms", async () => {
+ const cookies = await extractCookiesForPlatform(
+ "hotmart",
+ mockGetAllCookies(FAKE_COOKIES)
+ );
+
+ assert.equal(cookies, null);
+});
+
+test("returns null when no cookies exist for the platform", async () => {
+ const cookies = await extractCookiesForPlatform(
+ "twitch",
+ mockGetAllCookies({})
+ );
+
+ assert.equal(cookies, null);
+});
+
+test("handles session cookies with no expirationDate", async () => {
+ const cookies = await extractCookiesForPlatform(
+ "reddit",
+ mockGetAllCookies({
+ ".reddit.com": [
+ {
+ domain: ".reddit.com",
+ httpOnly: false,
+ path: "/",
+ secure: false,
+ name: "session_tracker",
+ value: "abc",
+ },
+ ],
+ })
+ );
+
+ assert.ok(cookies);
+ assert.equal(cookies[0].expires, 0);
+});
+
+test("extracts cookies from multiple domains for twitter", async () => {
+ const cookies = await extractCookiesForPlatform(
+ "twitter",
+ mockGetAllCookies({
+ ".twitter.com": [
+ { domain: ".twitter.com", httpOnly: false, path: "/", secure: true, expirationDate: 999, name: "ct0", value: "tok1" },
+ ],
+ ".x.com": [
+ { domain: ".x.com", httpOnly: false, path: "/", secure: true, expirationDate: 999, name: "auth_token", value: "tok2" },
+ ],
+ })
+ );
+
+ assert.ok(cookies);
+ assert.equal(cookies.length, 2);
+ assert.equal(cookies[0].domain, ".twitter.com");
+ assert.equal(cookies[1].domain, ".x.com");
+});
+
+test("uses default fallback when chrome.cookies is unavailable", async () => {
+ const cookies = await extractCookiesForPlatform("youtube");
+ assert.equal(cookies, null);
+});
diff --git a/browser-extension/chrome/tests/manifest.test.mjs b/browser-extension/chrome/tests/manifest.test.mjs
index c00e079b..9b4e7541 100644
--- a/browser-extension/chrome/tests/manifest.test.mjs
+++ b/browser-extension/chrome/tests/manifest.test.mjs
@@ -19,3 +19,16 @@ test("declares the 48px toolbar icon for the inactive action state", async () =>
assert.equal(manifest.action.default_icon["48"], "icons/inactive-48.png");
});
+
+test("declares cookies permission for cookie forwarding", async () => {
+ const manifest = await readManifest();
+
+ assert.ok(manifest.permissions.includes("cookies"));
+});
+
+test("declares host_permissions for supported platforms", async () => {
+ const manifest = await readManifest();
+
+ assert.ok(manifest.host_permissions.length > 0);
+ assert.ok(manifest.host_permissions.includes("*://*.youtube.com/*"));
+});
diff --git a/src-tauri/src/core/ytdlp.rs b/src-tauri/src/core/ytdlp.rs
index 8835e580..a70f059f 100644
--- a/src-tauri/src/core/ytdlp.rs
+++ b/src-tauri/src/core/ytdlp.rs
@@ -346,6 +346,20 @@ async fn find_ffmpeg_location_cached() -> Option<String> {
result
}
+fn extension_cookie_file() -> Option<std::path::PathBuf> {
+ let path = crate::native_host::extension_cookie_file_path();
+ if !path.exists() {
+ return None;
+ }
+ let metadata = std::fs::metadata(&path).ok()?;
+ let modified = metadata.modified().ok()?;
+ if modified.elapsed().unwrap_or_default() < std::time::Duration::from_secs(86400) {
+ Some(path)
+ } else {
+ None
+ }
+}
+
fn detect_cookies_browser() -> Option<String> {
let _timer_start = std::time::Instant::now();
let result = (|| -> Option<String> {
@@ -584,10 +598,16 @@ pub async fn get_video_info(
args.push(extractor_args.to_string());
}
- let browser_cookies = detect_cookies_browser_cached().await;
- if let Some(ref browser) = browser_cookies {
- args.push("--cookies-from-browser".to_string());
- args.push(browser.clone());
+ let extension_cookies = extension_cookie_file();
+ if let Some(ref cf) = extension_cookies {
+ args.push("--cookies".to_string());
+ args.push(cf.to_string_lossy().to_string());
+ } else {
+ let browser_cookies = detect_cookies_browser_cached().await;
+ if let Some(ref browser) = browser_cookies {
+ args.push("--cookies-from-browser".to_string());
+ args.push(browser.clone());
+ }
}
args.extend(proxy_args());
@@ -920,7 +940,13 @@ pub async fn download_video(
}
};
- let browser_cookies = if cookie_file.is_none() && global_cookie_file.is_none() {
+ let ext_cookies = if cookie_file.is_none() && global_cookie_file.is_none() {
+ extension_cookie_file()
+ } else {
+ None
+ };
+
+ let browser_cookies = if cookie_file.is_none() && global_cookie_file.is_none() && ext_cookies.is_none() {
detect_cookies_browser_cached().await
} else {
None
@@ -928,7 +954,8 @@ pub async fn download_video(
let effective_cookie_file = cookie_file
.map(|p| p.to_path_buf())
- .or_else(|| global_cookie_file.map(std::path::PathBuf::from));
+ .or_else(|| global_cookie_file.map(std::path::PathBuf::from))
+ .or(ext_cookies);
let mut base_args = vec!["-f".to_string(), format_selector];
if format_id.is_none() {
diff --git a/src-tauri/src/native_host.rs b/src-tauri/src/native_host.rs
index 08917913..7dd2bfc7 100644
--- a/src-tauri/src/native_host.rs
+++ b/src-tauri/src/native_host.rs
@@ -26,6 +26,20 @@ struct NativeHostRequest {
#[serde(rename = "type")]
kind: String,
url: String,
+ #[serde(default)]
+ cookies: Option<Vec<NativeCookie>>,
+}
+
+#[derive(Debug, Deserialize)]
+struct NativeCookie {
+ domain: String,
+ #[serde(rename = "httpOnly")]
+ http_only: bool,
+ path: String,
+ secure: bool,
+ expires: i64,
+ name: String,
+ value: String,
}
#[derive(Debug, Serialize)]
@@ -229,6 +243,47 @@ fn should_copy_exe(source: &Path, dest: &Path) -> bool {
src_meta.len() != dst_meta.len()
}
+pub fn extension_cookie_file_path() -> PathBuf {
+ crate::core::paths::app_data_dir()
+ .unwrap_or_else(|| PathBuf::from("."))
+ .join("chrome-extension-cookies.txt")
+}
+
+fn write_extension_cookies(cookies: &[NativeCookie]) -> anyhow::Result<()> {
+ let path = extension_cookie_file_path();
+ if let Some(parent) = path.parent() {
+ fs::create_dir_all(parent)?;
+ }
+
+ let mut content = String::from("# Netscape HTTP Cookie File\n");
+ for c in cookies {
+ let http_only_prefix = if c.http_only { "#HttpOnly_" } else { "" };
+ let include_subdomains = if c.domain.starts_with('.') { "TRUE" } else { "FALSE" };
+ let secure = if c.secure { "TRUE" } else { "FALSE" };
+ content.push_str(&format!(
+ "{}{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
+ http_only_prefix,
+ c.domain,
+ include_subdomains,
+ c.path,
+ secure,
+ c.expires,
+ c.name,
+ c.value,
+ ));
+ }
+
+ fs::write(&path, content)?;
+
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?;
+ }
+
+ Ok(())
+}
+
fn handle_request(request: NativeHostRequest) -> NativeHostResponse {
if request.kind != "enqueue" {
return NativeHostResponse {
@@ -246,6 +301,14 @@ fn handle_request(request: NativeHostRequest) -> NativeHostResponse {
};
}
+ if let Some(ref cookies) = request.cookies {
+ if !cookies.is_empty() {
+ if let Err(e) = write_extension_cookies(cookies) {
+ eprintln!("[OmniGet] Warning: failed to write extension cookies: {e}");
+ }
+ }
+ }
+
match launch_omniget(&request.url) {
Ok(()) => NativeHostResponse {
ok: true,