preload.cjs 5.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const { contextBridge, ipcRenderer } = require('electron');
  2. contextBridge.exposeInMainWorld('electronAPI', {
  3. getAdbPathConfig: () => ipcRenderer.invoke('get-adb-path-config'),
  4. scanADBDevices: () => ipcRenderer.invoke('scan-adb-devices'),
  5. getADBDevices: () => ipcRenderer.invoke('get-adb-devices'),
  6. connectADBDevice: (ipPort) => ipcRenderer.invoke('connect-adb-device', ipPort),
  7. getDeviceResolution: (ipPort) => ipcRenderer.invoke('get-device-resolution', ipPort),
  8. captureScreenshot: (ipPort, options) => ipcRenderer.invoke('capture-screenshot', ipPort, options),
  9. getCachedScreenshot: (ipPort) => ipcRenderer.invoke('get-cached-screenshot', ipPort),
  10. sendTap: (ipPort, x, y) => ipcRenderer.invoke('send-tap', ipPort, x, y),
  11. sendSwipe: (ipPort, x1, y1, x2, y2, duration) => ipcRenderer.invoke('send-swipe', ipPort, x1, y1, x2, y2, duration),
  12. sendText: (ipPort, text) => ipcRenderer.invoke('send-text', ipPort, text),
  13. sendScroll: (ipPort, direction, width, height, scrollDistance, duration) => ipcRenderer.invoke('send-scroll', ipPort, direction, width, height, scrollDistance, duration),
  14. sendKeyEvent: (ipPort, keyCode) => ipcRenderer.invoke('send-key-event', ipPort, keyCode),
  15. // 监听设备发现事件
  16. onDeviceFound: (callback) => {
  17. ipcRenderer.on('device-found', (event, device) => callback(device));
  18. },
  19. // 移除设备发现事件监听器
  20. removeDeviceFoundListener: () => {
  21. ipcRenderer.removeAllListeners('device-found');
  22. },
  23. // 获取 static 目录下的文件夹列表
  24. getStaticFolders: () => ipcRenderer.invoke('get-static-folders'),
  25. // 图像匹配并获取坐标
  26. matchImageAndGetCoordinate: (ipPort, templateImagePath) => ipcRenderer.invoke('match-image-and-get-coordinate', ipPort, templateImagePath),
  27. // 图像区域定位:在完整截图中查找区域截图位置,返回四个顶点坐标
  28. matchImageRegionLocation: (screenshotPath, regionPath, device) => ipcRenderer.invoke('match-image-region-location', screenshotPath, regionPath, device),
  29. // 裁剪并保存图片区域
  30. cropAndSaveImage: (imagePath, x, y, width, height, savePath) => ipcRenderer.invoke('crop-and-save-image', imagePath, x, y, width, height, savePath),
  31. // 读取 processing.json 文件
  32. readProcessingJson: (folderName) => ipcRenderer.invoke('read-processing-json', folderName),
  33. // 文字识别并获取坐标
  34. findTextAndGetCoordinate: (ipPort, targetText) => ipcRenderer.invoke('find-text-and-get-coordinate', ipPort, targetText),
  35. // OCR识别最后一条消息(兼容旧API)
  36. ocrLastMessage: (ipPort, method, avatarPath, area, folderPath) => ipcRenderer.invoke('ocr-last-message', ipPort, method, avatarPath, area, folderPath),
  37. // 提取聊天记录
  38. extractChatHistory: (ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath, region = null, friendRgb = null, myRgb = null) => ipcRenderer.invoke('extract-chat-history', ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath, region, friendRgb, myRgb),
  39. // OCR 聊天记录(向后兼容,重定向到 extract-chat-history)
  40. ocrChatHistory: (ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath) => ipcRenderer.invoke('ocr-chat-history', ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath),
  41. // 获取最后一条消息(带发送者信息)
  42. getLastChatMessage: (ipPort, friendAvatarPath, myAvatarPath) => ipcRenderer.invoke('get-last-chat-message', ipPort, friendAvatarPath, myAvatarPath),
  43. // 保存工作流(支持图片)
  44. saveWorkflow: (workflowJson, imagesData) => ipcRenderer.invoke('save-workflow', workflowJson, imagesData),
  45. // 删除工作流
  46. deleteWorkflow: (folderName) => ipcRenderer.invoke('delete-workflow', folderName),
  47. // 确保目录存在
  48. ensureDirectory: (dirPath) => ipcRenderer.invoke('ensure-directory', dirPath),
  49. // 写入文本文件
  50. writeTextFile: (filePath, content) => ipcRenderer.invoke('write-text-file', filePath, content),
  51. // 读取文本文件
  52. readTextFile: (filePath) => ipcRenderer.invoke('read-text-file', filePath),
  53. // 读取图片文件为 base64
  54. readImageFileAsBase64: (filePath) => ipcRenderer.invoke('read-image-file-as-base64', filePath),
  55. // 保存 base64 图片到文件
  56. saveBase64Image: (base64Data, savePath) => ipcRenderer.invoke('save-base64-image', base64Data, savePath),
  57. // 追加日志到文件
  58. appendLog: (workflowFolderPath, logMessage) => ipcRenderer.invoke('append-log', workflowFolderPath, logMessage),
  59. // 保存聊天记录
  60. saveChatHistory: (workflowFolderPath, historyData) => ipcRenderer.invoke('save-chat-history', workflowFolderPath, historyData),
  61. saveChatHistoryTxt: (workflowFolderPath, messages) => ipcRenderer.invoke('save-chat-history-txt', workflowFolderPath, messages),
  62. // 保存聊天记录总结
  63. saveChatHistorySummary: (workflowFolderPath, summary) => ipcRenderer.invoke('save-chat-history-summary', workflowFolderPath, summary),
  64. // 获取聊天记录总结
  65. getChatHistorySummary: (workflowFolderPath) => ipcRenderer.invoke('get-chat-history-summary', workflowFolderPath),
  66. // 读取最新聊天记录的所有消息
  67. readLatestChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-latest-chat-history', workflowFolderPath),
  68. // 读取最新聊天记录的最后一条消息
  69. readLastMessage: (workflowFolderPath) => ipcRenderer.invoke('read-last-message', workflowFolderPath),
  70. // 读取聊天记录(合并所有历史文件)
  71. readChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-chat-history', workflowFolderPath),
  72. // 向后兼容
  73. readAllChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-chat-history', workflowFolderPath),
  74. });