chore: configure eslint and update gitignore
This commit is contained in:
107
main.js
107
main.js
@@ -1,16 +1,74 @@
|
||||
const { app, BrowserWindow, ipcMain, clipboard, Menu } = require('electron');
|
||||
const { app, BrowserWindow, ipcMain, clipboard, Menu, desktopCapturer, screen } = require('electron');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const Database = require('./src/db/database');
|
||||
|
||||
// 强制设置用户数据存储位置为项目根目录下的 data 文件夹
|
||||
// 这样 Electron 的缓存、Localstorage、日志等都不会写到 C 盘
|
||||
// Force userData path to project root/data folder
|
||||
// Prevents Electron cache/localStorage/logs from writing to C drive
|
||||
app.setPath('userData', path.join(process.cwd(), 'data'));
|
||||
|
||||
let mainWindow;
|
||||
let db;
|
||||
let backupInterval;
|
||||
|
||||
// Auto backup feature
|
||||
function startAutoBackup() {
|
||||
const backupDir = path.join(process.cwd(), 'DB');
|
||||
const dbPath = path.join(process.cwd(), 'accounts.db');
|
||||
|
||||
// Ensure backup directory exists
|
||||
if (!fs.existsSync(backupDir)) {
|
||||
fs.mkdirSync(backupDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Backup every 30 minutes
|
||||
backupInterval = setInterval(() => {
|
||||
try {
|
||||
if (fs.existsSync(dbPath)) {
|
||||
const now = new Date();
|
||||
const timestamp = now.getFullYear().toString() +
|
||||
(now.getMonth() + 1).toString().padStart(2, '0') +
|
||||
now.getDate().toString().padStart(2, '0') + '_' +
|
||||
now.getHours().toString().padStart(2, '0') +
|
||||
now.getMinutes().toString().padStart(2, '0') +
|
||||
now.getSeconds().toString().padStart(2, '0');
|
||||
|
||||
const backupPath = path.join(backupDir, `accounts_${timestamp}.db`);
|
||||
fs.copyFileSync(dbPath, backupPath);
|
||||
console.log(`[Backup] Auto backup success: ${backupPath}`);
|
||||
|
||||
// Clean old backups, keep last 10
|
||||
cleanOldBackups(backupDir, 10);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Backup] Backup failed:', e);
|
||||
}
|
||||
}, 30 * 60 * 1000); // 30 minutes
|
||||
|
||||
console.log('[Backup] Auto backup started, interval 30 minutes');
|
||||
}
|
||||
|
||||
// Clean old backups
|
||||
function cleanOldBackups(backupDir, keepCount) {
|
||||
try {
|
||||
const files = fs.readdirSync(backupDir)
|
||||
.filter(f => f.startsWith('accounts_') && f.endsWith('.db'))
|
||||
.map(f => ({ name: f, path: path.join(backupDir, f), time: fs.statSync(path.join(backupDir, f)).mtime }))
|
||||
.sort((a, b) => b.time - a.time);
|
||||
|
||||
if (files.length > keepCount) {
|
||||
files.slice(keepCount).forEach(f => {
|
||||
fs.unlinkSync(f.path);
|
||||
console.log(`[Backup] Deleted old backup: ${f.name}`);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Backup] Clean old backups failed:', e);
|
||||
}
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
// 移除应用菜单栏
|
||||
// Remove application menu bar
|
||||
Menu.setApplicationMenu(null);
|
||||
|
||||
mainWindow = new BrowserWindow({
|
||||
@@ -21,7 +79,8 @@ function createWindow() {
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false
|
||||
nodeIntegration: false,
|
||||
sandbox: false
|
||||
},
|
||||
backgroundColor: '#0a0a0b',
|
||||
titleBarStyle: 'default',
|
||||
@@ -34,13 +93,16 @@ function createWindow() {
|
||||
mainWindow.show();
|
||||
});
|
||||
|
||||
// 开发时打开DevTools
|
||||
// mainWindow.webContents.openDevTools();
|
||||
// DevTools: opt-in only (prevents auto-opening on startup)
|
||||
if (process.env.ELECTRON_DEVTOOLS === '1') {
|
||||
mainWindow.webContents.openDevTools();
|
||||
}
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
db = new Database();
|
||||
createWindow();
|
||||
startAutoBackup();
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
@@ -50,6 +112,9 @@ app.whenReady().then(() => {
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (backupInterval) {
|
||||
clearInterval(backupInterval);
|
||||
}
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
@@ -105,3 +170,31 @@ ipcMain.handle('clipboard:write', async (event, text) => {
|
||||
clipboard.writeText(text);
|
||||
return true;
|
||||
});
|
||||
|
||||
// Screen capture feature
|
||||
ipcMain.handle('screen:capture', async () => {
|
||||
try {
|
||||
const primaryDisplay = screen.getPrimaryDisplay();
|
||||
const { width, height } = primaryDisplay.size;
|
||||
const scaleFactor = primaryDisplay.scaleFactor;
|
||||
|
||||
const sources = await desktopCapturer.getSources({
|
||||
types: ['screen'],
|
||||
thumbnailSize: { width: width * scaleFactor, height: height * scaleFactor }
|
||||
});
|
||||
|
||||
if (sources.length > 0) {
|
||||
const thumbnail = sources[0].thumbnail;
|
||||
return {
|
||||
success: true,
|
||||
image: thumbnail.toDataURL(),
|
||||
width: thumbnail.getSize().width,
|
||||
height: thumbnail.getSize().height
|
||||
};
|
||||
}
|
||||
return { success: false, error: 'Failed to capture screen' };
|
||||
} catch (e) {
|
||||
console.error('Screen capture failed:', e);
|
||||
return { success: false, error: e.message };
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user