preload.js

This commit is contained in:
chenqiang 2025-09-09 22:35:44 +08:00
parent 4b82de5374
commit 93daaf49f4
5 changed files with 135 additions and 113 deletions

View File

@ -1,4 +1,4 @@
const { getSystemDbPath, getUserDbPath } = require('./path.js'); // 添加路径函数导入 const { getSystemDbPath, getUserDbPath } = require('./path.js'); // 确保这行代码存在
const { systemSchema, userSchema, defaultData } = require('./schema.js'); const { systemSchema, userSchema, defaultData } = require('./schema.js');
const { openDatabase, batchInsert } = require('./utils.js'); const { openDatabase, batchInsert } = require('./utils.js');
const bcrypt = require('bcryptjs'); const bcrypt = require('bcryptjs');

View File

@ -1,4 +1,4 @@
const fs = require('fs'); // 添加缺失的fs模块导入 const fs = require('fs'); // 确保这行代码存在
const path = require('path'); const path = require('path');
const { app } = require('electron'); const { app } = require('electron');

View File

@ -43,27 +43,20 @@ let mainWindow = null;
async function createWindow() { async function createWindow() {
console.log('开始创建应用窗口...'); console.log('开始创建应用窗口...');
try {
// 创建浏览器窗口 // 创建浏览器窗口
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
// 确保在Windows 7上能正常显示
show: false, show: false,
// 在Windows 7上使用更兼容的配置
webPreferences: { webPreferences: {
// 使用绝对路径确保preload.js能被找到 // 更新preload路径使用__dirname确保路径正确
preload: path.resolve(__dirname, '../src/preload.js'), preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false, nodeIntegration: false,
contextIsolation: true, contextIsolation: true,
// 为Windows 7添加额外的兼容性配置
enableRemoteModule: false,
sandbox: false,
webSecurity: false
} }
}); });
console.log('Preload path:', path.resolve(__dirname, '../src/preload.js'));
// 设置隐藏菜单栏 // 设置隐藏菜单栏
mainWindow.setMenu(null); mainWindow.setMenu(null);
@ -150,11 +143,6 @@ async function createWindow() {
// 生产模式加载本地HTML文件 // 生产模式加载本地HTML文件
mainWindow.loadURL("app://./index.html"); mainWindow.loadURL("app://./index.html");
} }
} catch (windowError) {
console.error('创建窗口失败:', windowError);
// 显示错误对话框
dialog.showErrorBox('创建窗口失败', `无法创建应用窗口: ${windowError.message}`);
}
} }
// Quit when all windows are closed. // Quit when all windows are closed.

View File

@ -12,7 +12,41 @@ module.exports = {
pluginOptions: { pluginOptions: {
electronBuilder: { electronBuilder: {
mainProcessFile: 'background/main.js', // 基础配置
nodeIntegration: false,
contextIsolation: true,
// 更新preload路径到新位置
preload: "background/preload.js",
mainProcessFile: "background/main.js",
lintPreloadFiles: false,
externals: ["fontkit", "pdfkit"],
// 主进程webpack配置
chainWebpackMainProcess: (config) => {
config.module
.rule("babel")
.test(/\.js$/)
.use("babel-loader")
.loader("babel-loader")
.options({
presets: ["@babel/preset-env"],
plugins: [
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-class-properties",
],
})
.end();
// 添加对mjs文件的处理 - 兼容Electron 11
config.module
.rule("mjs")
.test(/\.mjs$/)
.include.add(/node_modules/)
.end()
.type("javascript/auto");
},
// 所有构建选项都放在这里
builderOptions: { builderOptions: {
// 应用基本信息 // 应用基本信息
appId: 'com.example.exam11', appId: 'com.example.exam11',