main.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
  285. // 阻止默认的错误处理
  286. event.preventDefault();
  287. // 允许证书错误
  288. callback(true);
  289. // 如果需要,可以记录日志
  290. console.warn(`忽略证书错误: ${error} - ${url}`);
  291. });
  292. // In this file you can include the rest of your app's specific main process
  293. // code. You can also put them in separate files and require them here.
  294. ipcMain.on("getData", (event, message) => {
  295. // 控制台打印一下知道来了
  296. console.log(message);
  297. var options = {
  298. 'method': 'GET',
  299. 'url': owoDecode(message.url),
  300. 'headers': message.headers,
  301. strictSSL: false
  302. };
  303. request(options, function (error, response) {
  304. if (error) throw new Error(error);
  305. event.returnValue = response.body
  306. });
  307. })
  308. ipcMain.on("postData", (event, message) => {
  309. // 控制台打印一下知道来了
  310. console.log(message);
  311. var options = {
  312. 'method': 'POST',
  313. 'url': owoDecode(message.url),
  314. 'headers': message.headers,
  315. 'body': message.body,
  316. strictSSL: false
  317. };
  318. request(options, function (error, response) {
  319. if (error) throw new Error(error);
  320. event.returnValue = response.body
  321. });
  322. })
  323. ipcMain.on("setProxy", (event, message) => {
  324. var win = new BrowserWindow({width: 800, height: 1500});
  325. mainWindow.webContents.session.setProxy({
  326. proxyRules: message.url,
  327. proxyBypassRules: 'localhost',
  328. });
  329. event.returnValue = 'ok'
  330. })
  331. // 添加注入代码
  332. ipcMain.on("addPreLoadCode", (event, message) => {
  333. if (message.data) preLoadCode += message.data
  334. })
  335. // 通用保存数据
  336. let dataStor = {}
  337. ipcMain.on("setStoData", (event, message) => {
  338. dataStor[message.key] = message.value
  339. event.returnValue = '{"err":0}'
  340. })
  341. ipcMain.on("getStoData", (event, message) => {
  342. event.returnValue = dataStor[message.key]
  343. })
  344. let maxWindowOpenNum = 10
  345. let nowWindowInd = 0
  346. let childWindowList = []
  347. 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;}
  348. ipcMain.handle("openWindow", async (event, message) => {
  349. console.log(message)
  350. let nowIndex = nowWindowInd++
  351. // 判断是否达到了最大窗口数量
  352. let nowWindowNumTemp = 0
  353. for (let index = 0; index < childWindowList.length; index++) {
  354. const element = childWindowList[index];
  355. if (element) nowWindowNumTemp++
  356. }
  357. if (nowWindowNumTemp > maxWindowOpenNum) {
  358. for (let index = 0; index < childWindowList.length; index++) {
  359. const element = childWindowList[index];
  360. if (element) {
  361. childWindowList[index].close()
  362. childWindowList[index] = null
  363. }
  364. }
  365. }
  366. // 获取对应 session
  367. let partitionName = 'persist:owoAppChild'
  368. if (message.noCache) {
  369. partitionName = 'persist:Session' + Math.round(Math.random()*100000)
  370. }
  371. const customSession = session.fromPartition(partitionName);
  372. if (message.proxy) {
  373. // 设置代理(等待设置完成)
  374. await customSession.setProxy({
  375. proxyRules: message.proxy,
  376. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  377. });
  378. }
  379. let childWindowPreferences = webConfig.webPreferences
  380. if (message.webPreferences) childWindowPreferences = message.webPreferences
  381. childWindowPreferences.partition = partitionName
  382. // 创建新窗口
  383. childWindowList[nowIndex] = new BrowserWindow({
  384. width: message.width || 800,
  385. height: message.height || 600,
  386. webPreferences: childWindowPreferences
  387. });
  388. // 判断是否静音
  389. if (message.muted) {
  390. // 设置静音
  391. childWindowList[nowIndex].webContents.setAudioMuted(true);
  392. }
  393. childWindowList[nowIndex].webContents.on("dom-ready", function() {
  394. console.log("dom-ready")
  395. childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
  396. });
  397. childWindowList[nowIndex].on('closed', () => {
  398. childWindowList[nowIndex] = null;
  399. });
  400. // 拦截到新窗口打开请求
  401. childWindowList[nowIndex].webContents.setWindowOpenHandler(({ url }) => {
  402. console.log('拦截到新窗口打开请求:', url);
  403. childWindowList[nowIndex].loadURL(url); // 当前窗口跳转
  404. return { action: 'deny' }; // 阻止 Electron 弹出窗口
  405. });
  406. childWindowList[nowIndex].loadURL(message.url, {
  407. 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"
  408. });
  409. event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
  410. })
  411. ipcMain.on("closeWindow", (event, message) => {
  412. if (message && message.key) {
  413. message.key = parseInt(message.key)
  414. setTimeout(() => {
  415. if (childWindowList[message.key]) {
  416. childWindowList[message.key].close()
  417. }
  418. setTimeout(() => {
  419. childWindowList[message.key] = null
  420. }, 0);
  421. }, message.time || 0);
  422. }
  423. event.returnValue = JSON.stringify({"err":0})
  424. })
  425. ipcMain.on("closeAllWindow", (event, message) => {
  426. for (let index = 0; index < childWindowList.length; index++) {
  427. const element = childWindowList[index];
  428. if (element && element.close) {
  429. try {
  430. element.close()
  431. } catch (error) {
  432. console.log(error)
  433. }
  434. }
  435. }
  436. setTimeout(() => {
  437. childWindowList = []
  438. }, 0);
  439. event.returnValue = JSON.stringify({"err":0})
  440. })
  441. ipcMain.on("changeProxy", (event, message) => {
  442. if (message && message.key) {
  443. message.key = parseInt(message.key)
  444. childWindowList[message.key].webContents.session.setProxy({
  445. proxyRules: "",
  446. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  447. });
  448. } else {
  449. for (let index = 0; index < childWindowList.length; index++) {
  450. const element = childWindowList[index];
  451. if (element) {
  452. element.webContents.session.setProxy({
  453. proxyRules: "",
  454. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  455. });
  456. }
  457. }
  458. }
  459. event.returnValue = JSON.stringify({"err":0})
  460. })
  461. ipcMain.on("readConfig", (event, message) => {
  462. if (fs.existsSync("./config.json")) {
  463. event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  464. } else {
  465. event.returnValue = {}
  466. }
  467. })
  468. ipcMain.handle("saveFile", async (event, message) => {
  469. try {
  470. // 确保download目录存在
  471. const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
  472. if (!fs.existsSync(downloadDir)) {
  473. fs.mkdirSync(downloadDir, { recursive: true });
  474. console.log('创建download目录:', downloadDir);
  475. }
  476. // 构建完整的文件路径
  477. const filePath = path.join(downloadDir, message.filename);
  478. // 将字符串编码为缓冲区
  479. const fileBuffer = iconv.encode(message.content, message.encoding ? message.encoding : 'utf8');
  480. fs.writeFileSync(filePath, fileBuffer);
  481. console.log('文件保存成功:', filePath);
  482. return { success: true, path: filePath };
  483. } catch (error) {
  484. console.error('保存文件失败:', error);
  485. return { success: false, error: error.message };
  486. }
  487. });
  488. ipcMain.handle("readFile", async (event, message) => {
  489. try {
  490. // 确保download目录存在
  491. const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
  492. if (!fs.existsSync(downloadDir)) {
  493. fs.mkdirSync(downloadDir, { recursive: true });
  494. console.log('创建download目录:', downloadDir);
  495. }
  496. // 构建完整的文件路径
  497. const filePath = path.join(downloadDir, message.filename);
  498. // 检查文件是否存在
  499. if (!fs.existsSync(filePath)) {
  500. console.log('文件不存在,返回空内容:', filePath);
  501. return { success: true, content: '', exists: false };
  502. }
  503. // 读取文件内容
  504. const buffer = fs.readFileSync(filePath);
  505. const content = iconv.decode(buffer, message.encoding ? message.encoding : 'utf8');
  506. console.log('文件读取成功:', filePath);
  507. return { success: true, content: content, exists: true };
  508. } catch (error) {
  509. console.error('读取文件失败:', error);
  510. return { success: false, error: error.message, exists: false };
  511. }
  512. });
  513. ipcMain.on("saveConfig", (event, message) => {
  514. fs.writeFileSync('./config.json', JSON.stringify(message))
  515. event.returnValue = {err: 0}
  516. })
  517. // 设置最大打开窗口数量
  518. ipcMain.on("setMaxWindowOpenNum", (event, message) => {
  519. if (message.value) {
  520. maxWindowOpenNum = parseInt(message.value)
  521. }
  522. })
  523. ipcMain.on("readdir", (event, directoryPath) => {
  524. fs.readdir(directoryPath, (err, files) => {
  525. if (err) {
  526. event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
  527. return;
  528. }
  529. event.returnValue = {err: 0, files}
  530. });
  531. })
  532. ipcMain.on("download", (event, message) => {
  533. download(message.url, message.path, {
  534. filename: message.filename,
  535. });
  536. event.returnValue = {err: 0}
  537. })
  538. // 窗口间通信
  539. ipcMain.on('broadcast-message', (event, message) => {
  540. // 获取发送者
  541. console.log(message)
  542. mainWindow.send('message-broadcast', message);
  543. // 广播给所有其他窗口
  544. for (const win of childWindowList) {
  545. if (win && !win.isDestroyed()) win.webContents.send('message-broadcast', message);
  546. }
  547. });