import "./styles.css"; import type { AppInfo, TabId } from "../shared/ipc"; declare global { interface Window { api: { info(): Promise }; __setTab?: (tab: TabId) => void; __snapshot?: () => unknown; } } const state: { tab: TabId; info: AppInfo | null } = { tab: "overview", info: null, }; const tabs: Array<{ id: TabId; label: string }> = [ { id: "overview", label: "总览" }, { id: "processes", label: "进程" }, { id: "security", label: "安全边界" }, ]; function escapeHtml(value: string): string { return value .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """); } function versionCard(label: string, value: string, testid: string): string { return `
${escapeHtml(label)}
${escapeHtml(value)}
`; } function overview(info: AppInfo): string { return `
${versionCard("Electron", info.electron, "info-electron")} ${versionCard("Node", info.node, "info-node")} ${versionCard("Chromium", info.chrome, "info-chrome")} ${versionCard("平台", `${info.platform} / ${info.arch}`, "info-platform")}

三层结构

主进程拥有 Node 与系统能力,创建窗口、调度 IPC
预加载脚本在隔离世界里执行,只暴露白名单 API
渲染进程每个窗口一个,跑 Chromium,负责界面
`; } function processes(info: AppInfo): string { return `

真实进程

角色PID内存
主进程${info.mainPid}—
渲染进程${info.rendererPid}${info.memoryMB} MB

PID 来自 webContents.getOSProcessId() 与 process.pid,可在活动监视器里对照。

`; } function security(): string { const rows: Array<[string, string]> = [ ["contextIsolation", "true"], ["sandbox", "true"], ["nodeIntegration", "false"], ["webSecurity", "true"], ["webviewTag", "false"], ]; return `

webPreferences 默认值

${rows.map(([k, v]) => ``).join("")}
选项值
${k}${v}

渲染进程拿不到 require 与 ipcRenderer,只能调用 preload 暴露的白名单方法。

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

正在读取进程信息…

` : state.tab === "overview" ? overview(info) : state.tab === "processes" ? processes(info) : security(); root.innerHTML = `

Electron 是什么

桌面应用 = Chromium 渲染 + Node 运行时 + 主进程调度

${body}
`; for (const button of root.querySelectorAll("[data-tab]")) { button.addEventListener("click", () => { const tab = button.dataset.tab; if (tab === "overview" || tab === "processes" || tab === "security") { state.tab = tab; render(); } }); } } async function boot(): Promise { state.info = await window.api.info(); render(); window.__setTab = (tab) => { state.tab = tab; render(); }; window.__snapshot = () => ({ tab: state.tab, electron: state.info?.electron, rendererPid: state.info?.rendererPid, }); } void boot();