示例工程 01-what-is-electron
01-what-is-electron/app/.tsc/src/renderer.js
import "./styles.css";
const state = {
tab: "overview",
info: null,
};
const tabs = [
{ id: "overview", label: "总览" },
{ id: "processes", label: "进程" },
{ id: "security", label: "安全边界" },
];
function escapeHtml(value) {
return value
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """);
}
function versionCard(label, value, testid) {
return `<div class="card">
<div class="card-label">${escapeHtml(label)}</div>
<div class="card-value" data-testid="${testid}">${escapeHtml(value)}</div>
</div>`;
}
function overview(info) {
return `
<div class="grid">
${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")}
</div>
<div class="section">
<h2>三层结构</h2>
<div class="layers">
<div class="layer"><strong>主进程</strong><span>拥有 Node 与系统能力,创建窗口、调度 IPC</span></div>
<div class="layer"><strong>预加载脚本</strong><span>在隔离世界里执行,只暴露白名单 API</span></div>
<div class="layer"><strong>渲染进程</strong><span>每个窗口一个,跑 Chromium,负责界面</span></div>
</div>
</div>`;
}
function processes(info) {
return `
<div class="section">
<h2>真实进程</h2>
<table class="table" data-testid="process-table">
<thead><tr><th>角色</th><th>PID</th><th>内存</th></tr></thead>
<tbody>
<tr><td>主进程</td><td>${info.mainPid}</td><td>—</td></tr>
<tr><td>渲染进程</td><td>${info.rendererPid}</td><td>${info.memoryMB} MB</td></tr>
</tbody>
</table>
<p class="hint">PID 来自 <code>webContents.getOSProcessId()</code> 与 <code>process.pid</code>,可在活动监视器里对照。</p>
</div>`;
}
function security() {
const rows = [
["contextIsolation", "true"],
["sandbox", "true"],
["nodeIntegration", "false"],
["webSecurity", "true"],
["webviewTag", "false"],
];
return `
<div class="section">
<h2>webPreferences 默认值</h2>
<table class="table" data-testid="security-panel">
<thead><tr><th>选项</th><th>值</th></tr></thead>
<tbody>
${rows.map(([k, v]) => `<tr><td><code>${k}</code></td><td>${v}</td></tr>`).join("")}
</tbody>
</table>
<p class="hint">渲染进程拿不到 <code>require</code> 与 <code>ipcRenderer</code>,只能调用 preload 暴露的白名单方法。</p>
</div>`;
}
function render() {
const root = document.querySelector("#app");
if (!root)
return;
const info = state.info;
const body = !info
? `<p class="hint">正在读取进程信息…</p>`
: state.tab === "overview"
? overview(info)
: state.tab === "processes"
? processes(info)
: security();
root.innerHTML = `
<header class="header">
<div>
<h1 data-testid="app-title">Electron 是什么</h1>
<p class="subtitle">桌面应用 = Chromium 渲染 + Node 运行时 + 主进程调度</p>
</div>
<nav class="tabs">
${tabs
.map((tab) => `<button class="tab${state.tab === tab.id ? " active" : ""}" data-tab="${tab.id}" data-testid="tab-${tab.id}">${tab.label}</button>`)
.join("")}
</nav>
</header>
<main class="content">${body}</main>`;
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() {
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();
//# sourceMappingURL=renderer.js.map