import "./styles.css";
const state = {
tab: "windows",
info: null,
busy: false,
error: "",
};
const tabs = [
{ id: "windows", label: "窗口" },
{ id: "processes", label: "进程" },
{ id: "events", label: "恢复日志" },
];
function escapeHtml(value) {
return value
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """);
}
function time(at) {
return new Date(at).toLocaleTimeString("zh-CN", { hour12: false });
}
function mb(kb) {
return `${(kb / 1024).toFixed(1)} MB`;
}
function uptime(ms) {
if (ms < 1000)
return `${ms} ms`;
if (ms < 60_000)
return `${(ms / 1000).toFixed(1)} s`;
const minutes = Math.floor(ms / 60_000);
const seconds = Math.floor((ms % 60_000) / 1000);
return `${minutes}m ${String(seconds).padStart(2, "0")}s`;
}
function summary(info) {
const recoveries = Object.values(info.recoveryCounts).reduce((sum, value) => sum + (value ?? 0), 0);
return `
渲染进程
${info.windows.length}
系统进程
${info.processes.length}
`;
}
function windowTable(rows) {
return `
窗口与渲染进程
ID 角色 PID 运行时长 内存 可见 操作
${rows
.map((row) => `
${row.id}
${escapeHtml(row.role)}
${row.pid}
${uptime(row.uptimeMs)}
${mb(row.memoryKB)}
${row.visible ? "是" : "否"}
聚焦
${row.role === "control"
? `受保护 `
: `结束渲染进程 `}
`)
.join("")}
结束渲染进程会触发 render-process-gone,主进程在 300ms 后重建同角色窗口。
`;
}
function processTable(info) {
return `
app.getAppMetrics()
类型 PID 内存 CPU
${info.processes
.map((row) => `
${escapeHtml(row.type)}
${row.pid}
${mb(row.memoryKB)}
${row.cpu.toFixed(1)}%
`)
.join("")}
Browser 是主进程;Tab 是渲染进程;GPU / Utility 由 Chromium 管理。内存单位是 workingSetSize。
`;
}
function eventTable(info) {
return `
恢复日志
时间 事件 消息
${info.events
.slice()
.reverse()
.map((row) => `
${time(row.at)}
${escapeHtml(row.evt)}
${escapeHtml(row.msg ?? "")}
`)
.join("")}
每个窗口最多自动恢复 3 次;超过后停止,避免崩溃循环把 CPU 打满。
`;
}
function render() {
const root = document.querySelector("#app");
if (!root)
return;
const info = state.info;
let body = `正在读取进程信息…
`;
if (info) {
if (state.tab === "windows")
body = summary(info) + windowTable(info.windows);
else if (state.tab === "processes")
body = processTable(info);
else
body = eventTable(info);
}
root.innerHTML = `
新建窗口
刷新
日志:${escapeHtml(info?.logFileLabel ?? "")}
${state.error ? `${escapeHtml(state.error)}
` : ""}
${body} `;
for (const button of root.querySelectorAll("[data-tab]")) {
button.addEventListener("click", () => {
const tab = button.dataset.tab;
if (tab === "windows" || tab === "processes" || tab === "events") {
state.tab = tab;
render();
}
});
}
for (const button of root.querySelectorAll("[data-focus]")) {
button.addEventListener("click", () => {
const id = Number(button.dataset.focus);
void action(() => window.api.focus(id));
});
}
for (const button of root.querySelectorAll("[data-kill]")) {
button.addEventListener("click", () => {
const id = Number(button.dataset.kill);
void action(() => window.api.kill(id));
});
}
root.querySelector("#create-button")?.addEventListener("click", () => {
void action(() => window.api.create());
});
root.querySelector("#refresh-button")?.addEventListener("click", () => {
void refresh();
});
}
async function action(run) {
if (state.busy)
return;
state.busy = true;
try {
await run();
state.error = "";
await new Promise((resolve) => setTimeout(resolve, 120));
state.info = await window.api.list();
}
catch (error) {
state.error = String(error);
}
finally {
state.busy = false;
render();
}
}
async function refresh() {
if (state.busy)
return;
state.busy = true;
try {
state.info = await window.api.list();
state.error = "";
}
catch (error) {
state.error = String(error);
}
finally {
state.busy = false;
render();
}
}
async function boot() {
state.info = await window.api.list();
render();
window.__setTab = (tab) => {
state.tab = tab;
render();
};
window.__snapshot = () => ({
tab: state.tab,
windows: state.info?.windows.length ?? 0,
processes: state.info?.processes.length ?? 0,
events: state.info?.events.length ?? 0,
});
setInterval(() => {
if (!state.busy)
void refresh();
}, 1000);
}
void boot();
//# sourceMappingURL=renderer.js.map