preload.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const { remote, ipcRenderer } = require('electron');
  2. const fs = require('fs');
  3. const { tr } = require('../core/translate');
  4. const { readJson } = require('../core/utils');
  5. // 监听 Electron 发送的 IPC,并将其转发到 hostAPI 中设置的监听器中
  6. let panelWinStateChangedListener = null;
  7. ipcRenderer.on("im-plugin:panel-win-state-changed", (event, eventName) => {
  8. panelWinStateChangedListener && panelWinStateChangedListener(eventName);
  9. });
  10. global.hostAPI = {
  11. imPluginVersion: readJson(`${__dirname}/../package.json`).version,
  12. sendToHost: (channel, param) => {
  13. if (channel === "new-msg") {
  14. ipcRenderer.sendToHost("new-msg", {
  15. newMsgCount: param,
  16. });
  17. } else if (channel === "download") {
  18. let options = {};
  19. if (param.type === "image") {
  20. options = {
  21. title: tr("save-image"),
  22. defaultPath: param.fileName,
  23. filters: [
  24. {
  25. name: tr("image-filter-name"),
  26. extensions: ["jpg", "jpeg", "png", "gif"],
  27. },
  28. ],
  29. };
  30. } else {
  31. options = {
  32. title: tr("save-file"),
  33. defaultPath: param.fileName,
  34. filters: [{ name: tr("file-filter-name"), extensions: ["*"] }],
  35. };
  36. }
  37. remote.dialog.showSaveDialog(options, (filePath) => {
  38. if (!filePath) return;
  39. ipcRenderer.sendToHost("download", {
  40. url: param.url,
  41. path: filePath,
  42. });
  43. });
  44. }
  45. },
  46. isReady(isReady) {
  47. ipcRenderer.sendToHost("is-ready", isReady);
  48. },
  49. setWindowListener: (listener) => {
  50. panelWinStateChangedListener = listener;
  51. },
  52. selectFile(type) {
  53. let result;
  54. if (type === "image") {
  55. result = remote.dialog.showOpenDialog({
  56. title: tr("select-image"),
  57. properties: ["openFile"],
  58. filters: [
  59. {
  60. name: tr("image-filter-name"),
  61. extensions: ["jpg", "jpeg", "png", "gif"],
  62. },
  63. ],
  64. });
  65. } else {
  66. result = remote.dialog.showOpenDialog({
  67. title: tr("select-file"),
  68. properties: ["openFile"],
  69. filters: [
  70. {
  71. name: tr("file-filter-name"),
  72. extensions: ["*"],
  73. },
  74. ],
  75. });
  76. }
  77. function getFileByPath(path) {
  78. const fileName = path.split("/").slice(-1)[0];
  79. const fileBuffer = fs.readFileSync(path);
  80. const file = new File([fileBuffer], fileName);
  81. return file;
  82. }
  83. if (!result) {
  84. return Promise.resolve("");
  85. }
  86. if (result.then) {
  87. return result.then((res) => {
  88. if (res.canceled) {
  89. return "";
  90. }
  91. // 单选
  92. const filePath = res.filePaths[0];
  93. return getFileByPath(filePath);
  94. });
  95. }
  96. // 单选
  97. const filePath = result[0];
  98. return Promise.resolve(getFileByPath(filePath));
  99. },
  100. };