desktop-framework-electron
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseElectron Desktop Applications
Electron桌面应用
Quick Guide: Electron apps run two process types: a main process (Node.js, manages windows and system APIs) and renderer processes (Chromium, one per window). All communication between them flows through IPC via a preload script that usesto expose a minimal, typed API surface. Never disablecontextBridge. Never enablecontextIsolationin renderers. Package with Electron Forge or Electron Builder. Auto-update vianodeIntegration(Squirrel on macOS/Windows) orautoUpdaterfor all platforms.electron-updater
<critical_requirements>
快速指南: Electron应用运行两种进程类型:主进程(基于Node.js,管理窗口和系统API)和渲染进程(基于Chromium,每个窗口对应一个)。它们之间的所有通信都通过IPC进行,借助预加载脚本使用暴露一个最小化的类型化API接口。永远不要禁用contextBridge。永远不要在渲染进程中启用contextIsolation。使用Electron Forge或Electron Builder进行打包。通过nodeIntegration(macOS/Windows平台使用Squirrel)或autoUpdater实现全平台自动更新。electron-updater
<critical_requirements>
CRITICAL: Before Using This Skill
关键要求:使用此技能前须知
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,, named constants)import type
(You MUST keep (the default) -- disabling it exposes the entire preload scope to untrusted renderer code)
contextIsolation: true(You MUST use in preload scripts -- never expose directly)
contextBridge.exposeInMainWorld()ipcRenderer(You MUST NOT enable in any BrowserWindow -- it gives renderers full Node.js access, which is a critical security vulnerability)
nodeIntegration: true(You MUST validate and sanitize ALL data received via IPC in the main process -- treat renderer messages as untrusted input)
(You MUST use / for request-response IPC -- avoid which blocks the renderer)
ipcMain.handle()ipcRenderer.invoke()sendSync(You MUST NOT load remote URLs with or disabled -- this is equivalent to giving the remote site full system access)
nodeIntegrationcontextIsolation</critical_requirements>
Auto-detection: Electron, electron, BrowserWindow, ipcMain, ipcRenderer, contextBridge, preload, webPreferences, electron-builder, electron-forge, app.whenReady, electronAPI, mainWindow, autoUpdater, nativeTheme, safeStorage, Tray, Menu, dialog, protocol, shell
When to use:
- Building cross-platform desktop applications
- Configuring main process / renderer process architecture
- Setting up secure IPC communication patterns
- Integrating with native OS features (tray, menus, dialogs, notifications, file system)
- Packaging and distributing desktop applications
- Implementing auto-update functionality
- Registering custom protocol handlers / deep links
When NOT to use:
- Choosing a UI framework for the renderer (use the appropriate web framework skill)
- Styling the renderer UI (use the appropriate styling skill)
- Server-side or backend logic not related to the main process
- Mobile applications (Electron is desktop-only)
- CLI tools that do not need a GUI
<patterns>
所有代码必须遵循CLAUDE.md中的项目规范(短横线命名法、命名导出、导入顺序、、命名常量)import type
(必须保持(默认设置)——禁用它会将整个预加载作用域暴露给不可信的渲染进程代码)
contextIsolation: true(必须在预加载脚本中使用——绝不能直接暴露)
contextBridge.exposeInMainWorld()ipcRenderer(绝不能在任何BrowserWindow中启用——这会赋予渲染进程完整的Node.js访问权限,属于严重安全漏洞)
nodeIntegration: true(必须在主进程中验证和清理所有通过IPC接收的数据——将渲染进程消息视为不可信输入)
(必须使用 / 处理请求响应式IPC——避免使用会阻塞渲染进程的)
ipcMain.handle()ipcRenderer.invoke()sendSync(绝不能在启用或禁用的情况下加载远程URL——这等同于授予远程站点完整的系统访问权限)
nodeIntegrationcontextIsolation</critical_requirements>
自动检测关键词: Electron, electron, BrowserWindow, ipcMain, ipcRenderer, contextBridge, preload, webPreferences, electron-builder, electron-forge, app.whenReady, electronAPI, mainWindow, autoUpdater, nativeTheme, safeStorage, Tray, Menu, dialog, protocol, shell
适用场景:
- 构建跨平台桌面应用
- 配置主进程/渲染进程架构
- 设置安全的IPC通信模式
- 集成原生操作系统功能(托盘、菜单、对话框、通知、文件系统)
- 打包和分发桌面应用
- 实现自动更新功能
- 注册自定义协议处理器/深度链接
不适用场景:
- 为渲染进程选择UI框架(使用对应的Web框架技能)
- 为渲染进程UI设置样式(使用对应的样式技能)
- 与主进程无关的服务端或后端逻辑
- 移动应用(Electron仅适用于桌面端)
- 不需要GUI的CLI工具
<patterns>
Key Patterns
核心模式
Pattern 1: Secure BrowserWindow Creation
模式1:安全的BrowserWindow创建
Every BrowserWindow must use a preload script and rely on the secure defaults: , , .
contextIsolation: truesandbox: truenodeIntegration: falsejavascript
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
// contextIsolation: true -- default since Electron 12
// sandbox: true -- default since Electron 20
// nodeIntegration: false -- default since Electron 5
},
});Key point: Never override the security defaults. The preload script is the ONLY bridge between main and renderer. See examples/core.md.
每个BrowserWindow必须使用预加载脚本,并依赖安全默认值:、、。
contextIsolation: truesandbox: truenodeIntegration: falsejavascript
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
// contextIsolation: true -- Electron 12起默认启用
// sandbox: true -- Electron 20起默认启用
// nodeIntegration: false -- Electron 5起默认启用
},
});核心要点: 绝不要覆盖安全默认值。预加载脚本是主进程与渲染进程之间的唯一桥梁。详见examples/core.md。
Pattern 2: Preload with contextBridge
模式2:使用contextBridge的预加载脚本
The preload script exposes a narrow, explicitly typed API to the renderer. Never expose directly.
ipcRendererjavascript
// preload.js
const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("electronAPI", {
readFile: (filePath) => ipcRenderer.invoke("read-file", filePath),
onUpdateAvailable: (callback) => {
ipcRenderer.on("update-available", (_event, data) => callback(data));
},
});Key point: Each exposed method wraps a single IPC channel. The renderer calls with no knowledge of IPC internals. See examples/core.md.
window.electronAPI.readFile(path)预加载脚本向渲染进程暴露一个窄范围、显式类型化的API。绝不能直接暴露。
ipcRendererjavascript
// preload.js
const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("electronAPI", {
readFile: (filePath) => ipcRenderer.invoke("read-file", filePath),
onUpdateAvailable: (callback) => {
ipcRenderer.on("update-available", (_event, data) => callback(data));
},
});核心要点: 每个暴露的方法对应一个IPC通道。渲染进程通过调用,无需了解IPC内部细节。详见examples/core.md。
window.electronAPI.readFile(path)Pattern 3: IPC Request-Response (invoke/handle)
模式3:IPC请求-响应模式(invoke/handle)
Use in main and in preload for async two-way communication.
ipcMain.handle()ipcRenderer.invoke()javascript
// main process
ipcMain.handle("read-file", async (_event, filePath) => {
const content = await fs.readFile(filePath, "utf-8");
return { success: true, content };
});Key point: / returns a Promise. Always validate in the handler -- never trust renderer input. See examples/ipc.md for all IPC patterns.
handleinvokefilePath在主进程中使用,在预加载脚本中使用实现异步双向通信。
ipcMain.handle()ipcRenderer.invoke()javascript
// 主进程
ipcMain.handle("read-file", async (_event, filePath) => {
const content = await fs.readFile(filePath, "utf-8");
return { success: true, content };
});核心要点: /返回Promise。必须在处理器中验证——绝不要信任渲染进程输入。所有IPC模式详见examples/ipc.md。
handleinvokefilePathPattern 4: Main-to-Renderer Messages
模式4:主进程到渲染进程的消息推送
Use from main and listen in the preload with a callback pattern.
webContents.send()javascript
// main: send to specific window
mainWindow.webContents.send("update-progress", { percent: 45 });
// preload: expose listener
onUpdateProgress: (callback) => {
ipcRenderer.on("update-progress", (_event, data) => callback(data));
},Key point: The renderer cannot pull from main -- main must push. Always scope listeners to specific channels. See examples/ipc.md.
在主进程中使用,在预加载脚本中通过回调模式监听。
webContents.send()javascript
// 主进程:向指定窗口发送消息
mainWindow.webContents.send("update-progress", { percent: 45 });
// 预加载脚本:暴露监听器
onUpdateProgress: (callback) => {
ipcRenderer.on("update-progress", (_event, data) => callback(data));
},核心要点: 渲染进程无法主动从主进程拉取数据——必须由主进程推送。始终将监听器限定在特定通道。详见examples/ipc.md。
Pattern 5: App Lifecycle
模式5:应用生命周期管理
The main process manages the app lifecycle with platform-specific conventions.
javascript
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});Key point: macOS apps stay alive when all windows close ( should not quit). The event recreates a window when the dock icon is clicked. See examples/core.md.
window-all-closedactivate主进程按照平台特定的规范管理应用生命周期。
javascript
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});核心要点: macOS应用在所有窗口关闭后仍保持运行(事件不应触发退出)。点击dock图标时,事件会重新创建窗口。详见examples/core.md。
window-all-closedactivatePattern 6: Native OS Integration
模式6:原生操作系统集成
Electron exposes native APIs for dialogs, menus, tray icons, notifications, and more -- all accessed from the main process.
javascript
const { dialog } = require("electron");
const result = await dialog.showOpenDialog(mainWindow, {
properties: ["openFile", "multiSelections"],
filters: [{ name: "Documents", extensions: ["txt", "md", "json"] }],
});Key point: Native dialogs are modal to a window when passed as the first argument. See examples/native-apis.md.
mainWindowElectron提供了用于对话框、菜单、托盘图标、通知等功能的原生API——所有这些都从主进程访问。
javascript
const { dialog } = require("electron");
const result = await dialog.showOpenDialog(mainWindow, {
properties: ["openFile", "multiSelections"],
filters: [{ name: "Documents", extensions: ["txt", "md", "json"] }],
});核心要点: 当将作为第一个参数传入时,原生对话框会成为该窗口的模态窗口。详见examples/native-apis.md。
mainWindowPattern 7: Custom Protocol / Deep Linking
模式7:自定义协议/深度链接
Register your app to handle URLs for deep linking from browsers or other apps.
myapp://javascript
if (process.defaultApp) {
app.setAsDefaultProtocolClient("myapp", process.execPath, [
path.resolve(process.argv[1]),
]);
} else {
app.setAsDefaultProtocolClient("myapp");
}Key point: In development ( is true), you must pass the script path as an argument. On macOS, handle the event on the object. On Windows/Linux, handle via event. See examples/native-apis.md.
</patterns>
process.defaultAppopen-urlappsecond-instance<decision_framework>
注册应用以处理格式的URL,实现从浏览器或其他应用的深度链接。
myapp://javascript
if (process.defaultApp) {
app.setAsDefaultProtocolClient("myapp", process.execPath, [
path.resolve(process.argv[1]),
]);
} else {
app.setAsDefaultProtocolClient("myapp");
}核心要点: 在开发环境中(为true),必须将脚本路径作为参数传入。在macOS上,通过对象的事件处理;在Windows/Linux上,通过事件处理。详见examples/native-apis.md。
</patterns>
process.defaultAppappopen-urlsecond-instance<decision_framework>
Decision Framework
决策框架
IPC Pattern Selection
IPC模式选择
Which IPC pattern?
+-- Renderer needs a response from main?
| +-- YES --> ipcMain.handle() + ipcRenderer.invoke()
+-- Renderer sends data, no response needed?
| +-- YES --> ipcMain.on() + ipcRenderer.send()
+-- Main needs to push data to renderer?
| +-- YES --> webContents.send() + ipcRenderer.on() (in preload)
+-- Two renderers need to communicate?
| +-- YES --> Route through main process (never direct renderer-to-renderer)
+-- High-frequency data transfer (streaming)?
+-- YES --> MessageChannelMain / MessagePort pair选择哪种IPC模式?
+-- 渲染进程需要主进程返回响应?
| +-- 是 --> ipcMain.handle() + ipcRenderer.invoke()
+-- 渲染进程发送数据,无需响应?
| +-- 是 --> ipcMain.on() + ipcRenderer.send()
+-- 主进程需要向渲染进程推送数据?
| +-- 是 --> webContents.send() + ipcRenderer.on()(在预加载脚本中)
+-- 两个渲染进程需要通信?
| +-- 是 --> 通过主进程中转(绝不能直接渲染进程间通信)
+-- 高频数据传输(流式)?
+-- 是 --> MessageChannelMain / MessagePort 配对Window Architecture
窗口架构选择
How many windows?
+-- Single window app?
| +-- One BrowserWindow, one preload script
+-- Multi-window (e.g., preferences, about)?
| +-- Separate BrowserWindow per view, each with its own preload
+-- Frameless / custom title bar?
| +-- frame: false + custom drag regions via CSS (-webkit-app-region: drag)
+-- Persistent background work?
+-- Use a hidden BrowserWindow or utilityProcess (Electron 22+)</decision_framework>
Detailed resources:
- examples/core.md - App lifecycle, BrowserWindow, preload, contextBridge fundamentals
- examples/ipc.md - All IPC patterns: invoke/handle, send/on, main-to-renderer, MessagePort
- examples/security.md - Security hardening, CSP, permission handlers, safe defaults
- examples/native-apis.md - Dialogs, menus, tray, notifications, protocol handlers, auto-updater
- examples/packaging.md - Electron Forge, Electron Builder, code signing, distribution
- reference.md - API quick-reference tables, version history, security checklist
<red_flags>
需要多少个窗口?
+-- 单窗口应用?
| +-- 一个BrowserWindow,一个预加载脚本
+-- 多窗口(如偏好设置、关于页面)?
| +-- 每个视图对应单独的BrowserWindow,各有独立预加载脚本
+-- 无边框/自定义标题栏?
| +-- frame: false + 通过CSS设置自定义拖拽区域(-webkit-app-region: drag)
+-- 持久化后台任务?
+-- 使用隐藏的BrowserWindow或utilityProcess(Electron 22+)</decision_framework>
详细资源:
- examples/core.md - 应用生命周期、BrowserWindow、预加载脚本、contextBridge基础
- examples/ipc.md - 所有IPC模式:invoke/handle、send/on、主进程到渲染进程、MessagePort
- examples/security.md - 安全加固、CSP、权限处理器、安全默认值
- examples/native-apis.md - 对话框、菜单、托盘、通知、协议处理器、自动更新
- examples/packaging.md - Electron Forge、Electron Builder、代码签名、分发
- reference.md - API速查表、版本历史、安全检查清单
<red_flags>
RED FLAGS
警示信号
Critical Security Issues:
- Disabling (
contextIsolation) -- exposes preload globals to renderercontextIsolation: false - Enabling -- gives renderer full Node.js access (fs, child_process, etc.)
nodeIntegration: true - Exposing directly via
ipcRendererinstead of wrapping individual channelscontextBridge - Loading remote/untrusted URLs without
sandbox: true - Disabling in production (
webSecuritydisables same-origin policy)webSecurity: false - Not validating IPC arguments in main process handlers (path traversal, injection attacks)
- Using with unvalidated URLs (can execute arbitrary commands)
shell.openExternal()
Architecture Issues:
- Using -- blocks the renderer process, causes UI freezes
ipcRenderer.sendSync() - Putting business logic in the renderer instead of the main process
- Direct renderer-to-renderer communication (bypassing main)
- Using module (removed in Electron 14+, was a security and performance hazard)
remote - Creating BrowserWindows from the renderer process
- Not handling per-platform (macOS apps should not quit)
window-all-closed
Packaging Issues:
- Shipping in production builds (bloated app size)
devDependencies - Not code-signing the application (OS warnings, auto-update failures on macOS)
- Bundling without pruning or using ASAR archive
node_modules - Hardcoding absolute paths that differ between dev and packaged environments
- Using in renderer code (unavailable in sandboxed renderers)
__dirname
Common Mistakes:
- Forgetting -- APIs are unavailable before the
app.whenReady()eventready - Not re-creating window on event (macOS dock click does nothing)
activate - Using in renderer scripts loaded via
require()tags (not available in sandboxed renderers)<script> - Setting for iframes loading external content
nodeIntegrationInSubFrames: true - Not setting proper headers for renderer HTML
Content-Security-Policy
</red_flags>
<critical_reminders>
严重安全问题:
- 禁用(
contextIsolation)——将预加载全局变量暴露给渲染进程contextIsolation: false - 启用——赋予渲染进程完整的Node.js访问权限(fs、child_process等)
nodeIntegration: true - 通过直接暴露
contextBridge而非封装单独通道ipcRenderer - 在未启用的情况下加载远程/不可信URL
sandbox: true - 生产环境中禁用(
webSecurity会关闭同源策略)webSecurity: false - 主进程处理器中未验证IPC参数(路径遍历、注入攻击)
- 使用处理未验证的URL(可能执行任意命令)
shell.openExternal()
架构问题:
- 使用——阻塞渲染进程,导致UI冻结
ipcRenderer.sendSync() - 将业务逻辑放在渲染进程而非主进程中
- 渲染进程间直接通信(绕过主进程)
- 使用模块(Electron 14+已移除,存在安全和性能隐患)
remote - 从渲染进程创建BrowserWindow
- 未按平台处理事件(macOS应用不应退出)
window-all-closed
打包问题:
- 生产构建中包含(应用体积臃肿)
devDependencies - 未对应用进行代码签名(系统警告,macOS上自动更新失败)
- 未裁剪或使用ASAR归档打包
node_modules - 硬编码开发环境与打包环境不同的绝对路径
- 在渲染进程代码中使用(沙箱化渲染进程中不可用)
__dirname
常见错误:
- 忘记——
app.whenReady()事件前API不可用ready - 未在事件中重新创建窗口(点击macOS dock图标无反应)
activate - 在通过标签加载的渲染进程脚本中使用
<script>(沙箱化渲染进程中不可用)require() - 为加载外部内容的iframe设置
nodeIntegrationInSubFrames: true - 未为渲染进程HTML设置合适的头
Content-Security-Policy
</red_flags>
<critical_reminders>
CRITICAL REMINDERS
关键提醒
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,, named constants)import type
(You MUST keep (the default) -- disabling it exposes the entire preload scope to untrusted renderer code)
contextIsolation: true(You MUST use in preload scripts -- never expose directly)
contextBridge.exposeInMainWorld()ipcRenderer(You MUST NOT enable in any BrowserWindow -- it gives renderers full Node.js access, which is a critical security vulnerability)
nodeIntegration: true(You MUST validate and sanitize ALL data received via IPC in the main process -- treat renderer messages as untrusted input)
(You MUST use / for request-response IPC -- avoid which blocks the renderer)
ipcMain.handle()ipcRenderer.invoke()sendSyncFailure to follow these rules will create severe security vulnerabilities or broken desktop applications.
</critical_reminders>
所有代码必须遵循CLAUDE.md中的项目规范(短横线命名法、命名导出、导入顺序、、命名常量)import type
(必须保持(默认设置)——禁用它会将整个预加载作用域暴露给不可信的渲染进程代码)
contextIsolation: true(必须在预加载脚本中使用——绝不能直接暴露)
contextBridge.exposeInMainWorld()ipcRenderer(绝不能在任何BrowserWindow中启用——这会赋予渲染进程完整的Node.js访问权限,属于严重安全漏洞)
nodeIntegration: true(必须在主进程中验证和清理所有通过IPC接收的数据——将渲染进程消息视为不可信输入)
(必须使用 / 处理请求响应式IPC——避免使用会阻塞渲染进程的)
ipcMain.handle()ipcRenderer.invoke()sendSync不遵守这些规则会导致严重的安全漏洞或桌面应用故障。
</critical_reminders>