fix: overlay窗口 require改ESM import, nodeIntegration开启使ipcRenderer可用, script标签模板转义修复

This commit is contained in:
yu 2026-11-03 13:55:28 +08:00
parent 0651bc995c
commit bc4b349771

View File

@ -1,8 +1,6 @@
import { BrowserWindow, ipcMain } from "electron";
import path from "node:path";
import { BrowserWindow, ipcMain, screen } from "electron";
let overlayWindow: BrowserWindow | null = null;
let cachedProgress = { current: "", done: 0, total: 0 };
export function showRunOverlay(mainWindow: BrowserWindow | null): void {
if (mainWindow && !mainWindow.isDestroyed()) mainWindow.minimize();
@ -14,9 +12,6 @@ export function showRunOverlay(mainWindow: BrowserWindow | null): void {
const w = 320;
const h = 56;
const {
screen
} = require("electron") as typeof import("electron");
const workArea = screen.getPrimaryDisplay().workArea;
const x = workArea.x + workArea.width - w - 16;
const y = workArea.y + workArea.height - h - 16;
@ -40,9 +35,8 @@ export function showRunOverlay(mainWindow: BrowserWindow | null): void {
transparent: false,
backgroundColor: "#1E2530",
webPreferences: {
preload: path.join(__dirname, "../preload/index.js"),
contextIsolation: true,
nodeIntegration: false,
contextIsolation: false,
nodeIntegration: true,
sandbox: false
}
});
@ -64,34 +58,33 @@ body{width:320px;height:56px;background:#1E2530;color:#E8ECF2;font-family:-apple
</style></head><body>
<div class="spinner" id="spin"></div>
<div class="info">
<div class="step" id="step">准备运行…</div>
<div class="step" id="step">\u51C6\u5907\u8FD0\u884C\u2026</div>
<div class="progress-bar"><div class="progress-fill" id="fill" style="width:0%"></div></div>
</div>
<div class="meta" id="meta">0/0</div>
<button class="stop-btn" id="stopBtn" onclick="stopRun()">停止</button>
<button class="stop-btn" id="stopBtn" onclick="stopRun()">\u505C\u6B62</button>
<script>
const { ipcRenderer } = require("electron");
let done = 0, total = 0, startTime = Date.now();
let timer = setInterval(() => {
const s = Math.floor((Date.now() - startTime) / 1000);
document.getElementById("meta").textContent = done + "/" + total + " · " + s + "s";
document.getElementById("meta").textContent = done + "/" + total + " \u00B7 " + s + "s";
}, 1000);
ipcRenderer.on("run:event", (_e, ev) => {
if (ev.type === "node.started" && ev.nodeLabel) {
document.getElementById("step").textContent = ev.nodeLabel;
}
if (ev.type === "node.started") { /* spinner already running */ }
if (ev.type === "run.started") { startTime = Date.now(); }
if (ev.type === "run.total" && ev.total) {
total = ev.total;
}
if (ev.type === "node.finished" || ev.type === "node.skipped") {
done++;
const pct = total > 0 ? Math.round(done / total * 100) : 0;
document.getElementById("fill").style.width = pct + "%";
document.getElementById("meta").textContent = done + "/" + total;
}
if (ev.type === "run.total") {
total = ev.total;
}
if (ev.type === "run.finished") {
clearInterval(timer);
document.getElementById("spin").style.display = "none";
@ -99,18 +92,18 @@ ipcRenderer.on("run:event", (_e, ev) => {
fill.style.background = ev.status === "succeeded" ? "#4CAF50" : "#E05252";
fill.style.width = "100%";
document.getElementById("step").textContent =
ev.status === "succeeded" ? "✓ 运行成功" :
ev.status === "failed" ? "✗ 运行失败" : "已取消";
document.getElementById("stopBtn").textContent = "关闭";
document.getElementById("stopBtn").onclick = () => { require("electron").ipcRenderer.send("overlay:close"); };
setTimeout(() => { require("electron").ipcRenderer.send("overlay:close"); }, ev.status === "succeeded" ? 3000 : 10000);
ev.status === "succeeded" ? "\u2713 \u8FD0\u884C\u6210\u529F" :
ev.status === "failed" ? "\u2717 \u8FD0\u884C\u5931\u8D25" : "\u5DF2\u53D6\u6D88";
document.getElementById("stopBtn").textContent = "\u5173\u95ED";
document.getElementById("stopBtn").onclick = () => { ipcRenderer.send("overlay:close"); };
setTimeout(() => { ipcRenderer.send("overlay:close"); }, ev.status === "succeeded" ? 3000 : 10000);
}
});
function stopRun() {
require("electron").ipcRenderer.send("overlay:stop");
ipcRenderer.send("overlay:stop");
}
<\/script>
</scr` + `ipt>
</body></html>`;
overlayWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(html)}`);