preload.mjs 980 B

123456789101112131415161718192021222324
  1. const { contextBridge, ipcRenderer } = require("electron");
  2. // 使用 contextBridge 暴露渲染进程 API
  3. contextBridge.exposeInMainWorld("electronAPI", {
  4. // sendMessage: 用于发送消息到主进程
  5. sendMessage: (message) => {
  6. ipcRenderer.send("message", message);
  7. },
  8. // runPythonScript: 用于运行 Python 脚本
  9. runPythonExe: async (args) => {
  10. return await ipcRenderer.invoke("run-python-exe", args);
  11. },
  12. callPythonAPI: (apiName, params) =>
  13. ipcRenderer.invoke("run-python-exe", apiName, params),
  14. // 可以考虑增加更多的方法,如接收来自主进程的消息
  15. receiveMessage: (callback) => {
  16. ipcRenderer.on("response", (event, data) => {
  17. callback(data);
  18. });
  19. },
  20. getFilePath: () => ipcRenderer.invoke("get-file-path"), // ✅ 返回数组
  21. getInstallPath: () => ipcRenderer.invoke("get-install-path"), // ✅ 返回数组
  22. readCsv: (filePath) => ipcRenderer.invoke("read-csv", filePath),
  23. });