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): localize tooltips and register native host

InstaZDLLe6d6dfd58fca
diff --git a/browser-extension/chrome/README.md b/browser-extension/chrome/README.md
index 1c78e780..ea4afbbb 100644
--- a/browser-extension/chrome/README.md
+++ b/browser-extension/chrome/README.md
@@ -1,15 +1,17 @@
# OmniGet Chrome Extension
-Windows-first MV3 extension for sending supported media pages to OmniGet.
+MV3 extension for sending supported media pages to OmniGet.
## Load it locally
-1. Install OmniGet and launch it once on Windows so it can register the Chrome native host.
+1. Install OmniGet and launch it once on Windows, macOS, or Linux so it can register the Chrome native host for your user profile.
2. Open `chrome://extensions`.
3. Enable `Developer mode`.
4. Click `Load unpacked`.
5. Select this folder: [`browser-extension/chrome`](/E:/Workspace/omniget/browser-extension/chrome)
+On macOS and Linux, that first launch writes Chrome's native messaging manifest into your user profile automatically.
+
The unpacked extension keeps a stable ID through the committed manifest key:
`dkjelkhaaakffpghdfalobccaaipajip`
@@ -24,5 +26,5 @@ The unpacked extension keeps a stable ID through the committed manifest key:
## Quick test
```powershell
-node --test browser-extension/chrome/tests/detect.test.mjs
+node --test browser-extension/chrome/tests/detect.test.mjs browser-extension/chrome/tests/action-title.test.mjs
```
diff --git a/browser-extension/chrome/_locales/el/messages.json b/browser-extension/chrome/_locales/el/messages.json
new file mode 100644
index 00000000..4a499585
--- /dev/null
+++ b/browser-extension/chrome/_locales/el/messages.json
@@ -0,0 +1,8 @@
+{
+ "action_title_supported": {
+ "message": "Στείλτε αυτήν τη σελίδα πολυμέσων στο OmniGet"
+ },
+ "action_title_unsupported": {
+ "message": "Δεν εντοπίστηκαν υποστηριζόμενα πολυμέσα σε αυτήν τη σελίδα"
+ }
+}
diff --git a/browser-extension/chrome/_locales/en/messages.json b/browser-extension/chrome/_locales/en/messages.json
new file mode 100644
index 00000000..1d631eba
--- /dev/null
+++ b/browser-extension/chrome/_locales/en/messages.json
@@ -0,0 +1,8 @@
+{
+ "action_title_supported": {
+ "message": "Send this media page to OmniGet"
+ },
+ "action_title_unsupported": {
+ "message": "No supported media detected on this page"
+ }
+}
diff --git a/browser-extension/chrome/_locales/fr/messages.json b/browser-extension/chrome/_locales/fr/messages.json
new file mode 100644
index 00000000..c2f919a1
--- /dev/null
+++ b/browser-extension/chrome/_locales/fr/messages.json
@@ -0,0 +1,8 @@
+{
+ "action_title_supported": {
+ "message": "Envoyer cette page multimedia vers OmniGet"
+ },
+ "action_title_unsupported": {
+ "message": "Aucun media pris en charge detecte sur cette page"
+ }
+}
diff --git a/browser-extension/chrome/_locales/it/messages.json b/browser-extension/chrome/_locales/it/messages.json
new file mode 100644
index 00000000..b6f22fea
--- /dev/null
+++ b/browser-extension/chrome/_locales/it/messages.json
@@ -0,0 +1,8 @@
+{
+ "action_title_supported": {
+ "message": "Invia questa pagina multimediale a OmniGet"
+ },
+ "action_title_unsupported": {
+ "message": "Nessun contenuto multimediale supportato rilevato in questa pagina"
+ }
+}
diff --git a/browser-extension/chrome/_locales/ja/messages.json b/browser-extension/chrome/_locales/ja/messages.json
new file mode 100644
index 00000000..7dc902d0
--- /dev/null
+++ b/browser-extension/chrome/_locales/ja/messages.json
@@ -0,0 +1,8 @@
+{
+ "action_title_supported": {
+ "message": "このメディアページをOmniGetに送信"
+ },
+ "action_title_unsupported": {
+ "message": "このページでは対応しているメディアが見つかりません"
+ }
+}
diff --git a/browser-extension/chrome/_locales/pt/messages.json b/browser-extension/chrome/_locales/pt/messages.json
new file mode 100644
index 00000000..34fe9237
--- /dev/null
+++ b/browser-extension/chrome/_locales/pt/messages.json
@@ -0,0 +1,8 @@
+{
+ "action_title_supported": {
+ "message": "Enviar esta pagina de midia para o OmniGet"
+ },
+ "action_title_unsupported": {
+ "message": "Nenhuma midia compativel detectada nesta pagina"
+ }
+}
diff --git a/browser-extension/chrome/_locales/zh_CN/messages.json b/browser-extension/chrome/_locales/zh_CN/messages.json
new file mode 100644
index 00000000..a34f9d23
--- /dev/null
+++ b/browser-extension/chrome/_locales/zh_CN/messages.json
@@ -0,0 +1,8 @@
+{
+ "action_title_supported": {
+ "message": "将此媒体页面发送到 OmniGet"
+ },
+ "action_title_unsupported": {
+ "message": "此页面未检测到受支持的媒体"
+ }
+}
diff --git a/browser-extension/chrome/_locales/zh_TW/messages.json b/browser-extension/chrome/_locales/zh_TW/messages.json
new file mode 100644
index 00000000..34823f65
--- /dev/null
+++ b/browser-extension/chrome/_locales/zh_TW/messages.json
@@ -0,0 +1,8 @@
+{
+ "action_title_supported": {
+ "message": "將此媒體頁面傳送到 OmniGet"
+ },
+ "action_title_unsupported": {
+ "message": "此頁面未偵測到支援的媒體"
+ }
+}
diff --git a/browser-extension/chrome/manifest.json b/browser-extension/chrome/manifest.json
index 3fc200b4..d0fdf1d1 100644
--- a/browser-extension/chrome/manifest.json
+++ b/browser-extension/chrome/manifest.json
@@ -3,6 +3,7 @@
"name": "OmniGet",
"description": "Detect supported media pages and send them to OmniGet with one click.",
"version": "0.1.0",
+ "default_locale": "en",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0ribk9X5HR4XBloFk2g/V6kjICK8XWYC/QqrGFTOjKkRhRNDJr8Z14JFLggvWBbrj0D/zYdd5CfHJG01bGWMZC0hxRtm71q5Pbu1jtChEfE4QO/xWvM8WfrXH895nJFcUzxi137eoWq+FtSGxqFnSl6IXg5EZdgYH5LN3t6kL0HTwKnJLB3/8d4JD1uF2tQxHzii+MosIozKyDatqtEaBLprY1gfAfCwfY8Iqr9yehFUm7IDH+TGwjE17GJY1ZhfkpNBRmH/h23hwyk6coocSsWpM9GbCFdBX22iUnZjxZqACO1n+8xw2364Lznqtuot4iawznQGF4ALL7CbIvHPOQIDAQAB",
"permissions": [
"nativeMessaging",
diff --git a/browser-extension/chrome/src/action-title.js b/browser-extension/chrome/src/action-title.js
new file mode 100644
index 00000000..5f213a15
--- /dev/null
+++ b/browser-extension/chrome/src/action-title.js
@@ -0,0 +1,16 @@
+const ACTION_TITLE_MESSAGES = Object.freeze({
+ action_title_supported: "Send this media page to OmniGet",
+ action_title_unsupported: "No supported media detected on this page",
+});
+
+function defaultGetMessage(name) {
+ if (typeof chrome === "undefined" || !chrome.i18n?.getMessage) {
+ return "";
+ }
+ return chrome.i18n.getMessage(name);
+}
+
+export function resolveActionTitle(supported, getMessage = defaultGetMessage) {
+ const key = supported ? "action_title_supported" : "action_title_unsupported";
+ return getMessage(key) || ACTION_TITLE_MESSAGES[key];
+}
diff --git a/browser-extension/chrome/src/background.js b/browser-extension/chrome/src/background.js
index ffba1fe7..fdd49ee0 100644
--- a/browser-extension/chrome/src/background.js
+++ b/browser-extension/chrome/src/background.js
@@ -1,4 +1,5 @@
import { detectSupportedMediaUrl } from "./detect.js";
+import { resolveActionTitle } from "./action-title.js";
const HOST_NAME = "wtf.tonho.omniget";
const INSTALL_URL = "https://github.com/tonhowtf/omniget/releases/latest";
@@ -101,12 +102,9 @@ async function refreshTabAction(tabId, tab) {
}
try {
- // TODO: use chrome.i18n via _locales/ for multi-language tooltips
await chrome.action.setTitle({
tabId,
- title: supported
- ? "Send this media page to OmniGet"
- : "No supported media detected on this page",
+ title: resolveActionTitle(supported),
});
} catch (error) {
console.error("[OmniGet] Failed to set title:", error);
diff --git a/browser-extension/chrome/tests/action-title.test.mjs b/browser-extension/chrome/tests/action-title.test.mjs
new file mode 100644
index 00000000..51877087
--- /dev/null
+++ b/browser-extension/chrome/tests/action-title.test.mjs
@@ -0,0 +1,25 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+import { resolveActionTitle } from "../src/action-title.js";
+
+test("uses localized title for supported pages", () => {
+ const title = resolveActionTitle(true, (key) =>
+ key === "action_title_supported" ? "Envoyer cette page multimedia vers OmniGet" : ""
+ );
+
+ assert.equal(title, "Envoyer cette page multimedia vers OmniGet");
+});
+
+test("uses localized title for unsupported pages", () => {
+ const title = resolveActionTitle(false, (key) =>
+ key === "action_title_unsupported" ? "Aucun media pris en charge detecte sur cette page" : ""
+ );
+
+ assert.equal(title, "Aucun media pris en charge detecte sur cette page");
+});
+
+test("falls back to English when a locale key is missing", () => {
+ assert.equal(resolveActionTitle(true, () => ""), "Send this media page to OmniGet");
+ assert.equal(resolveActionTitle(false, () => ""), "No supported media detected on this page");
+});
diff --git a/src-tauri/src/native_host.rs b/src-tauri/src/native_host.rs
index 14a8deb2..ef0dbae4 100644
--- a/src-tauri/src/native_host.rs
+++ b/src-tauri/src/native_host.rs
@@ -2,12 +2,18 @@ use std::fs;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
+#[cfg(any(target_os = "linux", target_os = "macos"))]
+use anyhow::Context;
use serde::{Deserialize, Serialize};
pub const CHROME_HOST_NAME: &str = "wtf.tonho.omniget";
pub const CHROME_EXTENSION_ID: &str = "dkjelkhaaakffpghdfalobccaaipajip";
+#[cfg(target_os = "windows")]
const HOST_COPY_NAME: &str = "omniget-native-host.exe";
+#[cfg(not(target_os = "windows"))]
+const HOST_COPY_NAME: &str = "omniget-native-host";
+const HOST_BINARY_STEM: &str = "omniget-native-host";
const HOST_CONFIG_NAME: &str = "native-host-config.json";
const HOST_MANIFEST_NAME: &str = "wtf.tonho.omniget.json";
@@ -36,7 +42,7 @@ pub fn should_run_as_native_host() -> bool {
std::env::current_exe()
.ok()
.and_then(|path| path.file_stem().map(|stem| stem.to_string_lossy().to_string()))
- .map(|stem| stem.eq_ignore_ascii_case("omniget-native-host"))
+ .map(|stem| stem.eq_ignore_ascii_case(HOST_BINARY_STEM))
.unwrap_or(false)
}
@@ -47,30 +53,57 @@ pub fn run_native_host() -> anyhow::Result<()> {
Ok(())
}
-#[cfg(target_os = "windows")]
pub fn ensure_registered() -> anyhow::Result<()> {
- use std::os::windows::process::CommandExt;
-
let current_exe = std::env::current_exe()?;
- let integration_dir = crate::core::paths::app_data_dir()
- .unwrap_or_else(|| PathBuf::from("."))
- .join("chrome-native-host");
+ let integration_dir = chrome_integration_dir();
fs::create_dir_all(&integration_dir)?;
let host_exe = integration_dir.join(HOST_COPY_NAME);
- if current_exe != host_exe && should_copy_exe(&current_exe, &host_exe) {
- fs::copy(&current_exe, &host_exe)?;
- }
+ copy_host_exe(&current_exe, &host_exe)?;
let config_path = integration_dir.join(HOST_CONFIG_NAME);
- let manifest_path = integration_dir.join(HOST_MANIFEST_NAME);
+ write_host_config(&config_path, &current_exe)?;
+
+ let manifest_path = chrome_manifest_path(&integration_dir)?;
+ if let Some(parent) = manifest_path.parent() {
+ fs::create_dir_all(parent)?;
+ }
+
+ write_host_manifest(&manifest_path, &host_exe)?;
+ register_host_manifest(&manifest_path)?;
+ Ok(())
+}
+
+fn chrome_integration_dir() -> PathBuf {
+ crate::core::paths::app_data_dir()
+ .unwrap_or_else(|| PathBuf::from("."))
+ .join("chrome-native-host")
+}
+
+fn copy_host_exe(source: &Path, dest: &Path) -> anyhow::Result<()> {
+ if source != dest && should_copy_exe(source, dest) {
+ fs::copy(source, dest)?;
+ sync_host_permissions(source, dest)?;
+ }
+ Ok(())
+}
+
+fn write_host_config(config_path: &Path, app_path: &Path) -> anyhow::Result<()> {
let config = NativeHostConfig {
- app_path: current_exe.to_string_lossy().to_string(),
+ app_path: app_path.to_string_lossy().to_string(),
};
- fs::write(&config_path, serde_json::to_vec_pretty(&config)?)?;
+ fs::write(config_path, serde_json::to_vec_pretty(&config)?)?;
+ Ok(())
+}
- let manifest = serde_json::json!({
+fn write_host_manifest(manifest_path: &Path, host_exe: &Path) -> anyhow::Result<()> {
+ fs::write(manifest_path, serde_json::to_vec_pretty(&build_host_manifest(host_exe))?)?;
+ Ok(())
+}
+
+fn build_host_manifest(host_exe: &Path) -> serde_json::Value {
+ serde_json::json!({
"name": CHROME_HOST_NAME,
"description": "OmniGet native host for Chrome",
"path": host_exe.to_string_lossy().to_string(),
@@ -78,8 +111,57 @@ pub fn ensure_registered() -> anyhow::Result<()> {
"allowed_origins": [
format!("chrome-extension://{}/", CHROME_EXTENSION_ID)
]
- });
- fs::write(&manifest_path, serde_json::to_vec_pretty(&manifest)?)?;
+ })
+}
+
+#[cfg(target_os = "windows")]
+fn chrome_manifest_path(integration_dir: &Path) -> anyhow::Result<PathBuf> {
+ Ok(chrome_manifest_path_from_base(integration_dir))
+}
+
+#[cfg(target_os = "linux")]
+fn chrome_manifest_path(_integration_dir: &Path) -> anyhow::Result<PathBuf> {
+ let base_dir = dirs::config_dir()
+ .context("Could not resolve the Linux config directory for Chrome native host registration")?;
+ Ok(chrome_manifest_path_from_base(&base_dir))
+}
+
+#[cfg(target_os = "macos")]
+fn chrome_manifest_path(_integration_dir: &Path) -> anyhow::Result<PathBuf> {
+ let base_dir = dirs::data_dir().context(
+ "Could not resolve the macOS Application Support directory for Chrome native host registration",
+ )?;
+ Ok(chrome_manifest_path_from_base(&base_dir))
+}
+
+#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
+fn chrome_manifest_path(_integration_dir: &Path) -> anyhow::Result<PathBuf> {
+ anyhow::bail!("Chrome native host registration is unsupported on this platform");
+}
+
+#[cfg(target_os = "windows")]
+fn chrome_manifest_path_from_base(base: &Path) -> PathBuf {
+ base.join(HOST_MANIFEST_NAME)
+}
+
+#[cfg(target_os = "linux")]
+fn chrome_manifest_path_from_base(base: &Path) -> PathBuf {
+ base.join("google-chrome")
+ .join("NativeMessagingHosts")
+ .join(HOST_MANIFEST_NAME)
+}
+
+#[cfg(target_os = "macos")]
+fn chrome_manifest_path_from_base(base: &Path) -> PathBuf {
+ base.join("Google")
+ .join("Chrome")
+ .join("NativeMessagingHosts")
+ .join(HOST_MANIFEST_NAME)
+}
+
+#[cfg(target_os = "windows")]
+fn register_host_manifest(manifest_path: &Path) -> anyhow::Result<()> {
+ use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x08000000;
let status = std::process::Command::new("reg")
@@ -106,11 +188,22 @@ pub fn ensure_registered() -> anyhow::Result<()> {
Ok(())
}
-// TODO: implement native messaging host registration for macOS
-// (~/.config/google-chrome/NativeMessagingHosts/ on Linux,
-// ~/Library/Application Support/Google/Chrome/NativeMessagingHosts/ on macOS)
#[cfg(not(target_os = "windows"))]
-pub fn ensure_registered() -> anyhow::Result<()> {
+fn register_host_manifest(_manifest_path: &Path) -> anyhow::Result<()> {
+ Ok(())
+}
+
+#[cfg(unix)]
+fn sync_host_permissions(source: &Path, dest: &Path) -> anyhow::Result<()> {
+ use std::os::unix::fs::PermissionsExt;
+
+ let mode = fs::metadata(source)?.permissions().mode();
+ fs::set_permissions(dest, std::fs::Permissions::from_mode(mode))?;
+ Ok(())
+}
+
+#[cfg(not(unix))]
+fn sync_host_permissions(_source: &Path, _dest: &Path) -> anyhow::Result<()> {
Ok(())
}
@@ -206,3 +299,68 @@ fn write_message(response: &NativeHostResponse) -> anyhow::Result<()> {
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn host_copy_name_matches_platform() {
+ #[cfg(target_os = "windows")]
+ assert_eq!(HOST_COPY_NAME, "omniget-native-host.exe");
+
+ #[cfg(not(target_os = "windows"))]
+ assert_eq!(HOST_COPY_NAME, "omniget-native-host");
+ }
+
+ #[test]
+ fn manifest_path_from_base_matches_platform_location() {
+ #[cfg(target_os = "windows")]
+ let base = Path::new(r"C:\Users\test\AppData\Roaming\omniget\chrome-native-host");
+
+ #[cfg(not(target_os = "windows"))]
+ let base = Path::new("/tmp/chrome-base");
+
+ let manifest_path = chrome_manifest_path_from_base(base);
+
+ #[cfg(target_os = "windows")]
+ assert_eq!(manifest_path, base.join(HOST_MANIFEST_NAME));
+
+ #[cfg(target_os = "linux")]
+ assert_eq!(
+ manifest_path,
+ base.join("google-chrome")
+ .join("NativeMessagingHosts")
+ .join(HOST_MANIFEST_NAME)
+ );
+
+ #[cfg(target_os = "macos")]
+ assert_eq!(
+ manifest_path,
+ base.join("Google")
+ .join("Chrome")
+ .join("NativeMessagingHosts")
+ .join(HOST_MANIFEST_NAME)
+ );
+ }
+
+ #[test]
+ fn build_host_manifest_contains_expected_fields() {
+ #[cfg(target_os = "windows")]
+ let host_exe = Path::new(r"C:\tmp\omniget-native-host.exe");
+
+ #[cfg(not(target_os = "windows"))]
+ let host_exe = Path::new("/tmp/omniget-native-host");
+
+ let manifest = build_host_manifest(host_exe);
+
+ assert_eq!(manifest["name"].as_str(), Some(CHROME_HOST_NAME));
+ assert_eq!(manifest["description"].as_str(), Some("OmniGet native host for Chrome"));
+ assert_eq!(manifest["path"].as_str(), Some(host_exe.to_string_lossy().as_ref()));
+ assert_eq!(manifest["type"].as_str(), Some("stdio"));
+ assert_eq!(
+ manifest["allowed_origins"][0].as_str(),
+ Some(format!("chrome-extension://{CHROME_EXTENSION_ID}/").as_str())
+ );
+ }
+}