import "./styles.css"; import type { BridgeConfig } from "../shared/ipc"; type TabId = "exposure" | "isolation" | "serialization"; declare global { interface Window { api: { config(): Promise; ping(message: string): Promise; }; require?: unknown; ipcRenderer?: unknown; __setTab?: (tab: TabId) => void; __snapshot?: () => unknown; } } const state: { tab: TabId; config: BridgeConfig | null; pingResult: string | null; busy: boolean; } = { tab: "exposure", config: null, pingResult: null, busy: false }; const tabs: Array<{ id: TabId; label: string }> = [ { id: "exposure", label: "暴露面" }, { id: "isolation", label: "隔离世界" }, { id: "serialization", label: "传值边界" }, ]; function escapeHtml(value: string): string { return value .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """); } function available(value: boolean): string { return value ? "可用" : "不可用"; } function exposure(): string { const hasApi = typeof window.api === "object" && window.api !== null; const hasPing = typeof window.api?.ping === "function"; const hasRequire = typeof window.require !== "undefined"; const hasIpcRenderer = typeof window.ipcRenderer !== "undefined"; return `

页面能看到什么

全局对象状态说明
window.api${available(hasApi)}preload 暴露的白名单对象
window.api.ping${available(hasPing)}只暴露具体方法
window.require${available(hasRequire)}渲染进程拿不到 Node 模块系统
window.ipcRenderer${available(hasIpcRenderer)}不暴露 IPC 本体

页面只能看到 api;require 与 ipcRenderer 都不在页面世界里。

`; } function isolation(config: BridgeConfig): string { return `

隔离世界

contextIsolation${config.contextIsolation}
sandbox${config.sandbox}
nodeIntegration${config.nodeIntegration}
preload${escapeHtml(config.preload)}

preload 在页面脚本之前、在一个隔离的 JavaScript 世界里执行;它与页面共享 DOM,但全局对象是分开的。

contextBridge 在两边做一次可序列化拷贝:函数被包装成可调用代理,对象被克隆,原型与引用关系不会过去。

`; } function serialization(result: string | null): string { return `

传值边界

点击按钮通过 window.api.ping 发一条字符串到主进程;主进程返回 pong: ...。

${ result ? `

结果:${escapeHtml(result)}

` : `

还没有调用。

` }

函数、普通对象、数组、字符串都能过桥;类实例的方法、DOM 节点、Promise 不行。跨进程传的是数据,不是对象图。

`; } function render(): void { const root = document.querySelector("#app"); if (!root) return; const body = state.tab === "exposure" ? exposure() : state.tab === "isolation" ? state.config ? isolation(state.config) : `

正在读取配置…

` : serialization(state.pingResult); root.innerHTML = `

预加载脚本与 contextBridge

页面只看得到白名单方法;preload 在隔离世界里跑

${body}
`; for (const button of root.querySelectorAll("[data-tab]")) { button.addEventListener("click", () => { const tab = button.dataset.tab; if ( tab === "exposure" || tab === "isolation" || tab === "serialization" ) { state.tab = tab; render(); } }); } root.querySelector("#ping-button")?.addEventListener("click", async () => { if (state.busy) return; state.busy = true; try { state.pingResult = await window.api.ping("hello"); } finally { state.busy = false; render(); } }); } async function boot(): Promise { state.config = await window.api.config(); render(); window.__setTab = (tab) => { state.tab = tab; render(); }; window.__snapshot = () => ({ tab: state.tab, hasApi: typeof window.api === "object", hasRequire: typeof window.require !== "undefined", }); } void boot();