main.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. "use strict";
  2. const { app, BrowserWindow, ipcMain, shell, session, Menu, Tray, nativeImage } = require("electron");
  3. const { join } = require("path");
  4. require("fs");
  5. let mainWindow = null;
  6. let tray = null;
  7. let isQuitting = false;
  8. const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL;
  9. function getIconPath() {
  10. return VITE_DEV_SERVER_URL ? join(__dirname, "../public/icons/icon-256.png") : join(__dirname, "../dist/icons/icon-256.png");
  11. }
  12. function getTrayIconPath() {
  13. return VITE_DEV_SERVER_URL ? join(__dirname, "../public/icons/tray-icon.png") : join(__dirname, "../dist/icons/tray-icon.png");
  14. }
  15. function createTrayIcon() {
  16. const trayIconPath = getTrayIconPath();
  17. return nativeImage.createFromPath(trayIconPath);
  18. }
  19. function createTray() {
  20. const trayIcon = createTrayIcon();
  21. tray = new Tray(trayIcon);
  22. const contextMenu = Menu.buildFromTemplate([
  23. {
  24. label: "显示主窗口",
  25. click: () => {
  26. if (mainWindow) {
  27. mainWindow.show();
  28. mainWindow.focus();
  29. }
  30. }
  31. },
  32. {
  33. label: "最小化到托盘",
  34. click: () => {
  35. mainWindow == null ? void 0 : mainWindow.hide();
  36. }
  37. },
  38. { type: "separator" },
  39. {
  40. label: "退出",
  41. click: () => {
  42. isQuitting = true;
  43. app.quit();
  44. }
  45. }
  46. ]);
  47. tray.setToolTip("多平台媒体管理系统");
  48. tray.setContextMenu(contextMenu);
  49. tray.on("click", () => {
  50. if (mainWindow) {
  51. if (mainWindow.isVisible()) {
  52. mainWindow.focus();
  53. } else {
  54. mainWindow.show();
  55. mainWindow.focus();
  56. }
  57. }
  58. });
  59. tray.on("double-click", () => {
  60. if (mainWindow) {
  61. mainWindow.show();
  62. mainWindow.focus();
  63. }
  64. });
  65. }
  66. function createWindow() {
  67. Menu.setApplicationMenu(null);
  68. const iconPath = getIconPath();
  69. mainWindow = new BrowserWindow({
  70. width: 1400,
  71. height: 900,
  72. minWidth: 1200,
  73. minHeight: 700,
  74. icon: iconPath,
  75. webPreferences: {
  76. preload: join(__dirname, "preload.js"),
  77. nodeIntegration: false,
  78. contextIsolation: true,
  79. webviewTag: true
  80. // 启用 webview 标签
  81. },
  82. frame: false,
  83. // 无边框窗口,自定义标题栏
  84. transparent: false,
  85. backgroundColor: "#f0f2f5",
  86. show: false
  87. });
  88. mainWindow.once("ready-to-show", () => {
  89. mainWindow == null ? void 0 : mainWindow.show();
  90. setupWindowEvents();
  91. });
  92. if (VITE_DEV_SERVER_URL) {
  93. mainWindow.loadURL(VITE_DEV_SERVER_URL);
  94. mainWindow.webContents.openDevTools();
  95. } else {
  96. mainWindow.loadFile(join(__dirname, "../dist/index.html"));
  97. }
  98. mainWindow.webContents.setWindowOpenHandler(({ url }) => {
  99. shell.openExternal(url);
  100. return { action: "deny" };
  101. });
  102. mainWindow.on("close", (event) => {
  103. if (!isQuitting) {
  104. event.preventDefault();
  105. mainWindow == null ? void 0 : mainWindow.hide();
  106. if (tray && !app.isPackaged) ;
  107. }
  108. });
  109. mainWindow.on("closed", () => {
  110. mainWindow = null;
  111. });
  112. }
  113. const gotTheLock = app.requestSingleInstanceLock();
  114. if (!gotTheLock) {
  115. app.quit();
  116. } else {
  117. app.on("second-instance", () => {
  118. if (mainWindow) {
  119. mainWindow.show();
  120. if (mainWindow.isMinimized()) mainWindow.restore();
  121. mainWindow.focus();
  122. }
  123. });
  124. app.whenReady().then(() => {
  125. createTray();
  126. createWindow();
  127. app.on("activate", () => {
  128. if (BrowserWindow.getAllWindows().length === 0) {
  129. createWindow();
  130. } else if (mainWindow) {
  131. mainWindow.show();
  132. }
  133. });
  134. });
  135. }
  136. app.on("window-all-closed", () => {
  137. });
  138. app.on("before-quit", () => {
  139. isQuitting = true;
  140. });
  141. app.on("quit", () => {
  142. if (tray) {
  143. tray.destroy();
  144. tray = null;
  145. }
  146. });
  147. ipcMain.handle("get-app-version", () => {
  148. return app.getVersion();
  149. });
  150. ipcMain.handle("get-platform", () => {
  151. return process.platform;
  152. });
  153. ipcMain.on("window-minimize", () => {
  154. mainWindow == null ? void 0 : mainWindow.minimize();
  155. });
  156. ipcMain.on("window-maximize", () => {
  157. if (mainWindow == null ? void 0 : mainWindow.isMaximized()) {
  158. mainWindow.unmaximize();
  159. } else {
  160. mainWindow == null ? void 0 : mainWindow.maximize();
  161. }
  162. });
  163. ipcMain.on("window-close", () => {
  164. mainWindow == null ? void 0 : mainWindow.hide();
  165. });
  166. ipcMain.on("app-quit", () => {
  167. isQuitting = true;
  168. app.quit();
  169. });
  170. ipcMain.handle("window-is-maximized", () => {
  171. return (mainWindow == null ? void 0 : mainWindow.isMaximized()) || false;
  172. });
  173. function setupWindowEvents() {
  174. mainWindow == null ? void 0 : mainWindow.on("maximize", () => {
  175. mainWindow == null ? void 0 : mainWindow.webContents.send("window-maximized", true);
  176. });
  177. mainWindow == null ? void 0 : mainWindow.on("unmaximize", () => {
  178. mainWindow == null ? void 0 : mainWindow.webContents.send("window-maximized", false);
  179. });
  180. }
  181. ipcMain.handle("get-webview-cookies", async (_event, partition, url) => {
  182. try {
  183. const ses = session.fromPartition(partition);
  184. const cookies = await ses.cookies.get({ url });
  185. return cookies;
  186. } catch (error) {
  187. console.error("获取 cookies 失败:", error);
  188. return [];
  189. }
  190. });
  191. ipcMain.handle("clear-webview-cookies", async (_event, partition) => {
  192. try {
  193. const ses = session.fromPartition(partition);
  194. await ses.clearStorageData({ storages: ["cookies"] });
  195. return true;
  196. } catch (error) {
  197. console.error("清除 cookies 失败:", error);
  198. return false;
  199. }
  200. });
  201. //# sourceMappingURL=main.js.map