计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
electron【效率与协作】Assists with building cross-platform desktop applications using Electron. Use when architecting main/renderer process communication, configuring secure contexts, implementing auto-updates, or packaging apps for Windows, macOS, and Linux. Trigger words: electron, desktop app, browserwindow, ipc, auto-update, electron-builder.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Electron is a framework for building cross-platform desktop applications using web technologies. It combines a Node.js main process for system access and window management with Chromium renderer processes for the UI, communicating via IPC with context isolation and preload scripts for security.
electron as a dev dependency (npm install --save-dev electron), or scaffold with Electron Forge (npm init electron-app@latest my-app). Point main in package.json at the main-process file and run with electron .. Current stable line: Electron 44 (checked 44.5.1).contextBridge.exposeInMainWorld(). Never expose ipcRenderer itself.ipcMain.handle() / ipcRenderer.invoke() for request-response, and webContents.send() for main-to-renderer push. Validate arguments and the sender (event.senderFrame.origin, not the URL) inside every handler.shell in the main process and reach them from the renderer only through the preload bridge.contextIsolation: true since Electron 12, nodeIntegration: false, sandbox: true for renderers) and do not turn them off. Add a Content Security Policy, deny unexpected navigation with will-navigate and webContents.setWindowOpenHandler(), never pass untrusted URLs to shell.openExternal, and flip unused fuses (runAsNode, nodeCliInspect) with @electron/fuses.electron-builder; sign Windows builds and sign plus notarize macOS builds.autoUpdater (Squirrel) with a static storage URL or update.electronjs.org for public GitHub repos, or electron-updater from electron-builder, which supports GitHub Releases or a generic server, differential downloads, and a stagingPercentage field in the release metadata for staged rollouts. autoUpdater does not work on Linux; updates there come through the package manager or AppImage updaters.webContents.on("render-process-gone", ...) and app.on("child-process-gone", ...), and reload or show an error window.// main.js
const { app, BrowserWindow, ipcMain, dialog } = require("electron");
const path = require("node:path");
function createWindow() {
const win = new BrowserWindow({
width: 1100, height: 720,
webPreferences: { preload: path.join(__dirname, "preload.js") }, // defaults stay secure
});
win.webContents.setWindowOpenHandler(() => ({ action: "deny" }));
win.loadFile("index.html");
}
ipcMain.handle("dialog:openFile", async (event) => {
if (event.senderFrame?.origin !== "file://") return null; // only our own page
const { canceled, filePaths } = await dialog.showOpenDialog({ properties: ["openFile"] });
return canceled ? null : filePaths[0];
});
app.whenReady().then(createWindow);
app.on("window-all-closed", () => { if (process.platform !== "darwin") app.quit(); });
{
"name": "file-manager",
"version": "1.0.0",
"main": "main.js",
"scripts": { "start": "electron ." },
"devDependencies": { "electron": "^44.5.1" }
}
// preload.js
const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("files", { openFile: () => ipcRenderer.invoke("dialog:openFile") });
User request: "Create an Electron app that browses and manages files with native dialogs"
Actions:
BrowserWindow and preload script exposing file system commandsdialog.showOpenDialog(), dialog.showSaveDialog(), and file operationsnpx electron . and confirm that window.files.openFile() in DevTools returns a path while require is undefined in the rendererOutput: A cross-platform file manager with native OS dialogs and secure IPC-based file access.
User request: "Set up auto-updates for my Electron app using GitHub Releases"
Actions:
electron-builder with publish settings pointing to GitHub Releaseselectron-updater in the main process with update check on startupstagingPercentage (for example 10) in the published latest.yml to update a share of users first, and raise it as the release proves stableOutput: An Electron app that automatically checks for updates, downloads them in the background, and prompts the user to restart.
nodeIntegration in the renderer or disable sandbox.ipcMain.handle() / ipcRenderer.invoke() for async operations over the older send/on pattern.default-src 'self'; script-src 'self'.render-process-gone event on webContents and monitor memory with process.getProcessMemoryInfo() or app.getAppMetrics().file:// for app content.