| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827 |
- // Modules to control application life and create native browser window
- const {app, ipcMain, BrowserWindow, session} = require('electron')
- const path = require('path')
- const fs = require('fs')
- const iconv = require('iconv-lite');
- const request = require('request');
- const download = require('download');
- // 读取配置文件
- let enterURL = 'https://demos.run/debuger/index.html'
- let webConfig = {
- width: 376,
- height: 667,
- webPreferences: {
- webSecurity: false,
- contextIsolation: true,
- nodeIntegration: false,
- allowRunningInsecureContent: true, // 允许不安全内容
- plugins: true,
- scrollBounce: true,
- preload: path.join(__dirname, "preload.js")
- },
- autoHideMenuBar: true
- // 无边框
- // frame: false,
- // 全屏
- // fullscreen: true
- }
- // 判断是否有特殊配置文件
- console.log(__dirname + "\\config.json")
- if (fs.existsSync("./config.json")) {
- let configStr = fs.readFileSync('./config.json', 'utf-8')
- // 特殊符号表示运行目录
- configStr = configStr.replaceAll('<dir>', __dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''))
- webConfig = JSON.parse(configStr)
- enterURL = webConfig.enterURL
- }
- console.log(webConfig)
- 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]"}
- function owoReplaceAll(str, s1, s2) {
- while (str.indexOf(s1) >= 0) {
- str = str.replace(s1, s2)
- }
- return str
- }
- function owoDecode(itemStr) {
- for (let item in codeMap) {
- itemStr = owoReplaceAll(itemStr, codeMap[item], item)
- }
- while (itemStr.indexOf(']') >= 0) {
- itemStr = owoDecode(itemStr)
- }
- return itemStr
- }
- let mainWindow = null
- let preLoadCode = `
- var owoApp = 5
- window.owoPC = true
- window.electronConfig = ${JSON.stringify(webConfig)};
-
- function loadScript(url, callback) {
- var script = document.createElement("script")
- script.type = "text/javascript";
- if (script.readyState) { //IE
- script.onreadystatechange = function () {
- if (script.readyState == "loaded" || script.readyState == "complete") {
- script.onreadystatechange = null;
- if (callback) callback();
- }
- };
- } else { //Others
- script.onload = function () {
- if (callback) callback();
- };
- }
- script.src = url;
- var head = document.head || document.getElementsByTagName('head')[0];
- head.appendChild(script);
- }
- function loadJsCode(code){
- var script = document.createElement('script');
- script.type = 'text/javascript';
- //for Chrome Firefox Opera Safari
- script.appendChild(document.createTextNode(code));
- //for IE
- //script.text = code;
- document.body.appendChild(script);
- }
- function loadCSS (url) {
- var link = document.createElement("link");
- link.rel = "stylesheet";
- link.type = "text/css";
- link.href = url;
- document.getElementsByTagName("head")[0].appendChild(link);
- }
-
- loadScript('https://cunchu.site/app/main.js');
- `
- // 拦截数据
- function setupDebuggerInterceptor(webContents, interceptor) {
- webContents.debugger.attach('1.3');
-
- const pendingRequests = new Map();
-
- webContents.debugger.on('message', (event, method, params) => {
- // 存储请求信息
- if (method === 'Network.requestWillBeSent') {
- pendingRequests.set(params.requestId, {
- url: params.request.url,
- method: params.request.method,
- headers: params.request.headers,
- postData: params.request.postData,
- timestamp: Date.now()
- });
- }
-
- // 响应接收时记录信息
- if (method === 'Network.responseReceived') {
- const { requestId, response } = params;
- let hookData = false;
-
- interceptor.forEach(hookURL => {
- if (response.url.includes(hookURL) || response.url === hookURL) {
- hookData = true;
- }
- });
-
- if (hookData) {
- console.log('拦截到响应:', response.url, response.status);
-
- // 存储响应信息,等待 loadingFinished
- const requestInfo = pendingRequests.get(requestId) || {};
- pendingRequests.set(requestId, {
- ...requestInfo,
- responseReceived: true,
- response,
- hookData: true
- });
- }
- }
-
- // 在资源加载完成时获取响应体
- if (method === 'Network.loadingFinished') {
- const { requestId } = params;
- const requestInfo = pendingRequests.get(requestId);
-
- if (requestInfo && requestInfo.hookData) {
- // 延迟获取响应体,确保数据可用
- setTimeout(() => {
- webContents.debugger.sendCommand('Network.getResponseBody', { requestId })
- .then(({ body }) => {
- // 获取请求体(如果有)
- let requestBody = null;
- if (requestInfo.postData) {
- try {
- // 尝试解析为JSON,如果不是JSON则保持原样
- requestBody = JSON.parse(requestInfo.postData);
- } catch (e) {
- requestBody = requestInfo.postData;
- }
- }
-
- const interceptedData = {
- url: requestInfo.response.url,
- method: requestInfo.method,
- statusCode: requestInfo.response.status,
- requestHeaders: requestInfo.headers,
- requestBody: requestBody,
- responseBody: body,
- responseHeaders: requestInfo.response.headers,
- requestId: requestId,
- timestamp: requestInfo.timestamp
- };
-
- console.log('拦截到的完整数据:', interceptedData.url);
- console.log(interceptedData);
-
- if (mainWindow && !mainWindow.isDestroyed()) {
- mainWindow.webContents.executeJavaScript(`
- if (window.onInterceptedData) {
- window.onInterceptedData(${JSON.stringify(interceptedData)});
- }
- `).catch(console.error);
- }
-
- // 清理缓存
- pendingRequests.delete(requestId);
- })
- .catch(error => {
- console.warn(`无法获取响应体 ${requestInfo.response.url}:`, error.message);
- pendingRequests.delete(requestId);
- });
- }, 100); // 添加小延迟
- }
- }
-
- // 清理已完成或失败的请求
- if (method === 'Network.loadingFailed' || method === 'Network.requestServedFromCache') {
- const { requestId } = params;
- pendingRequests.delete(requestId);
- }
- });
- // 启用网络跟踪
- webContents.debugger.sendCommand('Network.enable');
- }
- // 代码注入到iframe
- // 主进程中的递归注入函数
- function injectToAllNestedFrames(frame, injectedSet = new Set()) {
- try {
- // 安全检查
- if (!frame || frame.isDestroyed()) {
- return injectedSet;
- }
-
- const frameId = `${frame.processId}-${frame.routingId}`;
-
- // 避免重复注入
- if (injectedSet.has(frameId)) {
- return injectedSet;
- }
-
- // 注入当前框架
- if (!frame.isMainFrame) {
- try {
- frame.executeJavaScript(`
- if (!window.__electronDeepInjected) {
- ${preLoadCode}
- window.__electronDeepInjected = true;
- console.log('深层 iframe 注入成功,深度: ${frame.depth || 0}');
- }
- `, true).then(() => {
- console.log(`注入成功: ${frame.url ? frame.url.substring(0, 50) : '未知URL'} [深度: ${frame.depth || 0}]`);
- }).catch(err => {
- // 忽略无害错误
- });
-
- injectedSet.add(frameId);
- } catch (error) {
- // 忽略注入错误
- }
- }
-
- // 递归注入子框架
- try {
- if (frame.children && frame.children.length > 0) {
- frame.children.forEach(childFrame => {
- injectToAllNestedFrames(childFrame, injectedSet);
- });
- }
-
- // 也可以通过 frames 属性获取
- if (frame.frames && frame.frames.length > 0) {
- frame.frames.forEach(childFrame => {
- if (childFrame !== frame) { // 避免重复
- injectToAllNestedFrames(childFrame, injectedSet);
- }
- });
- }
- } catch (childError) {
- // 忽略子框架访问错误
- }
-
- } catch (error) {
- console.warn('递归注入出错:', error.message);
- }
-
- return injectedSet;
- }
- // 设置深层 iframe 注入
- function setupDeepFrameInjection(webContents) {
- console.log('设置深层 iframe 注入...');
- const injectedFrames = new Set();
-
- // 方法1:监听框架创建事件(包含嵌套框架)
- webContents.on('frame-created', (event, { frame, originalEvent }) => {
- console.log(`框架创建: ${frame.routingId}, 父框架: ${frame.parent ? frame.parent.routingId : '无'}`);
-
- setTimeout(() => {
- injectToAllNestedFrames(frame, injectedFrames);
- }, 500);
- });
-
- // 方法2:监听框架导航完成
- webContents.on('did-frame-finish-load', (event, isMainFrame, frameProcessId, frameRoutingId) => {
- setTimeout(() => {
- try {
- // 查找框架
- const findFrameRecursive = (parentFrame, targetId) => {
- if (parentFrame.routingId === targetId) {
- return parentFrame;
- }
-
- if (parentFrame.children) {
- for (const child of parentFrame.children) {
- const found = findFrameRecursive(child, targetId);
- if (found) return found;
- }
- }
-
- return null;
- };
-
- const frame = findFrameRecursive(webContents.mainFrame, frameRoutingId);
- if (frame) {
- injectToAllNestedFrames(frame, injectedFrames);
- }
- } catch (error) {
- // 忽略错误
- }
- }, 600);
- });
-
- // 方法3:定期深度扫描所有框架
- let deepScanInterval = setInterval(() => {
- if (webContents.isDestroyed()) {
- clearInterval(deepScanInterval);
- return;
- }
-
- console.log('执行深度框架扫描...');
- deepScanAllFrames(webContents.mainFrame, injectedFrames);
- }, 3000);
-
- // 初始扫描
- setTimeout(() => {
- deepScanAllFrames(webContents.mainFrame, injectedFrames);
- }, 2000);
-
- // 清理
- webContents.on('destroyed', () => {
- clearInterval(deepScanInterval);
- });
- }
- // 深度扫描所有框架(递归)
- function deepScanAllFrames(startFrame, injectedSet) {
- try {
- if (!startFrame || startFrame.isDestroyed()) {
- return;
- }
-
- // 注入当前框架
- if (!startFrame.isMainFrame) {
- const frameId = `${startFrame.processId}-${startFrame.routingId}`;
- if (!injectedSet.has(frameId)) {
- try {
- startFrame.executeJavaScript(`
- if (!window.__electronDeepInjected) {
- ${preLoadCode}
- window.__electronDeepInjected = true;
- }
- `, true);
- injectedSet.add(frameId);
- console.log(`深度扫描注入: ${startFrame.url ? startFrame.url.substring(0, 50) : '未知URL'} [深度: ${getFrameDepth(startFrame)}]`);
- } catch (error) {
- // 忽略注入错误
- }
- }
- }
-
- // 递归处理子框架
- if (startFrame.children && startFrame.children.length > 0) {
- startFrame.children.forEach(child => {
- deepScanAllFrames(child, injectedSet);
- });
- }
-
- // 处理同级框架(如果有的话)
- if (startFrame.frames && startFrame.frames.length > 0) {
- startFrame.frames.forEach(sibling => {
- if (sibling !== startFrame) {
- deepScanAllFrames(sibling, injectedSet);
- }
- });
- }
-
- } catch (error) {
- console.warn('深度扫描出错:', error.message);
- }
- }
- // 获取框架深度
- function getFrameDepth(frame) {
- let depth = 0;
- let current = frame;
-
- while (current && current.parent) {
- depth++;
- current = current.parent;
- }
-
- return depth;
- }
- function createWindow (partitionSession) {
- // console.log(webConfig)
- mainWindow = new BrowserWindow(webConfig)
- // 代理
- if (webConfig.proxy) {
- partitionSession.setProxy({
- proxyRules: webConfig.proxy,
- proxyBypassRules: 'localhost',
- }, function () {
- console.log('代理设置完毕')
- });
- }
- // 拦截到新窗口打开请求
- mainWindow.webContents.setWindowOpenHandler(({ url }) => {
- console.log('拦截到新窗口打开请求:', url);
- mainWindow.loadURL(url); // 当前窗口跳转
- return { action: 'deny' }; // 阻止 Electron 弹出窗口
- });
-
- // 使用纯 Chrome User Agent,完全移除 Electron 相关标识
-
- 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');
- if (enterURL.startsWith('http')) {
- mainWindow.loadURL(enterURL)
- } else {
- console.log(path.join(__dirname, enterURL))
- mainWindow.loadFile(enterURL)
- }
- if (webConfig.preLoadFile) {
- console.log(`Load additional JS: ${webConfig.preLoadFile}`)
- preLoadCode += fs.readFileSync(webConfig.preLoadFile, 'utf-8')
- }
- mainWindow.webContents.on("dom-ready", function() {
- mainWindow.webContents.executeJavaScript(preLoadCode);
- });
- // 只触发一次!
- mainWindow.webContents.once("did-finish-load", function() {
- if (webConfig.interceptor && webConfig.interceptor.length > 0) {
- console.log('Need to intercept data!')
- setupDebuggerInterceptor(mainWindow.webContents, webConfig.interceptor);
- }
- });
- // 打开新窗口触发
- mainWindow.webContents.on("did-create-window", function(neWindow) {
- neWindow.webContents.on("dom-ready", function() {
- neWindow.webContents.executeJavaScript(preLoadCode);
- });
- });
- setupDeepFrameInjection(mainWindow.webContents);
- }
- function setupCSPRemoval(ses) {
- ses.webRequest.onHeadersReceived((details, callback) => {
- const responseHeaders = details.responseHeaders || {};
-
- const cspHeaders = [
- 'content-security-policy',
- 'Content-Security-Policy',
- 'content-security-policy-report-only',
- 'x-content-security-policy',
- 'x-webkit-csp'
- ];
-
- cspHeaders.forEach(header => {
- if (responseHeaders[header]) {
- // console.log('Removing', header, 'from', details.url);
- delete responseHeaders[header];
- }
- });
-
- callback({ cancel: false, responseHeaders });
- });
- }
- // This method will be called when Electron has finished
- // initialization and is ready to create browser windows.
- // Some APIs can only be used after this event occurs.
- app.whenReady().then(() => {
- // 判断是否无缓存
- if (webConfig.noCache) {
- console.log('无缓存模式!')
- webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
-
- } else {
- webConfig.webPreferences.partition = 'persist:owoApp'
- }
- // 去掉安全措施
- const partitionSession = session.fromPartition(webConfig.webPreferences.partition);
- setupCSPRemoval(partitionSession);
-
-
- createWindow(partitionSession)
-
- app.on('activate', function () {
- // On macOS it's common to re-create a window in the app when the
- // dock icon is clicked and there are no other windows open.
- if (BrowserWindow.getAllWindows().length === 0) createWindow(partitionSession)
- })
- })
- // Quit when all windows are closed, except on macOS. There, it's common
- // for applications and their menu bar to stay active until the user quits
- // explicitly with Cmd + Q.
- app.on('window-all-closed', function () {
- if (process.platform !== 'darwin') app.quit()
- })
- app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
- // 阻止默认的错误处理
- event.preventDefault();
- // 允许证书错误
- callback(true);
-
- // 如果需要,可以记录日志
- console.warn(`忽略证书错误: ${error} - ${url}`);
- });
- // In this file you can include the rest of your app's specific main process
- // code. You can also put them in separate files and require them here.
- ipcMain.on("getData", (event, message) => {
- // 控制台打印一下知道来了
- console.log(message);
- var options = {
- 'method': 'GET',
- 'url': owoDecode(message.url),
- 'headers': message.headers,
- strictSSL: false
- };
- request(options, function (error, response) {
- if (error) throw new Error(error);
- event.returnValue = response.body
- });
- })
- ipcMain.on("postData", (event, message) => {
- // 控制台打印一下知道来了
- console.log(message);
- var options = {
- 'method': 'POST',
- 'url': owoDecode(message.url),
- 'headers': message.headers,
- 'body': message.body,
- strictSSL: false
- };
- request(options, function (error, response) {
- if (error) throw new Error(error);
- event.returnValue = response.body
- });
- })
- ipcMain.on("setProxy", (event, message) => {
- var win = new BrowserWindow({width: 800, height: 1500});
- mainWindow.webContents.session.setProxy({
- proxyRules: message.url,
- proxyBypassRules: 'localhost',
- });
- event.returnValue = 'ok'
- })
- // 添加注入代码
- ipcMain.on("addPreLoadCode", (event, message) => {
- if (message.data) preLoadCode += message.data
- })
- // 通用保存数据
- let dataStor = {}
- ipcMain.on("setStoData", (event, message) => {
- dataStor[message.key] = message.value
- event.returnValue = '{"err":0}'
- })
- ipcMain.on("getStoData", (event, message) => {
- event.returnValue = dataStor[message.key]
- })
- let maxWindowOpenNum = 10
- let nowWindowInd = 0
- let childWindowList = []
- 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;}
- ipcMain.handle("openWindow", async (event, message) => {
- console.log(message)
- let nowIndex = nowWindowInd++
- // 判断是否达到了最大窗口数量
- let nowWindowNumTemp = 0
- for (let index = 0; index < childWindowList.length; index++) {
- const element = childWindowList[index];
- if (element) nowWindowNumTemp++
- }
- if (nowWindowNumTemp > maxWindowOpenNum) {
- for (let index = 0; index < childWindowList.length; index++) {
- const element = childWindowList[index];
- if (element) {
- childWindowList[index].close()
- childWindowList[index] = null
- }
- }
- }
-
- // 获取对应 session
- let partitionName = 'persist:owoAppChild'
- if (message.noCache) {
- partitionName = 'persist:Session' + Math.round(Math.random()*100000)
- }
- const customSession = session.fromPartition(partitionName);
- if (message.proxy) {
- // 设置代理(等待设置完成)
- await customSession.setProxy({
- proxyRules: message.proxy,
- proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
- });
- }
- let childWindowPreferences = webConfig.webPreferences
- if (message.webPreferences) childWindowPreferences = message.webPreferences
- childWindowPreferences.partition = partitionName
- // 创建新窗口
- childWindowList[nowIndex] = new BrowserWindow({
- width: message.width || 800,
- height: message.height || 600,
- autoHideMenuBar: message.autoHideMenuBar || true,
- // 无边框
- frame: message.autoHideMenuBar || false,
- // 全屏
- fullscreen: message.autoHideMenuBar || false,
- webPreferences: childWindowPreferences
- });
- // 判断是否静音
- if (message.muted) {
- // 设置静音
- childWindowList[nowIndex].webContents.setAudioMuted(true);
- }
- childWindowList[nowIndex].webContents.on("dom-ready", function() {
- console.log("dom-ready")
- childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
- });
- childWindowList[nowIndex].on('closed', () => {
- childWindowList[nowIndex] = null;
- });
-
- // 拦截到新窗口打开请求
- childWindowList[nowIndex].webContents.setWindowOpenHandler(({ url }) => {
- console.log('拦截到新窗口打开请求:', url);
- childWindowList[nowIndex].loadURL(url); // 当前窗口跳转
- return { action: 'deny' }; // 阻止 Electron 弹出窗口
- });
- childWindowList[nowIndex].loadURL(message.url, {
- 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"
- });
-
- event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
- })
- ipcMain.on("closeWindow", (event, message) => {
- if (message && message.key) {
- message.key = parseInt(message.key)
- setTimeout(() => {
- if (childWindowList[message.key]) {
- childWindowList[message.key].close()
- }
- setTimeout(() => {
- childWindowList[message.key] = null
- }, 0);
- }, message.time || 0);
-
- }
-
-
- event.returnValue = JSON.stringify({"err":0})
- })
- ipcMain.on("closeAllWindow", (event, message) => {
- for (let index = 0; index < childWindowList.length; index++) {
- const element = childWindowList[index];
- if (element && element.close) {
- try {
- element.close()
- } catch (error) {
- console.log(error)
- }
- }
- }
- setTimeout(() => {
- childWindowList = []
- }, 0);
-
-
-
- event.returnValue = JSON.stringify({"err":0})
- })
- ipcMain.on("changeProxy", (event, message) => {
- if (message && message.key) {
- message.key = parseInt(message.key)
- childWindowList[message.key].webContents.session.setProxy({
- proxyRules: "",
- proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
- });
- } else {
- for (let index = 0; index < childWindowList.length; index++) {
- const element = childWindowList[index];
- if (element) {
- element.webContents.session.setProxy({
- proxyRules: "",
- proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
- });
- }
- }
- }
-
- event.returnValue = JSON.stringify({"err":0})
- })
- ipcMain.on("readConfig", (event, message) => {
- if (fs.existsSync("./config.json")) {
- event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
- } else {
- event.returnValue = {}
- }
- })
- ipcMain.handle("saveFile", async (event, message) => {
- try {
- // 确保download目录存在
- const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
- if (!fs.existsSync(downloadDir)) {
- fs.mkdirSync(downloadDir, { recursive: true });
- console.log('创建download目录:', downloadDir);
- }
-
- // 构建完整的文件路径
- const filePath = path.join(downloadDir, message.filename);
- // 将字符串编码为缓冲区
- const fileBuffer = iconv.encode(message.content, message.encoding ? message.encoding : 'utf8');
- fs.writeFileSync(filePath, fileBuffer);
-
- console.log('文件保存成功:', filePath);
- return { success: true, path: filePath };
- } catch (error) {
- console.error('保存文件失败:', error);
- return { success: false, error: error.message };
- }
- });
- ipcMain.handle("readFile", async (event, message) => {
- try {
- // 确保download目录存在
- const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
- if (!fs.existsSync(downloadDir)) {
- fs.mkdirSync(downloadDir, { recursive: true });
- console.log('创建download目录:', downloadDir);
- }
-
- // 构建完整的文件路径
- const filePath = path.join(downloadDir, message.filename);
-
- // 检查文件是否存在
- if (!fs.existsSync(filePath)) {
- console.log('文件不存在,返回空内容:', filePath);
- return { success: true, content: '', exists: false };
- }
-
- // 读取文件内容
- const buffer = fs.readFileSync(filePath);
- const content = iconv.decode(buffer, message.encoding ? message.encoding : 'utf8');
-
- console.log('文件读取成功:', filePath);
- return { success: true, content: content, exists: true };
- } catch (error) {
- console.error('读取文件失败:', error);
- return { success: false, error: error.message, exists: false };
- }
- });
- ipcMain.on("saveConfig", (event, message) => {
- fs.writeFileSync('./config.json', JSON.stringify(message))
- event.returnValue = {err: 0}
- })
- // 设置最大打开窗口数量
- ipcMain.on("setMaxWindowOpenNum", (event, message) => {
- if (message.value) {
- maxWindowOpenNum = parseInt(message.value)
- }
- })
- ipcMain.on("readdir", (event, directoryPath) => {
- fs.readdir(directoryPath, (err, files) => {
- if (err) {
- event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
- return;
- }
- event.returnValue = {err: 0, files}
- });
-
- })
- ipcMain.on("download", (event, message) => {
- download(message.url, message.path, {
- filename: message.filename,
- });
- event.returnValue = {err: 0}
- })
- // 窗口间通信
- ipcMain.on('broadcast-message', (event, message) => {
- // 获取发送者
- console.log(message)
- mainWindow.send('message-broadcast', message);
- // 广播给所有其他窗口
- for (const win of childWindowList) {
- if (win && !win.isDestroyed()) win.webContents.send('message-broadcast', message);
- }
- });
|