feat: 项目结构调整|新增依赖

This commit is contained in:
duanshuwen
2025-11-22 21:17:40 +08:00
parent 38b6a4b4a3
commit 6013c38fe7
40 changed files with 535 additions and 115 deletions

View File

@@ -0,0 +1,28 @@
const { chromium } = require("playwright");
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
try {
// 打开百度
await page.goto("https://www.baidu.com");
// 输入搜索关键词并回车
await page.fill("textarea#chat-textarea", "Playwright 自动化");
await page.keyboard.press("Enter");
// 等待搜索结果加载
await page.waitForSelector("#content_left");
console.log("百度搜索执行成功");
} catch (e) {
console.error("自动化执行失败:", e);
process.exitCode = 1;
} finally {
// 保持几秒以方便观察,再关闭
await page.waitForTimeout(2000);
await browser.close();
}
})();

View File

@@ -0,0 +1,247 @@
const { chromium } = require('playwright');
const fs = require('fs');
const path = require('path');
// 录制配置
const recordingConfig = {
outputFile: 'recorded-steps.json',
headless: false, // 显示浏览器窗口
slowMo: 100, // 减慢操作速度,便于观察
viewport: { width: 1280, height: 720 }
};
// 存储录制的步骤
let recordedSteps = [];
let recordingStartTime = Date.now();
let isRecording = true;
// 生成时间戳
function getTimestamp() {
return Date.now() - recordingStartTime;
}
// 记录步骤
function recordStep(step) {
const stepWithTimestamp = {
...step,
timestamp: getTimestamp()
};
recordedSteps.push(stepWithTimestamp);
console.log(`📹 录制步骤: ${step.type} - ${step.description}`);
}
// 保存录制的步骤到文件
function saveRecordedSteps() {
const outputPath = path.join(__dirname, recordingConfig.outputFile);
const data = {
steps: recordedSteps,
metadata: {
totalSteps: recordedSteps.length,
duration: getTimestamp(),
recordedAt: new Date().toISOString()
}
};
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2));
console.log(`\n✅ 录制完成!共录制 ${recordedSteps.length} 个步骤`);
console.log(`📁 录制文件已保存到: ${outputPath}`);
}
// 监听页面事件
async function setupPageListeners(page) {
// 监听点击事件
await page.exposeFunction('__recordClick', (selector, coordinates) => {
if (!isRecording) return;
recordStep({
type: 'click',
description: `点击元素: ${selector || '未知元素'}`,
selector: selector || '',
coordinates: coordinates || {}
});
});
// 监听输入事件
await page.exposeFunction('__recordInput', (selector, text) => {
if (!isRecording) return;
recordStep({
type: 'type',
description: `输入文本: "${text || ''}"`,
selector: selector || '',
text: text || ''
});
});
// 注入脚本监听用户操作
await page.addInitScript(() => {
// 监听点击事件
document.addEventListener('click', (event) => {
const target = event.target;
const selector = generateSelector(target);
const coordinates = { x: event.clientX, y: event.clientY };
if (window.__recordClick) {
window.__recordClick(selector, coordinates);
}
}, true);
// 监听输入事件
document.addEventListener('input', (event) => {
const target = event.target;
const selector = generateSelector(target);
const text = target.value || target.textContent || '';
if (window.__recordInput) {
window.__recordInput(selector, text);
}
}, true);
// 生成元素选择器
function generateSelector(element) {
if (!element) return '';
// 优先使用 ID
if (element.id) {
return `#${element.id}`;
}
// 使用 class 和标签名
const tagName = element.tagName.toLowerCase();
if (element.className) {
const classes = element.className.split(' ').filter(c => c.trim());
if (classes.length > 0) {
return `${tagName}.${classes.join('.')}`;
}
}
// 使用属性
const attributes = ['name', 'placeholder', 'type'];
for (const attr of attributes) {
if (element.getAttribute(attr)) {
return `${tagName}[${attr}="${element.getAttribute(attr)}"]`;
}
}
// 使用路径
return getElementPath(element);
}
function getElementPath(element) {
const path = [];
while (element && element.nodeType === Node.ELEMENT_NODE) {
let selector = element.nodeName.toLowerCase();
if (element.id) {
selector += `#${element.id}`;
path.unshift(selector);
break;
} else {
let sibling = element;
let nth = 1;
while (sibling = sibling.previousElementSibling) {
if (sibling.nodeName.toLowerCase() === selector) nth++;
}
if (nth !== 1) selector += `:nth-of-type(${nth})`;
}
path.unshift(selector);
element = element.parentNode;
}
return path.join(' > ');
}
});
}
// 主录制函数
async function startRecording() {
console.log('🎬 开始自动化录制...');
console.log('请在浏览器中进行操作,系统会自动记录您的操作步骤');
console.log('按 Ctrl+C 停止录制并保存结果\n');
let browser;
let page;
try {
// 启动浏览器
browser = await chromium.launch({
headless: recordingConfig.headless,
slowMo: recordingConfig.slowMo
});
// 创建新页面
page = await browser.newPage({
viewport: recordingConfig.viewport
});
// 设置页面监听器
await setupPageListeners(page);
// 监听页面导航
page.on('framenavigated', async (frame) => {
if (frame === page.mainFrame() && isRecording) {
recordStep({
type: 'navigate',
description: `导航到: ${frame.url()}`,
url: frame.url()
});
// 重新设置监听器
await setupPageListeners(page);
}
});
// 导航到百度首页作为起始页面
console.log('🌐 正在打开百度首页...');
await page.goto('https://www.baidu.com');
recordStep({
type: 'navigate',
description: '打开百度首页',
url: 'https://www.baidu.com'
});
console.log('✅ 录制已启动!请在浏览器中进行操作...');
console.log('💡 您可以:');
console.log(' - 点击页面元素');
console.log(' - 在输入框中输入文本');
console.log(' - 导航到其他页面');
console.log(' - 按回车键或其他键盘操作');
console.log('');
console.log('⚠️ 按 Ctrl+C 停止录制并保存结果');
// 保持浏览器打开,直到用户手动停止
await new Promise((resolve) => {
process.on('SIGINT', () => {
console.log('\n🛑 用户停止录制');
isRecording = false;
resolve();
});
});
} catch (error) {
console.error('❌ 录制过程中发生错误:', error);
} finally {
if (browser) {
// 保存录制的步骤
saveRecordedSteps();
await browser.close();
}
}
}
// 处理程序终止
process.on('SIGINT', () => {
console.log('\n🛑 用户停止录制');
saveRecordedSteps();
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\n🛑 程序终止');
saveRecordedSteps();
process.exit(0);
});
// 启动录制
(async () => {
await startRecording();
})();

158
src/electron/main/main.ts Normal file
View File

@@ -0,0 +1,158 @@
import { app, BrowserWindow, ipcMain, shell } from "electron";
import path from "node:path";
import started from "electron-squirrel-startup";
import { TabManager } from '@modules/tab-manager'
import { logger } from '@modules/logger'
import "@modules/window-size";
if (started) {
app.quit();
}
class AppMain {
private mainWindow: BrowserWindow | null = null
private tabs: TabManager | null = null
private readonly isDev = !!MAIN_WINDOW_VITE_DEV_SERVER_URL
init() {
this.registerLifecycle()
this.registerCommonIPC()
this.registerAppIPC()
this.registerLogIPC()
}
private createWindow(options?: { frameless?: boolean; route?: string }): BrowserWindow {
const frameless = !!options?.frameless
const win = new BrowserWindow({
width: 900,
height: 670,
autoHideMenuBar: true,
frame: frameless ? false : true,
// @ts-ignore
windowButtonVisibility: frameless ? false : true,
resizable: true,
maximizable: true,
minimizable: true,
webPreferences: {
devTools: this.isDev,
nodeIntegration: false,
contextIsolation: true, // 同时启动上下文隔离
sandbox: true, // 启动沙箱模式
preload: path.join(__dirname, "preload.js"),
},
})
this.loadEntry(win, options?.route)
if (this.isDev) win.webContents.openDevTools()
this.mainWindow = win
this.initTabsIfNeeded(options)
return win
}
private loadEntry(win: BrowserWindow, route?: string) {
// @ts-ignore
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
const target = route ? `${MAIN_WINDOW_VITE_DEV_SERVER_URL}${route}` : MAIN_WINDOW_VITE_DEV_SERVER_URL
win.loadURL(target)
} else {
win.loadFile(path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`))
}
// 暴露安全 API 示例:通过 IPC 处理文件读取
ipcMain.handle('read-file', async (event, filePath) => {
const fs = require('fs');
return fs.promises.readFile(filePath, 'utf-8'); // 主进程处理敏感操作
});
}
private initTabsIfNeeded(options?: { frameless?: boolean; route?: string }) {
if (!this.mainWindow) return
const route = options?.route || ''
const shouldInit = !!options?.frameless || route.startsWith('/browser')
if (!shouldInit) return
this.tabs = new TabManager(this.mainWindow)
this.registerTabIPC()
this.tabs.enable?.()
this.tabs.create('about:blank')
}
private registerLifecycle() {
app.on("ready", () => {
this.createWindow({ frameless: false, route: '/login' })
})
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit()
})
app.on("activate", () => {
if (!BrowserWindow.getAllWindows().length) this.createWindow({ frameless: false, route: '/login' })
})
}
private registerCommonIPC() {
ipcMain.handle('open-baidu', () => {
this.mainWindow?.loadURL("https://www.baidu.com")
})
ipcMain.handle("external-open", (_event, url: string) => {
try {
const allowed = /^https:\/\/(www\.)?(baidu\.com|\w+[\.-]?\w+\.[a-z]{2,})(\/.*)?$/i;
if (typeof url === "string" && allowed.test(url)) {
return shell.openExternal(url);
}
throw new Error("URL not allowed");
} catch (e) {
return Promise.reject(e);
}
})
}
private registerAppIPC() {
ipcMain.handle('app:set-frameless', async (event, route?: string) => {
const old = BrowserWindow.fromWebContents(event.sender)
const win = this.createWindow({ frameless: true, route })
if (old && !old.isDestroyed()) old.close()
return true
})
}
private registerTabIPC() {
if (!this.tabs) return
const tabs = this.tabs
ipcMain.handle('tab:create', (_e, url?: string) => tabs.create(url))
ipcMain.handle('tab:list', () => tabs.list())
ipcMain.handle('tab:navigate', (_e, payload: { tabId: string; url: string }) => tabs.navigate(payload.tabId, payload.url))
ipcMain.handle('tab:reload', (_e, tabId: string) => tabs.reload(tabId))
ipcMain.handle('tab:back', (_e, tabId: string) => tabs.goBack(tabId))
ipcMain.handle('tab:forward', (_e, tabId: string) => tabs.goForward(tabId))
ipcMain.handle('tab:switch', (_e, tabId: string) => tabs.switch(tabId))
ipcMain.handle('tab:close', (_e, tabId: string) => tabs.close(tabId))
}
private registerLogIPC() {
ipcMain.handle('log-to-main', (_e, logLevel: string, message: string) => {
switch(logLevel) {
case 'trace':
logger.trace(message)
break
case 'debug':
logger.debug(message)
break
case 'info':
logger.info(message)
break
case 'warn':
logger.warn(message)
break
case 'error':
logger.error(message)
break
default:
logger.info(message)
break
}
})
}
}
new AppMain().init()

View File

@@ -0,0 +1,95 @@
import { ipcMain, BaseWindow } from 'electron'
type Handler = (...args:any[]) => any
type AsyncHandler = (...args:any[]) => Promise<any>
export class IPCManager {
private static instance: IPCManager
private handlers: Map<string, Handler>
private asyncHandlers: Map<string,AsyncHandler>
private constructor() {
this.handlers = new Map()
this.asyncHandlers =new Map()
this.initialize()
}
public static getInstance(): IPCManager {
if (!IPCManager.instance) {
IPCManager.instance =new IPCManager()
}
return IPCManager.instance
}
private initialize(): void {
//注册同步处理器
ipcMain.on('ipc:invoke',(event, channel,...args) => {
try {
const handler = this.handlers.get(channel)
if(handler){
event.returnValue = handler(...args)
} else {
event.returnValue = { success: false, error: `No handler for channel: ${channel}`}
}
} catch (error) {
event.returnValue = { success: false, error:(error as Error).message}
}
})
// 注册异步处理器
ipcMain.handle('ipc:invokeAsync', async (_event, channel, ...args) => {
try {
const handler = this.asyncHandlers.get(channel)
if(handler){
return await handler(...args)
}
throw new Error(`No async handler for channel: ${channel}`)
} catch (error) {
throw error
}
})
ipcMain.handle('get-window-id',(event) => {
event.returnValue = event.sender.id
})
}
// 注册同步处理器
public register(channel:string, handler:Handler):void {
this.handlers.set(channel, handler)
}
// 注册异步处理器
public registerAsync(channel:string, handler:AsyncHandler):void {
this.asyncHandlers.set(channel, handler)
}
// 广播消息给所有窗口
public broadcast(channel:string, ...args:any[]):void {
BaseWindow.getAllWindows().forEach(window => {
if (!window.isDestroyed()) {
window.webContents.send(channel, ...args)
}
})
}
// 发送消息给指定窗口
public sendToWindow(windowId: string, channel:string, ...args:any[]):void {
const window = BaseWindow.fromId(windowId)
if (window && !window.isDestroyed()) {
window.webContents.send(channel, ...args)
}
}
// 清理所有处理器
public clear():void {
this.handlers.clear()
this.asyncHandlers.clear()
}
}
export const ipcManager = IPCManager.getInstance()

View File

@@ -0,0 +1,29 @@
import * as log4js from 'log4js';
log4js.configure({
appenders: {
out: {
type: 'stdout'
},
app: {
type: 'file',
filename: 'logs/app.log',
backups: 3,
compress: false,
encoding: 'utf-8',
layout: {
type: 'pattern',
pattern: '[%d{yyyy-MM-dd hh:mm:ss.SSS}] [%p] %m'
},
keepFileExt: true
}
},
categories: {
default: {
appenders: ['out', 'app'],
level: 'debug'
}
}
});
export const logger = log4js.getLogger();

View File

@@ -0,0 +1,152 @@
import { BrowserView, BrowserWindow } from 'electron'
import { randomUUID } from 'crypto'
type TabId = string
type TabInfo = { id: TabId; url: string; title: string; isLoading: boolean; canGoBack: boolean; canGoForward: boolean }
const UI_HEIGHT = 88
export class TabManager {
private win: BrowserWindow
private views: Map<TabId, BrowserView> = new Map()
private activeId: TabId | null = null
private skipNextNavigate: Map<TabId, boolean> = new Map()
private enabled = false
constructor(win: BrowserWindow) {
this.win = win
this.win.on('resize', () => this.updateActiveBounds())
}
enable() {
this.enabled = true
this.updateActiveBounds()
if (this.activeId) this.attach(this.activeId)
}
disable() {
this.enabled = false
const view = this.activeId ? this.views.get(this.activeId) : null
if (view) this.win.removeBrowserView(view)
}
list(): TabInfo[] {
return Array.from(this.views.entries()).map(([id, view]) => this.info(id, view))
}
create(url?: string): TabInfo {
const id = randomUUID()
const view = new BrowserView({ webPreferences: { sandbox: true } })
this.views.set(id, view)
if (this.enabled) this.attach(id)
const target = url && url.length > 0 ? url : 'about:blank'
view.webContents.loadURL(target)
this.bindEvents(id, view)
const info = this.info(id, view)
this.win.webContents.send('tab-created', info)
return info
}
switch(tabId: TabId): void {
if (!this.views.has(tabId)) return
if (this.enabled) this.attach(tabId)
this.win.webContents.send('tab-switched', { tabId })
}
close(tabId: TabId): void {
const view = this.views.get(tabId)
if (!view) return
if (this.activeId === tabId) {
this.win.removeBrowserView(view)
this.activeId = null
}
// @ts-ignore
view.webContents.destroy()
this.views.delete(tabId)
this.win.webContents.send('tab-closed', { tabId })
const next = this.views.keys().next().value as TabId | undefined
if (next) this.switch(next)
}
navigate(tabId: TabId, url: string): void {
const view = this.views.get(tabId)
if (!view) return
this.skipNextNavigate.set(tabId, true)
view.webContents.loadURL(url)
}
reload(tabId: TabId): void {
const view = this.views.get(tabId)
if (!view) return
view.webContents.reload()
}
goBack(tabId: TabId): void {
const view = this.views.get(tabId)
if (!view) return
if (view.webContents.canGoBack()) view.webContents.goBack()
}
goForward(tabId: TabId): void {
const view = this.views.get(tabId)
if (!view) return
if (view.webContents.canGoForward()) view.webContents.goForward()
}
private attach(tabId: TabId): void {
if (!this.enabled) return
const view = this.views.get(tabId)
if (!view) return
if (this.activeId && this.views.get(this.activeId)) {
const prev = this.views.get(this.activeId)!
this.win.removeBrowserView(prev)
}
this.activeId = tabId
this.win.addBrowserView(view)
this.updateActiveBounds()
}
private updateActiveBounds(): void {
if (!this.enabled || !this.activeId) return
const view = this.views.get(this.activeId)
if (!view) return
const [width, height] = this.win.getContentSize()
view.setBounds({ x: 0, y: UI_HEIGHT, width, height: Math.max(0, height - UI_HEIGHT) })
}
private bindEvents(id: TabId, view: BrowserView): void {
const send = () => this.win.webContents.send('tab-updated', this.info(id, view))
view.webContents.on('did-start-loading', send)
view.webContents.on('did-stop-loading', send)
view.webContents.on('did-finish-load', send)
view.webContents.on('page-title-updated', send)
view.webContents.on('did-navigate', send)
view.webContents.on('did-navigate-in-page', send)
view.webContents.on('will-navigate', (event, url) => {
if (this.skipNextNavigate.get(id)) {
this.skipNextNavigate.set(id, false)
return
}
event.preventDefault()
this.create(url)
})
view.webContents.setWindowOpenHandler(({ url }) => {
this.create(url)
return { action: 'deny' }
})
}
private info(id: TabId, view: BrowserView): TabInfo {
const wc = view.webContents
return {
id,
url: wc.getURL(),
title: wc.getTitle(),
isLoading: wc.isLoading(),
canGoBack: wc.canGoBack(),
canGoForward: wc.canGoForward()
}
}
}

View File

@@ -0,0 +1,31 @@
// 创建系统托盘
import { Tray, Menu } from 'electron'
import path from 'path'
const createTray = (app: Electron.App, win: Electron.BrowserWindow) => {
let tray = new Tray(path.join(__dirname, '../public/favicon.ico'))
tray.setToolTip('示例平台') // 鼠标放在托盘图标上的提示信息
tray.on('click', (e) => {
if (e.shiftKey) {
app.quit()
} else {
win.show()
}
})
tray.setContextMenu(
Menu.buildFromTemplate([
{
label: '退出',
click: () => {
// 先把用户的登录状态和用户的登录信息给清楚掉,再退出
app.quit()
}
}
])
)
}
module.exports = createTray

View File

@@ -0,0 +1,26 @@
import { ipcMain, BrowserWindow } from 'electron'
// 最小化
ipcMain.on('window-min', (event) => {
const webContent = event.sender
const win = BrowserWindow.fromWebContents(webContent)
win?.minimize()
})
// 最大化
ipcMain.on('window-max', (event) => {
const webContent = event.sender
const win = BrowserWindow.fromWebContents(webContent)
if (win?.isMaximized()) {
win.unmaximize()
} else {
win?.maximize()
}
})
// 关闭
ipcMain.on('window-close', (event) => {
const webContent = event.sender
const win = BrowserWindow.fromWebContents(webContent)
win?.close()
})

View File

@@ -0,0 +1,71 @@
import { contextBridge, ipcRenderer } from 'electron'
import { IPCChannel, IPCAPI } from '@/shared/types/ipc.types'
const api: IPCAPI = {
openBaidu: () => ipcRenderer.invoke('open-baidu'),
versions: process.versions,
external: {
open: (url: string) => ipcRenderer.invoke('external-open', url)
},
window: {
minimize: () => ipcRenderer.send('window-min'),
maximize: () => ipcRenderer.send('window-max'),
close: () => ipcRenderer.send('window-close')
},
app: {
setFrameless: (route?: string) => ipcRenderer.invoke('app:set-frameless', route)
},
tabs: {
create: (url?: string) => ipcRenderer.invoke('tab:create', url),
list: () => ipcRenderer.invoke('tab:list'),
navigate: (tabId: string, url: string) => ipcRenderer.invoke('tab:navigate', { tabId, url }),
reload: (tabId: string) => ipcRenderer.invoke('tab:reload', tabId),
back: (tabId: string) => ipcRenderer.invoke('tab:back', tabId),
forward: (tabId: string) => ipcRenderer.invoke('tab:forward', tabId),
switch: (tabId: string) => ipcRenderer.invoke('tab:switch', tabId),
close: (tabId: string) => ipcRenderer.invoke('tab:close', tabId),
on: (event: 'tab-updated' | 'tab-created' | 'tab-closed' | 'tab-switched', handler: (payload: any) => void) => {
const listener = (_e: any, payload: any) => handler(payload)
ipcRenderer.on(event, listener)
return () => ipcRenderer.removeListener(event, listener)
}
},
// 通过 IPC 调用主进程
readFile: (filePath: string) => ipcRenderer.invoke('read-file', filePath),
// 同步调用
invoke: (channel: IPCChannel, ...args: any[]) => ipcRenderer.sendSync('ipc:invoke', channel, ...args),
// 异步调用
invokeAsync: (channel: IPCChannel, ...args: any[]) => {
try {
ipcRenderer.invoke('ipc:invokeAsync', channel, ...args)
} catch (error) {
throw error
}
},
// 监听主进程消息
on: (event: IPCChannel, callback: (...args: any[]) => void) => {
const subscription = (_event: any, ...args: any[]) => callback(...args)
ipcRenderer.on(event, subscription)
return () => ipcRenderer.removeListener(event, subscription)
},
// 发送消息到主进程
send: (channel: IPCChannel, ...args: any[]) => ipcRenderer.send(channel, ...args),
// 获取窗口ID
getCurrentWindowId: () => ipcRenderer.sendSync(IPCChannel.GET_WINDOW_ID),
// 发送日志
logToMain: (logLevel: string, message: string) => ipcRenderer.send('log-to-main', logLevel, message),
}
contextBridge.exposeInMainWorld('ipcAPI', api)