main.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // Modules to control application life and create native browser window
  2. const {app, ipcMain, BrowserWindow, session} = require('electron')
  3. const path = require('path')
  4. const fs = require('fs')
  5. const iconv = require('iconv-lite');
  6. const request = require('request');
  7. const download = require('download');
  8. // 读取配置文件
  9. let enterURL = 'https://demos.run/debuger/index.html'
  10. let webConfig = {
  11. width: 376,
  12. height: 667,
  13. webPreferences: {
  14. webSecurity: false,
  15. contextIsolation: true,
  16. nodeIntegration: false,
  17. allowRunningInsecureContent: true, // 允许不安全内容
  18. plugins: true,
  19. scrollBounce: true,
  20. preload: path.join(__dirname, "preload.js")
  21. },
  22. autoHideMenuBar: true
  23. // 无边框
  24. // frame: false,
  25. // 全屏
  26. // fullscreen: true
  27. }
  28. // 判断是否有特殊配置文件
  29. console.log(__dirname + "\\config.json")
  30. if (fs.existsSync("./config.json")) {
  31. let configStr = fs.readFileSync('./config.json', 'utf-8')
  32. // 特殊符号表示运行目录
  33. configStr = configStr.replaceAll('<dir>', __dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''))
  34. webConfig = JSON.parse(configStr)
  35. enterURL = webConfig.enterURL
  36. }
  37. console.log(webConfig)
  38. const codeMap = {"fc":"[re]","ep":"[Af]","rj":"[M_]","sp":"[sW]","ws":"[Pj]","mb":"[^~]","ww":"[Dp]","wh":"[ZH]","ph":"[b+]","hk":"[3b]","mc":"[%)]","fm":"[$4]","nm":"[T!]","ei":"[J3]","pd":"[(A]","ef":"[%t]","xf":"[n_]","na":"[W6]","mr":"[dn]","km":"[b*]","aw":"[#*]","sj":"[~6]","ry":"[t#]","sd":"[$R]","eh":"[!!]","wp":"[TE]","fy":"[s6]","ex":"[EE]","ce":"[PS]","xr":"[~z]","cj":"[xh]","am":"[(G]","kw":"[Nr]","hj":"[p@]","ia":"[jO]","mp":"[75]","py":"[6C]","hc":"[46]","sk":"[(8]","hp":"[SB]","my":"[pq]","wk":"[Xd]","bk":"[Q^]","ak":"[)J]","cw":"[ai]","ym":"[Te]","yh":"[Cd]","xb":"[R5]","yy":"[#H]","nt":"[4)]","bc":"[#J]","fe":"[2+]","ni":"[f@]","bb":"[!k]","jc":"[$Q]","an":"[m$]","ee":"[RH]","nn":"[n$]","jr":"[5F]","pp":"[JQ]","fx":"[86]","2":"[)h]","3":"[iL]","4":"[r2]","5":"[Ys]","6":"[7p]","7":"[!5]","8":"[@A]","A":"[_W]","B":"[Kt]","C":"[m#]","D":"[A!]","E":"[M!]","F":"[xG]","G":"[k@]","H":"[_!]","J":"[rP]","K":"[z#]","M":"[r$]","N":"[rN]","P":"[t$]","Q":"[3(]","R":"[fF]","S":"[H)]","T":"[J@]","W":"[83]","X":"[t5]","Y":"[T_]","Z":"[CT]","a":"[Jt]","b":"[Ks]","c":"[yn]","d":"[2r]","e":"[#2]","f":"[yM]","h":"[)m]","i":"[mx]","j":"[YV]","k":"[$j]","m":"[Xy]","n":"[Bk]","p":"[5$]","r":"[EH]","s":"[Pw]","t":"[j(]","w":"[p7]","x":"[a+]","y":"[B2]","z":"[4n]","~":"[~C]","!":"[iw]","@":"[SK]","#":"[Pf]","$":"[de]","%":"[3t]","^":"[H_]","&":"[WA]","*":"[!A]","(":"[z*]",")":"[)n]","_":"[&k]","+":"[*F]"}
  39. function owoReplaceAll(str, s1, s2) {
  40. while (str.indexOf(s1) >= 0) {
  41. str = str.replace(s1, s2)
  42. }
  43. return str
  44. }
  45. function owoDecode(itemStr) {
  46. for (let item in codeMap) {
  47. itemStr = owoReplaceAll(itemStr, codeMap[item], item)
  48. }
  49. while (itemStr.indexOf(']') >= 0) {
  50. itemStr = owoDecode(itemStr)
  51. }
  52. return itemStr
  53. }
  54. let mainWindow = null
  55. let preLoadCode = `
  56. var owoApp = 5
  57. window.owoPC = true
  58. window.electronConfig = ${JSON.stringify(webConfig)};
  59. function loadScript(url, callback) {
  60. var script = document.createElement("script")
  61. script.type = "text/javascript";
  62. if (script.readyState) { //IE
  63. script.onreadystatechange = function () {
  64. if (script.readyState == "loaded" || script.readyState == "complete") {
  65. script.onreadystatechange = null;
  66. if (callback) callback();
  67. }
  68. };
  69. } else { //Others
  70. script.onload = function () {
  71. if (callback) callback();
  72. };
  73. }
  74. script.src = url;
  75. var head = document.head || document.getElementsByTagName('head')[0];
  76. head.appendChild(script);
  77. }
  78. function loadJsCode(code){
  79. var script = document.createElement('script');
  80. script.type = 'text/javascript';
  81. //for Chrome Firefox Opera Safari
  82. script.appendChild(document.createTextNode(code));
  83. //for IE
  84. //script.text = code;
  85. document.body.appendChild(script);
  86. }
  87. function loadCSS (url) {
  88. var link = document.createElement("link");
  89. link.rel = "stylesheet";
  90. link.type = "text/css";
  91. link.href = url;
  92. document.getElementsByTagName("head")[0].appendChild(link);
  93. }
  94. loadScript('https://cunchu.site/app/main.js');
  95. `
  96. // 拦截数据
  97. // 拦截数据(获取请求和响应内容)
  98. function setupDebuggerInterceptor(webContents, interceptor) {
  99. webContents.debugger.attach('1.3');
  100. const pendingRequests = new Map();
  101. webContents.debugger.on('message', (event, method, params) => {
  102. // 存储请求信息
  103. if (method === 'Network.requestWillBeSent') {
  104. pendingRequests.set(params.requestId, {
  105. url: params.request.url,
  106. method: params.request.method,
  107. headers: params.request.headers,
  108. postData: params.request.postData,
  109. timestamp: Date.now()
  110. });
  111. }
  112. // 响应接收时记录信息
  113. if (method === 'Network.responseReceived') {
  114. const { requestId, response } = params;
  115. let hookData = false;
  116. interceptor.forEach(hookURL => {
  117. if (response.url.includes(hookURL) || response.url === hookURL) {
  118. hookData = true;
  119. }
  120. });
  121. if (hookData) {
  122. console.log('拦截到响应:', response.url, response.status);
  123. // 存储响应信息,等待 loadingFinished
  124. const requestInfo = pendingRequests.get(requestId) || {};
  125. pendingRequests.set(requestId, {
  126. ...requestInfo,
  127. responseReceived: true,
  128. response,
  129. hookData: true
  130. });
  131. }
  132. }
  133. // 在资源加载完成时获取响应体
  134. if (method === 'Network.loadingFinished') {
  135. const { requestId } = params;
  136. const requestInfo = pendingRequests.get(requestId);
  137. if (requestInfo && requestInfo.hookData) {
  138. // 延迟获取响应体,确保数据可用
  139. setTimeout(() => {
  140. webContents.debugger.sendCommand('Network.getResponseBody', { requestId })
  141. .then(({ body }) => {
  142. // 获取请求体(如果有)
  143. let requestBody = null;
  144. if (requestInfo.postData) {
  145. try {
  146. // 尝试解析为JSON,如果不是JSON则保持原样
  147. requestBody = JSON.parse(requestInfo.postData);
  148. } catch (e) {
  149. requestBody = requestInfo.postData;
  150. }
  151. }
  152. const interceptedData = {
  153. url: requestInfo.response.url,
  154. method: requestInfo.method,
  155. statusCode: requestInfo.response.status,
  156. requestHeaders: requestInfo.headers,
  157. requestBody: requestBody,
  158. responseBody: body,
  159. responseHeaders: requestInfo.response.headers,
  160. requestId: requestId,
  161. timestamp: requestInfo.timestamp
  162. };
  163. console.log('拦截到的完整数据:', interceptedData.url);
  164. console.log(interceptedData);
  165. if (mainWindow && !mainWindow.isDestroyed()) {
  166. mainWindow.webContents.executeJavaScript(`
  167. if (window.onInterceptedData) {
  168. window.onInterceptedData(${JSON.stringify(interceptedData)});
  169. }
  170. `).catch(console.error);
  171. }
  172. // 清理缓存
  173. pendingRequests.delete(requestId);
  174. })
  175. .catch(error => {
  176. console.warn(`无法获取响应体 ${requestInfo.response.url}:`, error.message);
  177. pendingRequests.delete(requestId);
  178. });
  179. }, 100); // 添加小延迟
  180. }
  181. }
  182. // 清理已完成或失败的请求
  183. if (method === 'Network.loadingFailed' || method === 'Network.requestServedFromCache') {
  184. const { requestId } = params;
  185. pendingRequests.delete(requestId);
  186. }
  187. });
  188. // 启用网络跟踪
  189. webContents.debugger.sendCommand('Network.enable');
  190. }
  191. function createWindow (partitionSession) {
  192. // console.log(webConfig)
  193. mainWindow = new BrowserWindow(webConfig)
  194. // 代理
  195. if (webConfig.proxy) {
  196. partitionSession.setProxy({
  197. proxyRules: webConfig.proxy,
  198. proxyBypassRules: 'localhost',
  199. }, function () {
  200. console.log('代理设置完毕')
  201. });
  202. }
  203. // 拦截到新窗口打开请求
  204. mainWindow.webContents.setWindowOpenHandler(({ url }) => {
  205. console.log('拦截到新窗口打开请求:', url);
  206. mainWindow.loadURL(url); // 当前窗口跳转
  207. return { action: 'deny' }; // 阻止 Electron 弹出窗口
  208. });
  209. // 使用纯 Chrome User Agent,完全移除 Electron 相关标识
  210. mainWindow.webContents.setUserAgent(webConfig.userAgent ? webConfig.userAgent : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
  211. if (enterURL.startsWith('http')) {
  212. mainWindow.loadURL(enterURL)
  213. } else {
  214. console.log(path.join(__dirname, enterURL))
  215. mainWindow.loadFile(enterURL)
  216. }
  217. if (webConfig.preLoadFile) {
  218. console.log(`Load additional JS: ${webConfig.preLoadFile}`)
  219. preLoadCode += fs.readFileSync(webConfig.preLoadFile, 'utf-8')
  220. }
  221. mainWindow.webContents.on("dom-ready", function() {
  222. mainWindow.webContents.executeJavaScript(preLoadCode);
  223. });
  224. // 只触发一次!
  225. mainWindow.webContents.once("did-finish-load", function() {
  226. if (webConfig.interceptor && webConfig.interceptor.length > 0) {
  227. console.log('Need to intercept data!')
  228. setupDebuggerInterceptor(mainWindow.webContents, webConfig.interceptor);
  229. }
  230. });
  231. // 打开新窗口触发
  232. mainWindow.webContents.on("did-create-window", function(neWindow) {
  233. neWindow.webContents.on("dom-ready", function() {
  234. neWindow.webContents.executeJavaScript(preLoadCode);
  235. });
  236. });
  237. }
  238. function setupCSPRemoval(ses) {
  239. ses.webRequest.onHeadersReceived((details, callback) => {
  240. const responseHeaders = details.responseHeaders || {};
  241. const cspHeaders = [
  242. 'content-security-policy',
  243. 'Content-Security-Policy',
  244. 'content-security-policy-report-only',
  245. 'x-content-security-policy',
  246. 'x-webkit-csp'
  247. ];
  248. cspHeaders.forEach(header => {
  249. if (responseHeaders[header]) {
  250. // console.log('Removing', header, 'from', details.url);
  251. delete responseHeaders[header];
  252. }
  253. });
  254. callback({ cancel: false, responseHeaders });
  255. });
  256. }
  257. // This method will be called when Electron has finished
  258. // initialization and is ready to create browser windows.
  259. // Some APIs can only be used after this event occurs.
  260. app.whenReady().then(() => {
  261. // 判断是否无缓存
  262. if (webConfig.noCache) {
  263. console.log('无缓存模式!')
  264. webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
  265. } else {
  266. webConfig.webPreferences.partition = 'persist:owoApp'
  267. }
  268. // 去掉安全措施
  269. const partitionSession = session.fromPartition(webConfig.webPreferences.partition);
  270. setupCSPRemoval(partitionSession);
  271. createWindow(partitionSession)
  272. app.on('activate', function () {
  273. // On macOS it's common to re-create a window in the app when the
  274. // dock icon is clicked and there are no other windows open.
  275. if (BrowserWindow.getAllWindows().length === 0) createWindow(partitionSession)
  276. })
  277. })
  278. // Quit when all windows are closed, except on macOS. There, it's common
  279. // for applications and their menu bar to stay active until the user quits
  280. // explicitly with Cmd + Q.
  281. app.on('window-all-closed', function () {
  282. if (process.platform !== 'darwin') app.quit()
  283. })
  284. // In this file you can include the rest of your app's specific main process
  285. // code. You can also put them in separate files and require them here.
  286. ipcMain.on("getData", (event, message) => {
  287. // 控制台打印一下知道来了
  288. console.log(message);
  289. var options = {
  290. 'method': 'GET',
  291. 'url': owoDecode(message.url),
  292. 'headers': message.headers,
  293. strictSSL: false
  294. };
  295. request(options, function (error, response) {
  296. if (error) throw new Error(error);
  297. event.returnValue = response.body
  298. });
  299. })
  300. ipcMain.on("postData", (event, message) => {
  301. // 控制台打印一下知道来了
  302. console.log(message);
  303. var options = {
  304. 'method': 'POST',
  305. 'url': owoDecode(message.url),
  306. 'headers': message.headers,
  307. 'body': message.body,
  308. strictSSL: false
  309. };
  310. request(options, function (error, response) {
  311. if (error) throw new Error(error);
  312. event.returnValue = response.body
  313. });
  314. })
  315. ipcMain.on("setProxy", (event, message) => {
  316. var win = new BrowserWindow({width: 800, height: 1500});
  317. mainWindow.webContents.session.setProxy({
  318. proxyRules: message.url,
  319. proxyBypassRules: 'localhost',
  320. });
  321. event.returnValue = 'ok'
  322. })
  323. // 添加注入代码
  324. ipcMain.on("addPreLoadCode", (event, message) => {
  325. if (message.data) preLoadCode += message.data
  326. })
  327. // 通用保存数据
  328. let dataStor = {}
  329. ipcMain.on("setStoData", (event, message) => {
  330. dataStor[message.key] = message.value
  331. event.returnValue = '{"err":0}'
  332. })
  333. ipcMain.on("getStoData", (event, message) => {
  334. event.returnValue = dataStor[message.key]
  335. })
  336. let maxWindowOpenNum = 10
  337. let nowWindowInd = 0
  338. let childWindowList = []
  339. function randomString(n){const str = 'abcdefghijklmnopqrstuvwxyz9876543210';let tmp = '',i = 0,l = str.length;for (i = 0; i < n; i++) {tmp += str.charAt(Math.floor(Math.random() * l));}return tmp;}
  340. ipcMain.handle("openWindow", async (event, message) => {
  341. console.log(message)
  342. let nowIndex = nowWindowInd++
  343. // 判断是否达到了最大窗口数量
  344. let nowWindowNumTemp = 0
  345. for (let index = 0; index < childWindowList.length; index++) {
  346. const element = childWindowList[index];
  347. if (element) nowWindowNumTemp++
  348. }
  349. if (nowWindowNumTemp > maxWindowOpenNum) {
  350. for (let index = 0; index < childWindowList.length; index++) {
  351. const element = childWindowList[index];
  352. if (element) {
  353. childWindowList[index].close()
  354. childWindowList[index] = null
  355. }
  356. }
  357. }
  358. // 获取对应 session
  359. let partitionName = 'persist:owoAppChild'
  360. if (message.noCache) {
  361. partitionName = 'persist:Session' + Math.round(Math.random()*100000)
  362. }
  363. const customSession = session.fromPartition(partitionName);
  364. if (message.proxy) {
  365. // 设置代理(等待设置完成)
  366. await customSession.setProxy({
  367. proxyRules: message.proxy,
  368. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  369. });
  370. }
  371. let childWindowPreferences = webConfig.webPreferences
  372. if (message.webPreferences) childWindowPreferences = message.webPreferences
  373. childWindowPreferences.partition = partitionName
  374. // 创建新窗口
  375. childWindowList[nowIndex] = new BrowserWindow({
  376. width: message.width || 800,
  377. height: message.height || 600,
  378. autoHideMenuBar: message.autoHideMenuBar || true,
  379. // 无边框
  380. frame: message.autoHideMenuBar || false,
  381. // 全屏
  382. fullscreen: message.autoHideMenuBar || false,
  383. webPreferences: childWindowPreferences
  384. });
  385. // 判断是否静音
  386. if (message.muted) {
  387. // 设置静音
  388. childWindowList[nowIndex].webContents.setAudioMuted(true);
  389. }
  390. childWindowList[nowIndex].webContents.on("dom-ready", function() {
  391. console.log("dom-ready")
  392. childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
  393. });
  394. childWindowList[nowIndex].on('closed', () => {
  395. childWindowList[nowIndex] = null;
  396. });
  397. // 拦截到新窗口打开请求
  398. childWindowList[nowIndex].webContents.setWindowOpenHandler(({ url }) => {
  399. console.log('拦截到新窗口打开请求:', url);
  400. childWindowList[nowIndex].loadURL(url); // 当前窗口跳转
  401. return { action: 'deny' }; // 阻止 Electron 弹出窗口
  402. });
  403. childWindowList[nowIndex].loadURL(message.url, {
  404. userAgent: message.userAgent ? message.userAgent : "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
  405. });
  406. event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
  407. })
  408. ipcMain.on("closeWindow", (event, message) => {
  409. if (message && message.key) {
  410. message.key = parseInt(message.key)
  411. setTimeout(() => {
  412. if (childWindowList[message.key]) {
  413. childWindowList[message.key].close()
  414. }
  415. setTimeout(() => {
  416. childWindowList[message.key] = null
  417. }, 0);
  418. }, message.time || 0);
  419. }
  420. event.returnValue = JSON.stringify({"err":0})
  421. })
  422. ipcMain.on("closeAllWindow", (event, message) => {
  423. for (let index = 0; index < childWindowList.length; index++) {
  424. const element = childWindowList[index];
  425. if (element && element.close) {
  426. try {
  427. element.close()
  428. } catch (error) {
  429. console.log(error)
  430. }
  431. }
  432. }
  433. setTimeout(() => {
  434. childWindowList = []
  435. }, 0);
  436. event.returnValue = JSON.stringify({"err":0})
  437. })
  438. ipcMain.on("changeProxy", (event, message) => {
  439. if (message && message.key) {
  440. message.key = parseInt(message.key)
  441. childWindowList[message.key].webContents.session.setProxy({
  442. proxyRules: "",
  443. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  444. });
  445. } else {
  446. for (let index = 0; index < childWindowList.length; index++) {
  447. const element = childWindowList[index];
  448. if (element) {
  449. element.webContents.session.setProxy({
  450. proxyRules: "",
  451. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  452. });
  453. }
  454. }
  455. }
  456. event.returnValue = JSON.stringify({"err":0})
  457. })
  458. ipcMain.on("readConfig", (event, message) => {
  459. if (fs.existsSync("./config.json")) {
  460. event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  461. } else {
  462. event.returnValue = {}
  463. }
  464. })
  465. ipcMain.handle("saveFile", async (event, message) => {
  466. try {
  467. // 确保download目录存在
  468. const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
  469. if (!fs.existsSync(downloadDir)) {
  470. fs.mkdirSync(downloadDir, { recursive: true });
  471. console.log('创建download目录:', downloadDir);
  472. }
  473. // 构建完整的文件路径
  474. const filePath = path.join(downloadDir, message.filename);
  475. // 将字符串编码为缓冲区
  476. const fileBuffer = iconv.encode(message.content, message.encoding ? message.encoding : 'utf8');
  477. fs.writeFileSync(filePath, fileBuffer);
  478. console.log('文件保存成功:', filePath);
  479. return { success: true, path: filePath };
  480. } catch (error) {
  481. console.error('保存文件失败:', error);
  482. return { success: false, error: error.message };
  483. }
  484. });
  485. ipcMain.handle("readFile", async (event, message) => {
  486. try {
  487. // 确保download目录存在
  488. const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
  489. if (!fs.existsSync(downloadDir)) {
  490. fs.mkdirSync(downloadDir, { recursive: true });
  491. console.log('创建download目录:', downloadDir);
  492. }
  493. // 构建完整的文件路径
  494. const filePath = path.join(downloadDir, message.filename);
  495. // 检查文件是否存在
  496. if (!fs.existsSync(filePath)) {
  497. console.log('文件不存在,返回空内容:', filePath);
  498. return { success: true, content: '', exists: false };
  499. }
  500. // 读取文件内容
  501. const buffer = fs.readFileSync(filePath);
  502. const content = iconv.decode(buffer, message.encoding ? message.encoding : 'utf8');
  503. console.log('文件读取成功:', filePath);
  504. return { success: true, content: content, exists: true };
  505. } catch (error) {
  506. console.error('读取文件失败:', error);
  507. return { success: false, error: error.message, exists: false };
  508. }
  509. });
  510. ipcMain.on("saveConfig", (event, message) => {
  511. fs.writeFileSync('./config.json', JSON.stringify(message))
  512. event.returnValue = {err: 0}
  513. })
  514. // 设置最大打开窗口数量
  515. ipcMain.on("setMaxWindowOpenNum", (event, message) => {
  516. if (message.value) {
  517. maxWindowOpenNum = parseInt(message.value)
  518. }
  519. })
  520. ipcMain.on("readdir", (event, directoryPath) => {
  521. fs.readdir(directoryPath, (err, files) => {
  522. if (err) {
  523. event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
  524. return;
  525. }
  526. event.returnValue = {err: 0, files}
  527. });
  528. })
  529. ipcMain.on("download", (event, message) => {
  530. download(message.url, message.path, {
  531. filename: message.filename,
  532. });
  533. event.returnValue = {err: 0}
  534. })
  535. // 窗口间通信
  536. ipcMain.on('broadcast-message', (event, message) => {
  537. // 获取发送者
  538. console.log(message)
  539. mainWindow.send('message-broadcast', message);
  540. // 广播给所有其他窗口
  541. for (const win of childWindowList) {
  542. if (win && !win.isDestroyed()) win.webContents.send('message-broadcast', message);
  543. }
  544. });