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.

fix: parse embedded URLs in share text across omnibox and CLI (fixes #155)

IGECT88e081a97b32
diff --git a/src-tauri/omniget-cli/src/commands/common.rs b/src-tauri/omniget-cli/src/commands/common.rs
index 8b30cafa..abb3e676 100644
--- a/src-tauri/omniget-cli/src/commands/common.rs
+++ b/src-tauri/omniget-cli/src/commands/common.rs
@@ -98,10 +98,48 @@ pub fn core_platform_registry() -> PlatformRegistry {
registry
}
+fn is_cjk_or_punct(c: char) -> bool {
+ matches!(
+ c,
+ '\u{3000}'..='\u{303F}'
+ | '\u{4E00}'..='\u{9FFF}'
+ | '\u{FF01}'..='\u{FF5E}'
+ | '^' | '<' | '>'
+ )
+}
+
+pub fn extract_url(input: &str) -> &str {
+ let trimmed = input.trim();
+ if trimmed.starts_with("http://")
+ || trimmed.starts_with("https://")
+ || trimmed.starts_with("magnet:")
+ || trimmed.starts_with("p2p:")
+ {
+ return trimmed;
+ }
+
+ for prefix in &["https://", "http://", "magnet:?xt=", "p2p:"] {
+ if let Some(start_idx) = trimmed.find(prefix) {
+ let slice = &trimmed[start_idx..];
+ let end_idx = slice
+ .find(|c: char| c.is_whitespace() || is_cjk_or_punct(c))
+ .unwrap_or(slice.len());
+ let candidate = &slice[..end_idx];
+ let cleaned = candidate
+ .trim_end_matches(&['.', ',', ';', ':', '!', '?', '\'', '"', ')', ']', '>'][..]);
+ if !cleaned.is_empty() {
+ return cleaned;
+ }
+ }
+ }
+ trimmed
+}
+
pub async fn resolve_media_info(
registry: &PlatformRegistry,
- url: &str,
+ raw_url: &str,
) -> Result<(Arc<dyn PlatformDownloader>, MediaInfo)> {
+ let url = extract_url(raw_url);
let platform = registry
.find_platform(url)
.unwrap_or_else(|| Arc::new(GenericYtdlpDownloader::new()));
@@ -293,4 +331,27 @@ mod tests {
assert_eq!(o.audio_format.as_deref(), Some("mp3"));
assert_eq!(o.custom_ytdlp_args, None);
}
+
+ #[test]
+ fn extract_url_from_share_text() {
+ assert_eq!(
+ extract_url("https://www.bilibili.com/video/BV1GJ411x7h7"),
+ "https://www.bilibili.com/video/BV1GJ411x7h7"
+ );
+ assert_eq!(
+ extract_url(
+ "复制打开抖音,看看这个 https://www.bilibili.com/video/BV1GJ411x7h7 复制此链接"
+ ),
+ "https://www.bilibili.com/video/BV1GJ411x7h7"
+ );
+ assert_eq!(
+ extract_url("Check this: https://example.com/video.mp4!"),
+ "https://example.com/video.mp4"
+ );
+ assert_eq!(
+ extract_url("看作品https://v.douyin.com/xyz/,复制"),
+ "https://v.douyin.com/xyz/"
+ );
+ assert_eq!(extract_url("regular search query"), "regular search query");
+ }
}
diff --git a/src-tauri/omniget-cli/src/commands/download.rs b/src-tauri/omniget-cli/src/commands/download.rs
index 1855cc88..b55292dc 100644
--- a/src-tauri/omniget-cli/src/commands/download.rs
+++ b/src-tauri/omniget-cli/src/commands/download.rs
@@ -35,6 +35,7 @@ pub async fn execute(
proxy: Option<String>,
) -> Result<()> {
common::init_cli_runtime(proxy.as_deref())?;
+ let url = common::extract_url(&url).to_string();
let json_mode = output::is_json_mode();
let pb = ProgressBar::new(100);
diff --git a/src-tauri/omniget-cli/src/commands/info.rs b/src-tauri/omniget-cli/src/commands/info.rs
index 4f8afd8a..3032a5e4 100644
--- a/src-tauri/omniget-cli/src/commands/info.rs
+++ b/src-tauri/omniget-cli/src/commands/info.rs
@@ -5,6 +5,7 @@ use crate::output;
pub async fn execute(url: String, proxy: Option<String>) -> Result<()> {
common::init_cli_runtime(proxy.as_deref())?;
+ let url = common::extract_url(&url).to_string();
let registry = common::core_platform_registry();
let (platform, info) = common::resolve_media_info(&registry, &url).await?;
diff --git a/src/lib/home/omnibox-controller.ts b/src/lib/home/omnibox-controller.ts
index b55d3275..762f0cc2 100644
--- a/src/lib/home/omnibox-controller.ts
+++ b/src/lib/home/omnibox-controller.ts
@@ -1,61 +1,95 @@
-export type OmniState =
- | { kind: "idle" }
- | { kind: "detecting" }
- | { kind: "detected"; info: PlatformInfo }
- | { kind: "unsupported" }
- | { kind: "preparing"; platform: string }
- | { kind: "batch"; urls: string[] }
- | { kind: "searching" }
- | { kind: "search-results"; results: SearchResult[] }
- | { kind: "search-empty" }
- | { kind: "error"; message: string; originalUrl: string; platform: string };
-
-export type PlatformInfo = {
- platform: string;
- supported: boolean;
- content_id: string | null;
- content_type: string | null;
-};
-
-export type SearchResult = {
- id: string;
- title: string;
- author: string;
- duration: number | null;
- thumbnail_url: string | null;
- url: string;
- platform: string;
-};
-
-export type HomeInputMode = "url" | "batch" | "torrent" | "p2p";
-
-export function isUrl(value: string): boolean {
- return (
- value.startsWith("http://") ||
- value.startsWith("https://") ||
- value.startsWith("magnet:") ||
- value.startsWith("p2p:") ||
- value.endsWith(".torrent")
- );
-}
-
-export function showInspectorForState(state: OmniState): boolean {
- return (
- state.kind === "detected" ||
- state.kind === "preparing" ||
- state.kind === "error"
- );
-}
-
-export function showOmniboxForState(state: OmniState): boolean {
- return (
- state.kind === "idle" ||
- state.kind === "detecting" ||
- state.kind === "detected" ||
- state.kind === "unsupported" ||
- state.kind === "batch" ||
- state.kind === "searching" ||
- state.kind === "search-results" ||
- state.kind === "search-empty"
- );
-}
+export type OmniState =
+ | { kind: "idle" }
+ | { kind: "detecting" }
+ | { kind: "detected"; info: PlatformInfo }
+ | { kind: "unsupported" }
+ | { kind: "preparing"; platform: string }
+ | { kind: "batch"; urls: string[] }
+ | { kind: "searching" }
+ | { kind: "search-results"; results: SearchResult[] }
+ | { kind: "search-empty" }
+ | { kind: "error"; message: string; originalUrl: string; platform: string };
+
+export type PlatformInfo = {
+ platform: string;
+ supported: boolean;
+ content_id: string | null;
+ content_type: string | null;
+};
+
+export type SearchResult = {
+ id: string;
+ title: string;
+ author: string;
+ duration: number | null;
+ thumbnail_url: string | null;
+ url: string;
+ platform: string;
+};
+
+export type HomeInputMode = "url" | "batch" | "torrent" | "p2p";
+
+export function isUrl(value: string): boolean {
+ return (
+ value.startsWith("http://") ||
+ value.startsWith("https://") ||
+ value.startsWith("magnet:") ||
+ value.startsWith("p2p:") ||
+ value.endsWith(".torrent")
+ );
+}
+
+const URL_SCHEME_REGEX =
+ /(?:https?:\/\/[^\s\u3000\u4e00-\u9fff,。!?、“”‘’()《》〈〉【】^<>]+|magnet:\?xt=[^\s]+|p2p:[^\s]+)/gi;
+const TRAILING_PUNCT_REGEX = /[.,;:!?'")\]>]+$/;
+
+export function extractUrls(text: string): string[] {
+ const trimmed = text.trim();
+ if (!trimmed) return [];
+
+ if (isUrl(trimmed)) {
+ return [trimmed];
+ }
+
+ const matches = trimmed.match(URL_SCHEME_REGEX) ?? [];
+ const results: string[] = [];
+
+ for (const m of matches) {
+ const cleaned = m.replace(TRAILING_PUNCT_REGEX, "").trim();
+ if (cleaned && isUrl(cleaned) && !results.includes(cleaned)) {
+ results.push(cleaned);
+ }
+ }
+
+ if (results.length === 0) {
+ const tokens = trimmed.split(/[\s\n]+/).filter(isUrl);
+ for (const t of tokens) {
+ if (!results.includes(t)) {
+ results.push(t);
+ }
+ }
+ }
+
+ return results;
+}
+
+export function showInspectorForState(state: OmniState): boolean {
+ return (
+ state.kind === "detected" ||
+ state.kind === "preparing" ||
+ state.kind === "error"
+ );
+}
+
+export function showOmniboxForState(state: OmniState): boolean {
+ return (
+ state.kind === "idle" ||
+ state.kind === "detecting" ||
+ state.kind === "detected" ||
+ state.kind === "unsupported" ||
+ state.kind === "batch" ||
+ state.kind === "searching" ||
+ state.kind === "search-results" ||
+ state.kind === "search-empty"
+ );
+}
diff --git a/src/lib/home/omnibox-url.test.ts b/src/lib/home/omnibox-url.test.ts
new file mode 100644
index 00000000..224d6c4f
--- /dev/null
+++ b/src/lib/home/omnibox-url.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it } from "vitest";
+import { extractUrls, isUrl } from "./omnibox-controller";
+
+describe("omnibox URL detection and extraction", () => {
+ it("detects clean URLs directly", () => {
+ expect(isUrl("https://example.com/video")).toBe(true);
+ expect(extractUrls("https://example.com/video")).toEqual(["https://example.com/video"]);
+ });
+
+ it("extracts embedded URL from Chinese mobile share text", () => {
+ const shareText =
+ "8.20 复制打开抖音极速版,看看【xxx的作品】https://v.douyin.com/XXXXXX/ 复制此链接,打开Dou音搜索";
+ expect(extractUrls(shareText)).toEqual(["https://v.douyin.com/XXXXXX/"]);
+ });
+
+ it("extracts URL when followed immediately by Chinese punctuation without space", () => {
+ const text = "来看看这个好玩的作品https://v.douyin.com/XXXXXX/,复制本段信息打开app";
+ expect(extractUrls(text)).toEqual(["https://v.douyin.com/XXXXXX/"]);
+ });
+
+ it("strips trailing sentence-ending punctuation from URL", () => {
+ const text = "Check out this video: https://www.bilibili.com/video/BV1GJ411x7h7.";
+ expect(extractUrls(text)).toEqual(["https://www.bilibili.com/video/BV1GJ411x7h7"]);
+ });
+
+ it("extracts multiple URLs for batch processing", () => {
+ const text = "Here are two links:\nhttps://example.com/1\nand https://example.com/2";
+ expect(extractUrls(text)).toEqual(["https://example.com/1", "https://example.com/2"]);
+ });
+
+ it("returns empty array for pure search query", () => {
+ expect(extractUrls("funny cat videos 2026")).toEqual([]);
+ expect(extractUrls("tutorial for beginners")).toEqual([]);
+ });
+
+ it("handles magnet links and p2p links", () => {
+ expect(extractUrls("Magnet link: magnet:?xt=urn:btih:123456789abcdef enjoy")).toEqual([
+ "magnet:?xt=urn:btih:123456789abcdef",
+ ]);
+ });
+});
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index 336d7f38..e7b5eea5 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -26,6 +26,7 @@
showInspectorForState,
showOmniboxForState,
isUrl,
+ extractUrls,
} from "$lib/home/omnibox-controller";
import { getDownloads, formatBytes } from "$lib/stores/download-store.svelte";
import { getDownloadStats } from "$lib/stores/download-stats.svelte";
@@ -402,20 +403,24 @@
return;
}
- const urls = trimmed.split(/[\s\n]+/).filter(isUrl);
+ const urls = extractUrls(trimmed);
if (urls.length > 1) {
omniState = { kind: "batch", urls };
return;
}
- if (isUrl(trimmed)) {
+ if (urls.length === 1) {
+ const targetUrl = urls[0];
+ if (url !== targetUrl) {
+ url = targetUrl;
+ }
omniState = { kind: "detecting" };
if (getSettings()?.download.auto_download_on_paste) {
pendingAutoDownload = true;
}
debounceTimer = setTimeout(() => {
- detectPlatform(trimmed);
+ detectPlatform(targetUrl);
}, 500);
return;
}