Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat(tauri): add compatibility shim for window.__TAURI_INTERNALS__ on Windows

hairyf3568a7c306f4
diff --git a/src-tauri/src/desktop/builder.rs b/src-tauri/src/desktop/builder.rs
index 95df4ba3..c5e71bdc 100644
--- a/src-tauri/src/desktop/builder.rs
+++ b/src-tauri/src/desktop/builder.rs
@@ -1106,6 +1106,13 @@ pub fn builder() -> tauri::Builder<tauri::Wry> {
crate::utils::show_main_window(app);
}));
+ // Windows/WebView2 上 wry 忽略 `for_main_frame_only`、把每个初始化脚本注入所有子 frame,
+ // 而 Tauri 只在 main frame 定义 `window.__TAURI_INTERNALS__`;dsh GUI 所在的跨源 iframe
+ // 因此每次加载都抛 `path` 插件脚本的 "reading 'plugins'"。垫片注册在最前面,保证排在
+ // core 插件(path 在其中)之前执行,只补骨架、不碰 isTauri(见 desktop::tauri_internals)。
+ #[cfg(windows)]
+ let builder = builder.plugin(crate::desktop::tauri_internals::shim());
+
builder
// 官方跨平台登录启动实现:Windows HKCU Run、macOS LaunchAgent、Linux XDG。
.plugin(
diff --git a/src-tauri/src/desktop/mod.rs b/src-tauri/src/desktop/mod.rs
index 934705da..4c11e080 100644
--- a/src-tauri/src/desktop/mod.rs
+++ b/src-tauri/src/desktop/mod.rs
@@ -10,6 +10,8 @@ pub mod payload;
pub mod pet;
pub mod pet_mouse;
pub mod plugin_boot;
+#[cfg(windows)]
+pub mod tauri_internals;
pub mod window;
pub mod zoom;
diff --git a/src-tauri/src/desktop/tauri_internals.rs b/src-tauri/src/desktop/tauri_internals.rs
new file mode 100644
index 00000000..3d1bc7a1
--- /dev/null
+++ b/src-tauri/src/desktop/tauri_internals.rs
@@ -0,0 +1,46 @@
+//! Tauri 初始化脚本兼容桥:给子 frame 补上 `window.__TAURI_INTERNALS__`。
+//!
+//! 背景:Tauri 的 `path` 插件初始化脚本(`tauri/src/path/init.js`)无条件读取
+//! `window.__TAURI_INTERNALS__.plugins`,而定义该全局的引导脚本是 main-frame-only
+//! (`tauri/src/manager/webview.rs` 的 `main_frame_script`)。Windows/WebView2 上 wry
+//! 忽略该标记(“scripts are always added to subframes regardless of the
+//! `for_main_frame_only` option”),会把每个插件初始化脚本注入所有子 frame;dsh GUI
+//! 正跑在跨源 `<iframe>` 里(见 `src/layout/components/iframe.tsx`),于是每次加载都抛
+//! `Uncaught TypeError: Cannot read properties of undefined (reading 'plugins')`
+//! (堆栈 `<anonymous>:5:50` 就是 `path/init.js` 第 5 行)。
+//!
+//! 修法只能落在壳层:Tauri 用户插件的初始化脚本排在 core 插件之前注册
+//! (`App::register_core_plugins` 在 `Builder::build` 末尾才执行),所以把本垫片注册成
+//! **第一个**用户插件,就能保证它先于 `path` 脚本执行,把缺失的骨架补回去。
+//!
+//! 刻意不定义 `isTauri`:引导脚本用不可配置的 `Object.defineProperty` 定义它,垫片重复
+//! 定义会抛 `Cannot redefine property: isTauri`,反而在主 frame 制造新报错。
+//!
+//! 仅在 Windows 注入:其余平台 wry 尊重 `for_main_frame_only`,子 frame 本来收不到
+//! `path` 脚本;补上 `__TAURI_INTERNALS__` 会改变 dsh 页面在 macOS/Linux 下的能力探测。
+
+/// 补齐 `window.__TAURI_INTERNALS__` 骨架的幂等垫片(与 Tauri 引导脚本同形)。
+pub(crate) const TAURI_INTERNALS_SHIM_JS: &str = r#"(function () {
+ if (window.__TAURI_INTERNALS__) return;
+ Object.defineProperty(window, '__TAURI_INTERNALS__', { value: { plugins: {} } });
+})();"#;
+
+/// 构造垫片插件:只带初始化脚本,注入所有 frame(Windows 上 wry 本来就全 frame 注入)。
+pub(crate) fn shim<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
+ tauri::plugin::Builder::new("dsh-tauri-internals-shim")
+ .js_init_script_on_all_frames(TAURI_INTERNALS_SHIM_JS)
+ .build()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn shim_fills_internals_without_touching_is_tauri() {
+ assert!(TAURI_INTERNALS_SHIM_JS.contains("window.__TAURI_INTERNALS__"));
+ assert!(TAURI_INTERNALS_SHIM_JS.contains("plugins"));
+ // isTauri 由引导脚本以不可配置 defineProperty 定义,垫片再定义会抛错
+ assert!(!TAURI_INTERNALS_SHIM_JS.contains("isTauri"));
+ }
+}