Files
zn-ai/electron/service/script-recorder-service/index.ts
2026-04-12 15:46:28 +08:00

57 lines
1.7 KiB
TypeScript
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.

import { chromium } from 'playwright';
import log from 'electron-log';
import { launchLocalChrome } from '@electron/utils/chrome/launchLocalChrome';
let recorderBrowser: any = null;
let recorderContext: any = null;
export async function startRecording(url?: string): Promise<{ success: boolean; code?: string; error?: string }> {
try {
await launchLocalChrome();
if (recorderBrowser) {
await stopRecording();
}
recorderBrowser = await chromium.connectOverCDP('http://127.0.0.1:9222');
recorderContext = recorderBrowser.contexts()[0] || (await recorderBrowser.newContext());
const page = await recorderContext.newPage();
const targetUrl = url || 'about:blank';
await page.goto(targetUrl, { waitUntil: 'domcontentloaded' });
// 唤起 Playwright Inspector让用户手动录制并生成代码
await page.pause();
return {
success: true,
code: '',
};
} catch (error: any) {
log.error('[script-recorder-service] Failed to start recording:', error);
return {
success: false,
error: error?.message || 'Failed to start recording',
};
}
}
export async function stopRecording(): Promise<{ success: boolean; code?: string; error?: string }> {
try {
if (recorderContext) {
await recorderContext.close().catch(() => {});
recorderContext = null;
}
if (recorderBrowser) {
await recorderBrowser.close().catch(() => {});
recorderBrowser = null;
}
return { success: true, code: '' };
} catch (error: any) {
log.error('[script-recorder-service] Failed to stop recording:', error);
return {
success: false,
error: error?.message || 'Failed to stop recording',
};
}
}