OmniCS / electron /src /preload.ts
qf46578's picture
🦞 Add: electron/src/preload.ts
768b2a1 verified
Raw History Blame Contribute Delete
2.73 kB
import { contextBridge, ipcRenderer } from 'electron';
// 暴露安全的API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
// 截屏
captureScreen: (region?: { x: number; y: number; width: number; height: number }) =>
ipcRenderer.invoke('capture-screen', region),
// OCR识别
ocrRecognize: (imageBase64: string) =>
ipcRenderer.invoke('ocr-recognize', imageBase64),
// 自动化控制
automation: {
click: (x: number, y: number, button?: 'left' | 'right' | 'middle') =>
ipcRenderer.invoke('automation-click', x, y, button),
type: (text: string) =>
ipcRenderer.invoke('automation-type', text),
move: (x: number, y: number) =>
ipcRenderer.invoke('automation-move', x, y),
hotKey: (keys: string[]) =>
ipcRenderer.invoke('automation-hotkey', keys),
findImage: (templateBase64: string) =>
ipcRenderer.invoke('automation-find-image', templateBase64),
},
// 平台自动化
platform: {
init: (platform: string, config: any) =>
ipcRenderer.invoke(`platform-${platform}-init`, config),
poll: (platform: string) =>
ipcRenderer.invoke(`platform-${platform}-poll`),
send: (platform: string, customerId: string, message: string) =>
ipcRenderer.invoke(`platform-${platform}-send`, customerId, message),
},
// 事件监听
on: (channel: string, callback: (...args: any[]) => void) => {
ipcRenderer.on(channel, (_, ...args) => callback(...args));
},
removeListener: (channel: string, callback: (...args: any[]) => void) => {
ipcRenderer.removeListener(channel, callback);
},
});
// 类型定义
export interface ElectronAPI {
captureScreen: (region?: { x: number; y: number; width: number; height: number }) => Promise<string>;
ocrRecognize: (imageBase64: string) => Promise<{ text: string; confidence: number }>;
automation: {
click: (x: number, y: number, button?: 'left' | 'right' | 'middle') => Promise<{ success: boolean }>;
type: (text: string) => Promise<{ success: boolean }>;
move: (x: number, y: number) => Promise<{ success: boolean }>;
hotKey: (keys: string[]) => Promise<{ success: boolean }>;
findImage: (templateBase64: string) => Promise<{ x: number; y: number } | null>;
};
platform: {
init: (platform: string, config: any) => Promise<{ success: boolean }>;
poll: (platform: string) => Promise<any>;
send: (platform: string, customerId: string, message: string) => Promise<{ success: boolean }>;
};
on: (channel: string, callback: (...args: any[]) => void) => void;
removeListener: (channel: string, callback: (...args: any[]) => void) => void;
}
declare global {
interface Window {
electronAPI: ElectronAPI;
}
}