Files
PeojectTWT/WordDictationStudentApp/main.js
2025-10-11 10:47:07 +08:00

103 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// main.js
const { app, BrowserWindow, dialog, ipcMain } = require('electron'); // 1. 导入 ipcMain
const path = require('path');
let mainWindow;
let switchCount = 0; // 在主进程中也维护一个计数器
const switchLimit = 3;
const warningLimit = 2;
function createWindow () {
mainWindow = new BrowserWindow({
// --- 关键修改:设置为全屏无框 ---
fullscreen: true,
frame: false,
resizable: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true, // 2. 必须开启
enableRemoteModule: false,
preload: path.join(__dirname, 'preload.js') // 3. 关键:加载 preload 脚本
},
// --- 可选:设置图标 ---
// icon: path.join(__dirname, 'icon.png')
});
mainWindow.loadFile('student_app_fullscreen.html');
// --- 关键修改:监听窗口失去焦点事件 ---
mainWindow.on('blur', () => {
switchCount++;
console.log(`窗口失去焦点,切屏次数: ${switchCount}`); // 控制台输出日志
if (switchCount <= warningLimit - 1 ) {
// 显示警告对话框
dialog.showMessageBox(mainWindow, {
type: 'warning',
title: '切屏警告',
message: `您进行了切屏操作,此行为已被记录,如您再次进行两次切屏,则会重置考试`,
buttons: ['确定']
}).then(() => {
// 警告后,将焦点重新移回主窗口
mainWindow.focus();
});
} else if (switchCount < switchLimit) {
// 第三次切屏,重置警告
dialog.showMessageBox(mainWindow, {
type: 'warning',
title: '切屏警告',
message: `您再次进行了切屏操作,此行为已被记录,如您再次进行一次切屏,则会重置考试`,
buttons: ['确定']
}).then(() => {
mainWindow.focus();
});
} else {
// 超过限制次数
dialog.showMessageBox(mainWindow, {
type: 'error',
title: '切屏过多',
message: `您因切屏超过误触缓冲次数,重置考试`,
buttons: ['确定']
}).then(() => {
// 重置考试:重新加载页面,重置计数器
switchCount = 0;
mainWindow.reload();
});
}
});
// --- 结束监听 ---
// 监听窗口关闭事件
mainWindow.on('closed', () => {
mainWindow = null;
});
}
// --- 新增IPC 处理程序 ---
// 处理来自渲染进程的获取切屏次数请求
ipcMain.handle('get-switch-count', async () => {
return switchCount; // 返回当前计数
});
// --- 结束新增 ---
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});
// --- 注释掉或移除旧的 IPC 代码 ---
// const { ipcMain } = require('electron');
// ipcMain.on('get-switch-count', (event) => {
// event.reply('switch-count-reply', switchCount);
// });
// ipcMain.on('reset-switch-count', () => {
// switchCount = 0;
// });
// --- 结束注释 ---