重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
electron by gentleman-programming/gentleman-skills
npx skills add https://github.com/gentleman-programming/gentleman-skills --skill electron在以下情况下加载此技能:
src/
├── main/ # 主进程 (Node.js)
│ ├── index.ts # 入口点
│ ├── ipc/ # IPC 处理器
│ │ ├── handlers.ts
│ │ └── channels.ts # 类型安全的通道名称
│ ├── services/ # 原生服务
│ │ ├── store.ts # electron-store
│ │ └── updater.ts # 自动更新器
│ └── windows/ # 窗口管理
│ └── main-window.ts
├── renderer/ # 渲染器进程 (浏览器)
│ ├── src/
│ │ ├── App.tsx
│ │ ├── components/
│ │ └── hooks/
│ │ └── useIPC.ts # IPC 钩子
│ └── index.html
├── preload/ # 预加载脚本
│ └── index.ts # 暴露安全的 API
└── shared/ # 共享类型
└── types.ts
始终使用 contextBridge 进行安全通信:
// preload/index.ts
import { contextBridge, ipcRenderer } from 'electron';
import type { IpcChannels } from '../shared/types';
// 类型安全的暴露 API
const electronAPI = {
// 单向:渲染器 -> 主进程
send: <T extends keyof IpcChannels>(
channel: T,
data: IpcChannels[T]['request']
) => {
ipcRenderer.send(channel, data);
},
// 双向:渲染器 -> 主进程 -> 渲染器
invoke: <T extends keyof IpcChannels>(
channel: T,
data: IpcChannels[T]['request']
): Promise<IpcChannels[T]['response']> => {
return ipcRenderer.invoke(channel, data);
},
// 监听:主进程 -> 渲染器
on: <T extends keyof IpcChannels>(
channel: T,
callback: (data: IpcChannels[T]['response']) => void
) => {
const subscription = (_: Electron.IpcRendererEvent, data: IpcChannels[T]['response']) => {
callback(data);
};
ipcRenderer.on(channel, subscription);
return () => ipcRenderer.removeListener(channel, subscription);
},
};
contextBridge.exposeInMainWorld('electron', electronAPI);
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
使用请求/响应类型定义所有通道:
// shared/types.ts
export interface IpcChannels {
'app:get-version': {
request: void;
response: string;
};
'file:read': {
request: { path: string };
response: { content: string } | { error: string };
};
'file:write': {
request: { path: string; content: string };
response: { success: boolean };
};
'dialog:open-file': {
request: { filters?: Electron.FileFilter[] };
response: string | null;
};
'store:get': {
request: { key: string };
response: unknown;
};
'store:set': {
request: { key: string; value: unknown };
response: void;
};
}
// 为渲染器扩展 Window 接口
declare global {
interface Window {
electron: typeof electronAPI;
}
}
// main/index.ts
import { app, BrowserWindow, ipcMain } from 'electron';
import path from 'path';
import { registerIpcHandlers } from './ipc/handlers';
let mainWindow: BrowserWindow | null = null;
async function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
webPreferences: {
preload: path.join(__dirname, '../preload/index.js'),
contextIsolation: true, // 安全必需
nodeIntegration: false, // 安全必需
sandbox: true, // 额外安全
},
titleBarStyle: process.platform === 'darwin' ? 'hiddenInset' : 'default',
trafficLightPosition: { x: 15, y: 10 },
});
// 注册 IPC 处理器
registerIpcHandlers();
// 加载应用
if (process.env.NODE_ENV === 'development') {
mainWindow.loadURL('http://localhost:5173');
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'));
}
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// main/ipc/handlers.ts
import { ipcMain, dialog, app } from 'electron';
import fs from 'fs/promises';
import Store from 'electron-store';
const store = new Store();
export function registerIpcHandlers() {
// 获取应用版本
ipcMain.handle('app:get-version', () => {
return app.getVersion();
});
// 文件操作
ipcMain.handle('file:read', async (_, { path }) => {
try {
const content = await fs.readFile(path, 'utf-8');
return { content };
} catch (error) {
return { error: (error as Error).message };
}
});
ipcMain.handle('file:write', async (_, { path, content }) => {
try {
await fs.writeFile(path, content, 'utf-8');
return { success: true };
} catch {
return { success: false };
}
});
// 原生对话框
ipcMain.handle('dialog:open-file', async (_, { filters }) => {
const result = await dialog.showOpenDialog({
properties: ['openFile'],
filters: filters || [{ name: 'All Files', extensions: ['*'] }],
});
return result.canceled ? null : result.filePaths[0];
});
// 持久化存储
ipcMain.handle('store:get', (_, { key }) => {
return store.get(key);
});
ipcMain.handle('store:set', (_, { key, value }) => {
store.set(key, value);
});
}
// renderer/src/hooks/useIPC.ts
import { useCallback, useEffect, useState } from 'react';
export function useIPC<T>(
channel: string,
initialValue: T
): [T, boolean, Error | null] {
const [data, setData] = useState<T>(initialValue);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
let mounted = true;
window.electron
.invoke(channel, undefined)
.then((result) => {
if (mounted) {
setData(result as T);
setLoading(false);
}
})
.catch((err) => {
if (mounted) {
setError(err);
setLoading(false);
}
});
return () => {
mounted = false;
};
}, [channel]);
return [data, loading, error];
}
// 用于 IPC 订阅的 Hook
export function useIPCListener<T>(
channel: string,
callback: (data: T) => void
) {
useEffect(() => {
const unsubscribe = window.electron.on(channel, callback);
return unsubscribe;
}, [channel, callback]);
}
// 用于 IPC 变更的 Hook
export function useIPCMutation<TRequest, TResponse>(channel: string) {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const mutate = useCallback(
async (data: TRequest): Promise<TResponse | null> => {
setLoading(true);
setError(null);
try {
const result = await window.electron.invoke(channel, data);
return result as TResponse;
} catch (err) {
setError(err as Error);
return null;
} finally {
setLoading(false);
}
},
[channel]
);
return { mutate, loading, error };
}
// main/services/updater.ts
import { autoUpdater } from 'electron-updater';
import { BrowserWindow } from 'electron';
import log from 'electron-log';
export function setupAutoUpdater(mainWindow: BrowserWindow) {
autoUpdater.logger = log;
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.on('checking-for-update', () => {
mainWindow.webContents.send('updater:checking');
});
autoUpdater.on('update-available', (info) => {
mainWindow.webContents.send('updater:available', info);
});
autoUpdater.on('update-not-available', () => {
mainWindow.webContents.send('updater:not-available');
});
autoUpdater.on('download-progress', (progress) => {
mainWindow.webContents.send('updater:progress', progress);
});
autoUpdater.on('update-downloaded', () => {
mainWindow.webContents.send('updater:downloaded');
});
autoUpdater.on('error', (error) => {
mainWindow.webContents.send('updater:error', error.message);
});
// 启动时检查更新(延迟)
setTimeout(() => {
autoUpdater.checkForUpdates();
}, 5000);
}
// 更新器的 IPC 处理器
export function registerUpdaterHandlers() {
ipcMain.handle('updater:check', () => autoUpdater.checkForUpdates());
ipcMain.handle('updater:download', () => autoUpdater.downloadUpdate());
ipcMain.handle('updater:install', () => autoUpdater.quitAndInstall());
}
// main/menu.ts
import { Menu, shell, app, BrowserWindow } from 'electron';
export function createMenu(mainWindow: BrowserWindow) {
const isMac = process.platform === 'darwin';
const template: Electron.MenuItemConstructorOptions[] = [
...(isMac
? [{
label: app.name,
submenu: [
{ role: 'about' as const },
{ type: 'separator' as const },
{ role: 'services' as const },
{ type: 'separator' as const },
{ role: 'hide' as const },
{ role: 'hideOthers' as const },
{ role: 'unhide' as const },
{ type: 'separator' as const },
{ role: 'quit' as const },
],
}]
: []),
{
label: '文件',
submenu: [
{
label: '打开文件',
accelerator: 'CmdOrCtrl+O',
click: () => mainWindow.webContents.send('menu:open-file'),
},
{
label: '保存',
accelerator: 'CmdOrCtrl+S',
click: () => mainWindow.webContents.send('menu:save'),
},
{ type: 'separator' },
isMac ? { role: 'close' } : { role: 'quit' },
],
},
{
label: '编辑',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'selectAll' },
],
},
{
label: '视图',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
],
},
{
label: '帮助',
submenu: [
{
label: '文档',
click: () => shell.openExternal('https://example.com/docs'),
},
],
},
];
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}
// ❌ 危险 - 永远不要这样做
const win = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // 安全漏洞!
contextIsolation: false, // 安全漏洞!
},
});
// ✅ 安全 - 始终将 contextIsolation 与预加载脚本一起使用
const win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
},
});
// ❌ 不好 - remote 已弃用且不安全
const { BrowserWindow } = require('@electron/remote');
// ✅ 好 - 对所有主进程访问使用 IPC
// 在渲染器中:
const result = await window.electron.invoke('dialog:open-file', {});
// ❌ 不好 - 暴露了所有内容
contextBridge.exposeInMainWorld('electron', {
ipcRenderer: ipcRenderer, // 永远不要暴露整个模块!
});
// ✅ 好 - 只暴露特定的、类型化的方法
contextBridge.exposeInMainWorld('electron', {
invoke: (channel: string, data: unknown) => {
const allowedChannels = ['app:get-version', 'file:read'];
if (allowedChannels.includes(channel)) {
return ipcRenderer.invoke(channel, data);
}
throw new Error(`Channel ${channel} not allowed`);
},
});
| 任务 | 模式 |
|---|---|
| 创建项目 | npm create electron-vite@latest |
| 主进程文件访问 | 在主进程中使用 Node.js fs 模块 |
| 渲染器文件访问 | 通过预加载脚本进行 IPC |
| 持久化存储 | 在主进程中使用 electron-store |
| 自动更新 | electron-updater |
| 原生通知 | 在主进程中使用 new Notification() |
| 系统托盘 | 在主进程中使用 Tray 类 |
| 键盘快捷键 | globalShortcut.register() |
| 深度链接 | app.setAsDefaultProtocolClient() |
| 代码签名 | electron-builder 配置 |
每周安装数
50
代码仓库
GitHub 星标数
374
首次出现
2026年1月24日
安全审计
安装于
opencode44
codex36
gemini-cli35
github-copilot34
cursor34
kimi-cli32
Load this skill when:
src/
├── main/ # Main process (Node.js)
│ ├── index.ts # Entry point
│ ├── ipc/ # IPC handlers
│ │ ├── handlers.ts
│ │ └── channels.ts # Type-safe channel names
│ ├── services/ # Native services
│ │ ├── store.ts # electron-store
│ │ └── updater.ts # auto-updater
│ └── windows/ # Window management
│ └── main-window.ts
├── renderer/ # Renderer process (browser)
│ ├── src/
│ │ ├── App.tsx
│ │ ├── components/
│ │ └── hooks/
│ │ └── useIPC.ts # IPC hooks
│ └── index.html
├── preload/ # Preload scripts
│ └── index.ts # Expose safe APIs
└── shared/ # Shared types
└── types.ts
Always use contextBridge for secure communication:
// preload/index.ts
import { contextBridge, ipcRenderer } from 'electron';
import type { IpcChannels } from '../shared/types';
// Type-safe exposed API
const electronAPI = {
// One-way: renderer -> main
send: <T extends keyof IpcChannels>(
channel: T,
data: IpcChannels[T]['request']
) => {
ipcRenderer.send(channel, data);
},
// Two-way: renderer -> main -> renderer
invoke: <T extends keyof IpcChannels>(
channel: T,
data: IpcChannels[T]['request']
): Promise<IpcChannels[T]['response']> => {
return ipcRenderer.invoke(channel, data);
},
// Listen: main -> renderer
on: <T extends keyof IpcChannels>(
channel: T,
callback: (data: IpcChannels[T]['response']) => void
) => {
const subscription = (_: Electron.IpcRendererEvent, data: IpcChannels[T]['response']) => {
callback(data);
};
ipcRenderer.on(channel, subscription);
return () => ipcRenderer.removeListener(channel, subscription);
},
};
contextBridge.exposeInMainWorld('electron', electronAPI);
Define all channels with request/response types:
// shared/types.ts
export interface IpcChannels {
'app:get-version': {
request: void;
response: string;
};
'file:read': {
request: { path: string };
response: { content: string } | { error: string };
};
'file:write': {
request: { path: string; content: string };
response: { success: boolean };
};
'dialog:open-file': {
request: { filters?: Electron.FileFilter[] };
response: string | null;
};
'store:get': {
request: { key: string };
response: unknown;
};
'store:set': {
request: { key: string; value: unknown };
response: void;
};
}
// Extend Window interface for renderer
declare global {
interface Window {
electron: typeof electronAPI;
}
}
// main/index.ts
import { app, BrowserWindow, ipcMain } from 'electron';
import path from 'path';
import { registerIpcHandlers } from './ipc/handlers';
let mainWindow: BrowserWindow | null = null;
async function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
webPreferences: {
preload: path.join(__dirname, '../preload/index.js'),
contextIsolation: true, // Required for security
nodeIntegration: false, // Required for security
sandbox: true, // Extra security
},
titleBarStyle: process.platform === 'darwin' ? 'hiddenInset' : 'default',
trafficLightPosition: { x: 15, y: 10 },
});
// Register IPC handlers
registerIpcHandlers();
// Load the app
if (process.env.NODE_ENV === 'development') {
mainWindow.loadURL('http://localhost:5173');
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'));
}
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// main/ipc/handlers.ts
import { ipcMain, dialog, app } from 'electron';
import fs from 'fs/promises';
import Store from 'electron-store';
const store = new Store();
export function registerIpcHandlers() {
// Get app version
ipcMain.handle('app:get-version', () => {
return app.getVersion();
});
// File operations
ipcMain.handle('file:read', async (_, { path }) => {
try {
const content = await fs.readFile(path, 'utf-8');
return { content };
} catch (error) {
return { error: (error as Error).message };
}
});
ipcMain.handle('file:write', async (_, { path, content }) => {
try {
await fs.writeFile(path, content, 'utf-8');
return { success: true };
} catch {
return { success: false };
}
});
// Native dialogs
ipcMain.handle('dialog:open-file', async (_, { filters }) => {
const result = await dialog.showOpenDialog({
properties: ['openFile'],
filters: filters || [{ name: 'All Files', extensions: ['*'] }],
});
return result.canceled ? null : result.filePaths[0];
});
// Persistent storage
ipcMain.handle('store:get', (_, { key }) => {
return store.get(key);
});
ipcMain.handle('store:set', (_, { key, value }) => {
store.set(key, value);
});
}
// renderer/src/hooks/useIPC.ts
import { useCallback, useEffect, useState } from 'react';
export function useIPC<T>(
channel: string,
initialValue: T
): [T, boolean, Error | null] {
const [data, setData] = useState<T>(initialValue);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
useEffect(() => {
let mounted = true;
window.electron
.invoke(channel, undefined)
.then((result) => {
if (mounted) {
setData(result as T);
setLoading(false);
}
})
.catch((err) => {
if (mounted) {
setError(err);
setLoading(false);
}
});
return () => {
mounted = false;
};
}, [channel]);
return [data, loading, error];
}
// Hook for IPC subscriptions
export function useIPCListener<T>(
channel: string,
callback: (data: T) => void
) {
useEffect(() => {
const unsubscribe = window.electron.on(channel, callback);
return unsubscribe;
}, [channel, callback]);
}
// Hook for IPC mutations
export function useIPCMutation<TRequest, TResponse>(channel: string) {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const mutate = useCallback(
async (data: TRequest): Promise<TResponse | null> => {
setLoading(true);
setError(null);
try {
const result = await window.electron.invoke(channel, data);
return result as TResponse;
} catch (err) {
setError(err as Error);
return null;
} finally {
setLoading(false);
}
},
[channel]
);
return { mutate, loading, error };
}
// main/services/updater.ts
import { autoUpdater } from 'electron-updater';
import { BrowserWindow } from 'electron';
import log from 'electron-log';
export function setupAutoUpdater(mainWindow: BrowserWindow) {
autoUpdater.logger = log;
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.on('checking-for-update', () => {
mainWindow.webContents.send('updater:checking');
});
autoUpdater.on('update-available', (info) => {
mainWindow.webContents.send('updater:available', info);
});
autoUpdater.on('update-not-available', () => {
mainWindow.webContents.send('updater:not-available');
});
autoUpdater.on('download-progress', (progress) => {
mainWindow.webContents.send('updater:progress', progress);
});
autoUpdater.on('update-downloaded', () => {
mainWindow.webContents.send('updater:downloaded');
});
autoUpdater.on('error', (error) => {
mainWindow.webContents.send('updater:error', error.message);
});
// Check for updates on startup (with delay)
setTimeout(() => {
autoUpdater.checkForUpdates();
}, 5000);
}
// IPC handlers for updater
export function registerUpdaterHandlers() {
ipcMain.handle('updater:check', () => autoUpdater.checkForUpdates());
ipcMain.handle('updater:download', () => autoUpdater.downloadUpdate());
ipcMain.handle('updater:install', () => autoUpdater.quitAndInstall());
}
// main/menu.ts
import { Menu, shell, app, BrowserWindow } from 'electron';
export function createMenu(mainWindow: BrowserWindow) {
const isMac = process.platform === 'darwin';
const template: Electron.MenuItemConstructorOptions[] = [
...(isMac
? [{
label: app.name,
submenu: [
{ role: 'about' as const },
{ type: 'separator' as const },
{ role: 'services' as const },
{ type: 'separator' as const },
{ role: 'hide' as const },
{ role: 'hideOthers' as const },
{ role: 'unhide' as const },
{ type: 'separator' as const },
{ role: 'quit' as const },
],
}]
: []),
{
label: 'File',
submenu: [
{
label: 'Open File',
accelerator: 'CmdOrCtrl+O',
click: () => mainWindow.webContents.send('menu:open-file'),
},
{
label: 'Save',
accelerator: 'CmdOrCtrl+S',
click: () => mainWindow.webContents.send('menu:save'),
},
{ type: 'separator' },
isMac ? { role: 'close' } : { role: 'quit' },
],
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'selectAll' },
],
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
],
},
{
label: 'Help',
submenu: [
{
label: 'Documentation',
click: () => shell.openExternal('https://example.com/docs'),
},
],
},
];
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}
// ❌ DANGEROUS - Never do this
const win = new BrowserWindow({
webPreferences: {
nodeIntegration: true, // Security vulnerability!
contextIsolation: false, // Security vulnerability!
},
});
// ✅ Safe - Always use contextIsolation with preload
const win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
},
});
// ❌ Bad - remote is deprecated and insecure
const { BrowserWindow } = require('@electron/remote');
// ✅ Good - Use IPC for all main process access
// In renderer:
const result = await window.electron.invoke('dialog:open-file', {});
// ❌ Bad - exposes everything
contextBridge.exposeInMainWorld('electron', {
ipcRenderer: ipcRenderer, // Never expose the entire module!
});
// ✅ Good - expose only specific, typed methods
contextBridge.exposeInMainWorld('electron', {
invoke: (channel: string, data: unknown) => {
const allowedChannels = ['app:get-version', 'file:read'];
if (allowedChannels.includes(channel)) {
return ipcRenderer.invoke(channel, data);
}
throw new Error(`Channel ${channel} not allowed`);
},
});
| Task | Pattern |
|---|---|
| Create project | npm create electron-vite@latest |
| Main process file access | Use Node.js fs module in main |
| Renderer file access | IPC through preload |
| Persistent storage | electron-store in main process |
| Auto-updates | electron-updater |
| Native notifications | new Notification() in main |
| System tray | class in main |
Weekly Installs
50
Repository
GitHub Stars
374
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode44
codex36
gemini-cli35
github-copilot34
cursor34
kimi-cli32
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
127,000 周安装
Tray| Keyboard shortcuts | globalShortcut.register() |
| Deep linking | app.setAsDefaultProtocolClient() |
| Code signing | electron-builder config |