feat(desktop): build & inject preview-agent script into child webview

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-05-29 12:58:21 +08:00
parent 029c2a3f8f
commit d3e7a35e7a
5 changed files with 13 additions and 4 deletions

View File

@ -5,7 +5,8 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"build": "bun run build:preview-agent && tsc -b && vite build",
"build:preview-agent": "bun run ./scripts/build-preview-agent.ts",
"build:sidecars": "bun run ./scripts/build-sidecars.ts",
"build:macos-arm64": "bash ./scripts/build-macos-arm64.sh",
"build:windows-x64": "powershell -ExecutionPolicy Bypass -File ./scripts/build-windows-x64.ps1",

View File

@ -0,0 +1,3 @@
import { $ } from 'bun'
await $`bun build ./src/preview-agent/index.ts --outfile=./src-tauri/resources/preview-agent.js --format=iife --minify`
console.log('preview-agent.js built')

View File

@ -0,0 +1 @@
(()=>{(()=>{window.__PREVIEW_AGENT__=!0})();})();

View File

@ -3,8 +3,7 @@ use tauri::{AppHandle, LogicalPosition, LogicalSize, Manager, Runtime, WebviewBu
const PREVIEW_LABEL: &str = "preview";
/// M1 placeholder init script; replaced by the real preview-agent bundle in a later milestone.
const PREVIEW_INIT_SCRIPT: &str = "window.__PREVIEW_AGENT_READY__ = true;";
const PREVIEW_INIT_SCRIPT: &str = include_str!("../resources/preview-agent.js");
#[derive(Default)]
pub struct PreviewState(Mutex<PreviewInner>);
@ -63,7 +62,7 @@ pub async fn preview_open<R: Runtime>(
.get_window(crate::MAIN_WINDOW_LABEL)
.ok_or_else(|| "main window not found".to_string())?;
let builder = WebviewBuilder::new(PREVIEW_LABEL, WebviewUrl::External(target))
.initialization_script(PREVIEW_INIT_SCRIPT);
.initialization_script_for_all_frames(PREVIEW_INIT_SCRIPT);
main.add_child(
builder,
LogicalPosition::new(bounds.x, bounds.y),

View File

@ -0,0 +1,5 @@
// 注入到被预览页面的 IIFE 入口M3 逐步填实)
;(() => {
// 标记注入成功,供宿主探测
;(window as unknown as { __PREVIEW_AGENT__?: boolean }).__PREVIEW_AGENT__ = true
})()