const { app, BrowserWindow, ipcMain } = require('electron') const path = require('path') const fs = require('fs') const os = require('os') const WindowStateManager = require('./windowState') // 窗口状态管理器(延迟初始化) let windowStateManager = null // 设置进程编码,确保中文正常显示 process.env.LANG = 'zh_CN.UTF-8' process.env.LC_ALL = 'zh_CN.UTF-8' // 获取项目根目录绝对路径 const appRoot = path.resolve(__dirname, '../..') // 获取本地IP地址 function getLocalIPAddresses() { try { const interfaces = os.networkInterfaces() const addresses = [] for (const name of Object.keys(interfaces)) { for (const interface of interfaces[name]) { // 跳过内部地址和非IPv4地址 if (interface.family === 'IPv4' && !interface.internal) { addresses.push({ name: name, address: interface.address, netmask: interface.netmask, family: interface.family }) } } } // 优先返回非回环地址 const nonLoopback = addresses.filter(addr => addr.address !== '127.0.0.1') return nonLoopback.length > 0 ? nonLoopback[0] : addresses[0] || null } catch (error) { console.error('获取本地IP地址失败:', error) return null } } let mainWindow let errorLogCount = 0 let logFilePath // 检查是否为开发环境(延迟初始化) let isDev = null function getIsDev() { if (isDev === null) { try { isDev = process.env.NODE_ENV === 'development' || !app.isPackaged } catch (error) { // 如果app对象还没有准备好,默认使用开发环境 isDev = process.env.NODE_ENV === 'development' || true } } return isDev } // 初始化日志系统 function initLogging() { try { // 创建日志目录 const logDir = path.join(app.getPath('userData'), 'logs') if (!fs.existsSync(logDir)) { fs.mkdirSync(logDir, { recursive: true }) } // 设置日志文件路径 const timestamp = new Date().toISOString().split('T')[0] logFilePath = path.join(logDir, `gofaster-${timestamp}.log`) // 重定向控制台输出到日志文件,设置UTF-8编码 const logStream = fs.createWriteStream(logFilePath, { flags: 'a', encoding: 'utf8' }) // 重写console.log const originalLog = console.log console.log = function(...args) { const timestamp = new Date().toISOString() const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : String(arg) ).join(' ') logStream.write(`[${timestamp}] [INFO] ${message}\n`) originalLog.apply(console, args) } // 重写console.error const originalError = console.error console.error = function(...args) { const timestamp = new Date().toISOString() const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : String(arg) ).join(' ') errorLogCount++ logStream.write(`[${timestamp}] [ERROR] ${message}\n`) originalError.apply(console, args) // 通知渲染进程错误计数更新 if (mainWindow && !mainWindow.isDestroyed()) { mainWindow.webContents.send('error-log-updated', { count: errorLogCount }) } } // 重写console.warn const originalWarn = console.warn console.warn = function(...args) { const timestamp = new Date().toISOString() const message = args.map(arg => typeof arg === 'object' ? JSON.stringify(arg) : String(arg) ).join(' ') logStream.write(`[${timestamp}] [WARN] ${message}\n`) originalWarn.apply(console, args) } } catch (error) { console.error('初始化日志系统失败:', error) } } // 开发环境热重载 - 只在非watch模式下启用 if (getIsDev() && !process.argv.includes('--watch')) { try { // 修复热重载配置 require('electron-reload')(appRoot, { electron: path.join(__dirname, '..', '..', 'node_modules', 'electron', 'dist', 'electron.exe'), hardResetMethod: 'exit', // 添加更稳定的配置 forceHardReset: true, ignored: [ /node_modules|[\/\\]\./, /dist|[\/\\]\./, /\.git|[\/\\]\./ ] }); } catch (error) { console.log('Hot reload failed:', error.message); } } // 监听文件变化(用于watch模式) if (getIsDev() && process.argv.includes('--watch')) { // 监听dist目录变化 const distPath = path.join(appRoot, 'dist/renderer'); if (fs.existsSync(distPath)) { let reloadTimeout = null; fs.watch(distPath, { recursive: true }, (eventType, filename) => { if (filename && mainWindow && !mainWindow.isDestroyed()) { // 添加防抖机制,避免频繁重载 if (reloadTimeout) { clearTimeout(reloadTimeout); } reloadTimeout = setTimeout(() => { try { mainWindow.reload(); } catch (error) { console.log('Page reload failed:', error.message); } }, 500); // 500ms防抖延迟 } }); } } function createWindow() { try { // 加载保存的窗口状态 let windowState if (!windowStateManager) { console.warn('窗口状态管理器未初始化,使用默认状态') windowState = { width: 1200, height: 800, x: undefined, y: undefined, isMaximized: false, isFullScreen: false } } else { windowState = windowStateManager.loadState() } mainWindow = new BrowserWindow({ width: windowState.width, height: windowState.height, x: windowState.x, y: windowState.y, autoHideMenuBar: true, webPreferences: { nodeIntegration: false, contextIsolation: true, enableRemoteModule: false, preload: path.join(appRoot, 'src/preload.js'), // 强化安全配置 webSecurity: true, allowRunningInsecureContent: false, experimentalFeatures: false, // 禁用Node.js集成 nodeIntegrationInWorker: false, nodeIntegrationInSubFrames: false, // 沙箱模式 sandbox: false, // 保持false以支持preload脚本 // 禁用eval相关功能 enableBlinkFeatures: '', disableBlinkFeatures: 'Auxclick' }, // 添加字体配置,确保中文正常显示 titleBarStyle: 'default', show: false // 先隐藏窗口,等加载完成后再显示 }) // 如果保存的状态是最大化或全屏,应用这些状态 if (windowState.isMaximized) { mainWindow.maximize() } else if (windowState.isFullScreen) { mainWindow.setFullScreen(true) } // 开发环境下抑制安全警告 if (getIsDev()) { mainWindow.webContents.on('did-frame-finish-load', () => { // 移除有问题的JavaScript执行,避免IPC通信错误 // 简化JavaScript注入,避免序列化错误 mainWindow.webContents.executeJavaScript(` // 重写console.warn和console.error来抑制特定警告 const originalWarn = console.warn; const originalError = console.error; console.warn = function(...args) { const message = args.join(' '); if (message.includes('Electron Security Warning') || message.includes('Insecure Content-Security-Policy') || message.includes('unsafe-eval')) { return; // 抑制这些警告 } originalWarn.apply(console, args); }; console.error = function(...args) { const message = args.join(' '); if (message.includes('Electron Security Warning') || message.includes('Insecure Content-Security-Policy') || message.includes('unsafe-eval')) { return; // 抑制这些警告 } originalError.apply(console, args); }; `).catch(err => { console.log('JavaScript injection failed:', err.message); // 注入失败不影响应用运行 }); }); // 抑制Electron安全警告 mainWindow.webContents.on('console-message', (event, level, message, line, sourceId) => { // 抑制Autofill相关警告 if (message.includes('Autofill.enable failed') || message.includes('Autofill.setAddresses failed') || message.includes('Request Autofill')) { event.preventDefault(); return false; } // 抑制Electron安全警告 if (message.includes('Electron Security Warning') || message.includes('Insecure Content-Security-Policy') || message.includes('unsafe-eval')) { event.preventDefault(); return false; } }); } const loadPath = path.join(appRoot, 'dist/renderer/index.html') // 检查文件是否存在 if (!fs.existsSync(loadPath)) { // 在watch模式下等待文件构建完成 if (getIsDev() && process.argv.includes('--watch')) { const checkFile = () => { if (fs.existsSync(loadPath)) { loadMainWindow(); } else { setTimeout(checkFile, 1000); // 每秒检查一次 } }; checkFile(); return; // 重要:如果文件不存在,直接返回,不执行后续代码 } else { // 非watch模式,直接显示错误 console.error('File not found:', loadPath); mainWindow.loadURL(`data:text/html,