const { contextBridge, ipcRenderer } = require('electron'); contextBridge.exposeInMainWorld('electronAPI', { getAdbPathConfig: () => ipcRenderer.invoke('get-adb-path-config'), scanADBDevices: () => ipcRenderer.invoke('scan-adb-devices'), getADBDevices: () => ipcRenderer.invoke('get-adb-devices'), connectADBDevice: (ipPort) => ipcRenderer.invoke('connect-adb-device', ipPort), getDeviceResolution: (ipPort) => ipcRenderer.invoke('get-device-resolution', ipPort), captureScreenshot: (ipPort, options) => ipcRenderer.invoke('capture-screenshot', ipPort, options), getCachedScreenshot: (ipPort) => ipcRenderer.invoke('get-cached-screenshot', ipPort), sendTap: (ipPort, x, y) => ipcRenderer.invoke('send-tap', ipPort, x, y), sendSwipe: (ipPort, x1, y1, x2, y2, duration) => ipcRenderer.invoke('send-swipe', ipPort, x1, y1, x2, y2, duration), sendText: (ipPort, text) => ipcRenderer.invoke('send-text', ipPort, text), sendScroll: (ipPort, direction, width, height, scrollDistance, duration) => ipcRenderer.invoke('send-scroll', ipPort, direction, width, height, scrollDistance, duration), sendKeyEvent: (ipPort, keyCode) => ipcRenderer.invoke('send-key-event', ipPort, keyCode), sendSystemKey: (ipPort, keyCode) => ipcRenderer.invoke('send-system-key', ipPort, keyCode), sendBackKey: (ipPort) => ipcRenderer.invoke('send-back-key', ipPort), sendHomeKey: (ipPort) => ipcRenderer.invoke('send-home-key', ipPort), sendRecentAppsKey: (ipPort) => ipcRenderer.invoke('send-recent-apps-key', ipPort), // 监听设备发现事件 onDeviceFound: (callback) => { ipcRenderer.on('device-found', (event, device) => callback(device)); }, // 移除设备发现事件监听器 removeDeviceFoundListener: () => { ipcRenderer.removeAllListeners('device-found'); }, // 获取 static 目录下的文件夹列表 getStaticFolders: () => ipcRenderer.invoke('get-static-folders'), // 图像匹配并获取坐标 matchImageAndGetCoordinate: (ipPort, templateImagePath) => ipcRenderer.invoke('match-image-and-get-coordinate', ipPort, templateImagePath), // 图像区域定位:在完整截图中查找区域截图位置,返回四个顶点坐标 matchImageRegionLocation: (screenshotPath, regionPath, device) => ipcRenderer.invoke('match-image-region-location', screenshotPath, regionPath, device), // 裁剪并保存图片区域 cropAndSaveImage: (imagePath, x, y, width, height, savePath) => ipcRenderer.invoke('crop-and-save-image', imagePath, x, y, width, height, savePath), // 读取 processing.json 文件 readProcessingJson: (folderName) => ipcRenderer.invoke('read-processing-json', folderName), // 保存 processing.json 文件到现有工作流文件夹 saveProcessingJson: (folderName, workflowData) => ipcRenderer.invoke('save-processing-json', folderName, workflowData), // 文字识别并获取坐标 findTextAndGetCoordinate: (ipPort, targetText) => ipcRenderer.invoke('find-text-and-get-coordinate', ipPort, targetText), // OCR识别最后一条消息(兼容旧API) ocrLastMessage: (ipPort, method, avatarPath, area, folderPath) => ipcRenderer.invoke('ocr-last-message', ipPort, method, avatarPath, area, folderPath), // 提取聊天记录 extractChatHistory: (ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath, region = null, friendRgb = null, myRgb = null) => ipcRenderer.invoke('extract-chat-history', ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath, region, friendRgb, myRgb), // OCR 聊天记录(向后兼容,重定向到 extract-chat-history) ocrChatHistory: (ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath) => ipcRenderer.invoke('ocr-chat-history', ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath), // 获取最后一条消息(带发送者信息) getLastChatMessage: (ipPort, friendAvatarPath, myAvatarPath) => ipcRenderer.invoke('get-last-chat-message', ipPort, friendAvatarPath, myAvatarPath), // 保存工作流(支持图片) saveWorkflow: (workflowJson, imagesData) => ipcRenderer.invoke('save-workflow', workflowJson, imagesData), // 删除工作流 deleteWorkflow: (folderName) => ipcRenderer.invoke('delete-workflow', folderName), // 读取 bp.json 文件(蓝图位置信息) readBlueprintJson: (folderName) => ipcRenderer.invoke('read-blueprint-json', folderName), // 保存 bp.json 文件(蓝图位置信息) saveBlueprintJson: (folderName, blueprintData) => ipcRenderer.invoke('save-blueprint-json', folderName, blueprintData), // 确保目录存在 ensureDirectory: (dirPath) => ipcRenderer.invoke('ensure-directory', dirPath), // 写入文本文件 writeTextFile: (filePath, content) => ipcRenderer.invoke('write-text-file', filePath, content), // 读取文本文件 readTextFile: (filePath) => ipcRenderer.invoke('read-text-file', filePath), // 读取图片文件为 base64 readImageFileAsBase64: (filePath) => ipcRenderer.invoke('read-image-file-as-base64', filePath), // 保存 base64 图片到文件 saveBase64Image: (base64Data, savePath) => ipcRenderer.invoke('save-base64-image', base64Data, savePath), // 追加日志到文件 appendLog: (workflowFolderPath, logMessage) => ipcRenderer.invoke('append-log', workflowFolderPath, logMessage), // 保存聊天记录 saveChatHistory: (workflowFolderPath, historyData) => ipcRenderer.invoke('save-chat-history', workflowFolderPath, historyData), saveChatHistoryTxt: (workflowFolderPath, messages) => ipcRenderer.invoke('save-chat-history-txt', workflowFolderPath, messages), // 保存聊天记录总结 saveChatHistorySummary: (workflowFolderPath, summary) => ipcRenderer.invoke('save-chat-history-summary', workflowFolderPath, summary), // 获取聊天记录总结 getChatHistorySummary: (workflowFolderPath) => ipcRenderer.invoke('get-chat-history-summary', workflowFolderPath), // 读取最新聊天记录的所有消息 readLatestChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-latest-chat-history', workflowFolderPath), // 读取最新聊天记录的最后一条消息 readLastMessage: (workflowFolderPath) => ipcRenderer.invoke('read-last-message', workflowFolderPath), // 读取聊天记录(合并所有历史文件) readChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-chat-history', workflowFolderPath), // 向后兼容 readAllChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-chat-history', workflowFolderPath), // 检测 Python 环境 checkPythonEnvironment: () => ipcRenderer.invoke('check-python-environment'), });