preload.cjs 6.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. sendSystemKey: (ipPort, keyCode) => ipcRenderer.invoke('send-system-key', ipPort, keyCode),
  16. sendBackKey: (ipPort) => ipcRenderer.invoke('send-back-key', ipPort),
  17. sendHomeKey: (ipPort) => ipcRenderer.invoke('send-home-key', ipPort),
  18. sendRecentAppsKey: (ipPort) => ipcRenderer.invoke('send-recent-apps-key', ipPort),
  19. // 监听设备发现事件
  20. onDeviceFound: (callback) => {
  21. ipcRenderer.on('device-found', (event, device) => callback(device));
  22. },
  23. // 移除设备发现事件监听器
  24. removeDeviceFoundListener: () => {
  25. ipcRenderer.removeAllListeners('device-found');
  26. },
  27. // 获取 static 目录下的文件夹列表
  28. getStaticFolders: () => ipcRenderer.invoke('get-static-folders'),
  29. // 图像匹配并获取坐标
  30. matchImageAndGetCoordinate: (ipPort, templateImagePath) => ipcRenderer.invoke('match-image-and-get-coordinate', ipPort, templateImagePath),
  31. // 图像区域定位:在完整截图中查找区域截图位置,返回四个顶点坐标
  32. matchImageRegionLocation: (screenshotPath, regionPath, device) => ipcRenderer.invoke('match-image-region-location', screenshotPath, regionPath, device),
  33. // 裁剪并保存图片区域
  34. cropAndSaveImage: (imagePath, x, y, width, height, savePath) => ipcRenderer.invoke('crop-and-save-image', imagePath, x, y, width, height, savePath),
  35. // 读取 processing.json 文件
  36. readProcessingJson: (folderName) => ipcRenderer.invoke('read-processing-json', folderName),
  37. // 保存 processing.json 文件到现有工作流文件夹
  38. saveProcessingJson: (folderName, workflowData) => ipcRenderer.invoke('save-processing-json', folderName, workflowData),
  39. // 文字识别并获取坐标
  40. findTextAndGetCoordinate: (ipPort, targetText) => ipcRenderer.invoke('find-text-and-get-coordinate', ipPort, targetText),
  41. // OCR识别最后一条消息(兼容旧API)
  42. ocrLastMessage: (ipPort, method, avatarPath, area, folderPath) => ipcRenderer.invoke('ocr-last-message', ipPort, method, avatarPath, area, folderPath),
  43. // 提取聊天记录
  44. extractChatHistory: (ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath, region = null, friendRgb = null, myRgb = null) => ipcRenderer.invoke('extract-chat-history', ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath, region, friendRgb, myRgb),
  45. // OCR 聊天记录(向后兼容,重定向到 extract-chat-history)
  46. ocrChatHistory: (ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath) => ipcRenderer.invoke('ocr-chat-history', ipPort, friendAvatarPath, myAvatarPath, workflowFolderPath),
  47. // 获取最后一条消息(带发送者信息)
  48. getLastChatMessage: (ipPort, friendAvatarPath, myAvatarPath) => ipcRenderer.invoke('get-last-chat-message', ipPort, friendAvatarPath, myAvatarPath),
  49. // 保存工作流(支持图片)
  50. saveWorkflow: (workflowJson, imagesData) => ipcRenderer.invoke('save-workflow', workflowJson, imagesData),
  51. // 删除工作流
  52. deleteWorkflow: (folderName) => ipcRenderer.invoke('delete-workflow', folderName),
  53. // 读取 bp.json 文件(蓝图位置信息)
  54. readBlueprintJson: (folderName) => ipcRenderer.invoke('read-blueprint-json', folderName),
  55. // 保存 bp.json 文件(蓝图位置信息)
  56. saveBlueprintJson: (folderName, blueprintData) => ipcRenderer.invoke('save-blueprint-json', folderName, blueprintData),
  57. // 确保目录存在
  58. ensureDirectory: (dirPath) => ipcRenderer.invoke('ensure-directory', dirPath),
  59. // 写入文本文件
  60. writeTextFile: (filePath, content) => ipcRenderer.invoke('write-text-file', filePath, content),
  61. // 读取文本文件
  62. readTextFile: (filePath) => ipcRenderer.invoke('read-text-file', filePath),
  63. // 读取图片文件为 base64
  64. readImageFileAsBase64: (filePath) => ipcRenderer.invoke('read-image-file-as-base64', filePath),
  65. // 保存 base64 图片到文件
  66. saveBase64Image: (base64Data, savePath) => ipcRenderer.invoke('save-base64-image', base64Data, savePath),
  67. // 追加日志到文件
  68. appendLog: (workflowFolderPath, logMessage) => ipcRenderer.invoke('append-log', workflowFolderPath, logMessage),
  69. // 保存聊天记录
  70. saveChatHistory: (workflowFolderPath, historyData) => ipcRenderer.invoke('save-chat-history', workflowFolderPath, historyData),
  71. saveChatHistoryTxt: (workflowFolderPath, messages) => ipcRenderer.invoke('save-chat-history-txt', workflowFolderPath, messages),
  72. // 保存聊天记录总结
  73. saveChatHistorySummary: (workflowFolderPath, summary) => ipcRenderer.invoke('save-chat-history-summary', workflowFolderPath, summary),
  74. // 获取聊天记录总结
  75. getChatHistorySummary: (workflowFolderPath) => ipcRenderer.invoke('get-chat-history-summary', workflowFolderPath),
  76. // 读取最新聊天记录的所有消息
  77. readLatestChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-latest-chat-history', workflowFolderPath),
  78. // 读取最新聊天记录的最后一条消息
  79. readLastMessage: (workflowFolderPath) => ipcRenderer.invoke('read-last-message', workflowFolderPath),
  80. // 读取聊天记录(合并所有历史文件)
  81. readChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-chat-history', workflowFolderPath),
  82. // 向后兼容
  83. readAllChatHistory: (workflowFolderPath) => ipcRenderer.invoke('read-chat-history', workflowFolderPath),
  84. });