refactor: 移除所有录制功能代码
- 删除录制相关页面组件(RecordReplayPage、ReplayListPage、ReplayPlayerPage) - 删除录制工具文件(recordEventsDb、recordUtils、useRecorder、tabUtils) - 清理 background.ts 和 content.ts 中的录制代码 - 更新 App.tsx 移除录制路由 - 清理 messages.tsx 中的消息定义 - 删除 types.tsx 中的录制状态 - 移除 package.json 中的 rrweb 相关依赖 - 更新 wxt.config.ts 移除 offscreen、downloads 权限 - 删除 offscreen 目录 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+2
-22
@@ -1,28 +1,8 @@
|
||||
import { defineExtensionMessaging } from '@webext-core/messaging';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
||||
interface ProtocolMap {
|
||||
// --- Popup 相关 ---
|
||||
'popup:start': () => { ok: boolean; error?: string };
|
||||
'popup:stop': () => { ok: boolean; error?: string };
|
||||
'popup:started': () => void;
|
||||
'popup:stopped': () => void;
|
||||
'popup:tab-changed': (data: { currentTabId: number; recordingTabId: number | undefined }) => void;
|
||||
'popup:check-status': () => { active: boolean; startTime?: number };
|
||||
'popup:ready': () => void;
|
||||
'popup:get-sessions': () => { id: string; startTime: number; tabId: number; chunkCount: number; totalEvents: number }[];
|
||||
'popup:get-session-events': (sessionId: string) => unknown[];
|
||||
'popup:delete-session': (sessionId: string) => { ok: boolean };
|
||||
'popup:download-session': (sessionId: string) => { ok: boolean };
|
||||
|
||||
// --- Content 相关 ---
|
||||
'content:save-track-events': (event: unknown) => boolean;
|
||||
'content:start-recording': () => { ok: boolean; error?: string };
|
||||
'content:stop-recording': () => { ok: boolean };
|
||||
'content:check-status': () => boolean;
|
||||
|
||||
// --- Offscreen 相关 ---
|
||||
'offscreen:start-recording': (streamId: string) => void;
|
||||
'offscreen:stop-recording': () => void;
|
||||
// Placeholder - 扩展消息协议
|
||||
}
|
||||
|
||||
export const { sendMessage, onMessage } = defineExtensionMessaging<ProtocolMap>();
|
||||
|
||||
@@ -1,367 +0,0 @@
|
||||
/**
|
||||
* IndexedDB 工具模块 - 用于分块存储录制事件
|
||||
* 避免长时间录制导致内存溢出
|
||||
*/
|
||||
|
||||
const DB_NAME = 'recording-events-db';
|
||||
const DB_VERSION = 1;
|
||||
const STORE_NAME = 'events';
|
||||
const CHUNK_SIZE = 100; // 每个 chunk 存储的事件数量
|
||||
|
||||
export interface EventChunk {
|
||||
id: number;
|
||||
sessionId: string;
|
||||
chunkIndex: number;
|
||||
events: unknown[];
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
interface RecordingSession {
|
||||
id: string;
|
||||
startTime: number;
|
||||
tabId: number;
|
||||
chunkCount: number;
|
||||
totalEvents: number;
|
||||
}
|
||||
|
||||
let db: IDBDatabase | null = null;
|
||||
|
||||
/**
|
||||
* 打开数据库连接
|
||||
*/
|
||||
export async function openDB(): Promise<IDBDatabase> {
|
||||
if (db) return db;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
||||
|
||||
request.onerror = () => reject(request.error);
|
||||
request.onsuccess = () => {
|
||||
db = request.result;
|
||||
resolve(db);
|
||||
};
|
||||
|
||||
request.onupgradeneeded = (event) => {
|
||||
const database = (event.target as IDBOpenDBRequest).result;
|
||||
|
||||
// 创建事件 chunk 存储
|
||||
if (!database.objectStoreNames.contains(STORE_NAME)) {
|
||||
const store = database.createObjectStore(STORE_NAME, { keyPath: 'id', autoIncrement: true });
|
||||
store.createIndex('sessionId', 'sessionId', { unique: false });
|
||||
store.createIndex('chunkIndex', 'chunkIndex', { unique: false });
|
||||
store.createIndex('sessionId_chunkIndex', ['sessionId', 'chunkIndex'], { unique: true });
|
||||
}
|
||||
|
||||
// 创建录制会话存储
|
||||
if (!database.objectStoreNames.contains('sessions')) {
|
||||
const sessionStore = database.createObjectStore('sessions', { keyPath: 'id' });
|
||||
sessionStore.createIndex('startTime', 'startTime', { unique: false });
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化新的录制会话
|
||||
*/
|
||||
export async function initRecordingSession(sessionId: string, tabId: number): Promise<void> {
|
||||
const database = await openDB();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = database.transaction('sessions', 'readwrite');
|
||||
const store = tx.objectStore('sessions');
|
||||
|
||||
const session: RecordingSession = {
|
||||
id: sessionId,
|
||||
startTime: Date.now(),
|
||||
tabId,
|
||||
chunkCount: 0,
|
||||
totalEvents: 0,
|
||||
};
|
||||
|
||||
const request = store.put(session);
|
||||
request.onsuccess = () => resolve();
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前会话的 chunk 数量
|
||||
*/
|
||||
export async function getSessionChunkCount(sessionId: string): Promise<number> {
|
||||
// 参数验证
|
||||
if (!sessionId || typeof sessionId !== 'string') {
|
||||
console.error('[db] Invalid sessionId for getSessionChunkCount:', sessionId);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const database = await openDB();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const tx = database.transaction('sessions', 'readonly');
|
||||
const store = tx.objectStore('sessions');
|
||||
|
||||
try {
|
||||
const request = store.get(sessionId);
|
||||
|
||||
request.onsuccess = () => {
|
||||
const session = request.result as RecordingSession | undefined;
|
||||
resolve(session?.chunkCount || 0);
|
||||
};
|
||||
request.onerror = (e) => {
|
||||
console.error('[db] Error getting session chunk count:', e);
|
||||
resolve(0);
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[db] Exception getting session chunk count:', error);
|
||||
resolve(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加事件到存储(自动分块)
|
||||
* 当当前 chunk 满时创建新 chunk
|
||||
*/
|
||||
export async function appendEvents(sessionId: string, newEvents: unknown[]): Promise<void> {
|
||||
const database = await openDB();
|
||||
const chunkCount = await getSessionChunkCount(sessionId);
|
||||
|
||||
// 获取或创建当前 chunk
|
||||
let currentChunk: EventChunk | null = null;
|
||||
let currentChunkIndex = chunkCount > 0 ? chunkCount - 1 : 0;
|
||||
|
||||
if (chunkCount > 0) {
|
||||
currentChunk = await getChunk(database, sessionId, currentChunkIndex);
|
||||
}
|
||||
|
||||
// 如果当前 chunk 不存在或已满,创建新 chunk
|
||||
if (!currentChunk || currentChunk.events.length >= CHUNK_SIZE) {
|
||||
currentChunkIndex = chunkCount;
|
||||
currentChunk = {
|
||||
id: 0, // 将由 IndexedDB 自动分配
|
||||
sessionId,
|
||||
chunkIndex: currentChunkIndex,
|
||||
events: [],
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
|
||||
// 更新会话的 chunk 计数
|
||||
await updateSessionChunkCount(database, sessionId, chunkCount + 1);
|
||||
}
|
||||
|
||||
// 将新事件添加到当前 chunk
|
||||
currentChunk.events.push(...newEvents);
|
||||
|
||||
// 保存 chunk
|
||||
await saveChunk(database, currentChunk);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定 chunk
|
||||
*/
|
||||
async function getChunk(
|
||||
database: IDBDatabase,
|
||||
sessionId: string,
|
||||
chunkIndex: number,
|
||||
): Promise<EventChunk | null> {
|
||||
// 参数验证
|
||||
if (!sessionId || typeof sessionId !== 'string') {
|
||||
console.error('[db] Invalid sessionId for getChunk:', sessionId);
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
const tx = database.transaction(STORE_NAME, 'readonly');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const index = store.index('sessionId_chunkIndex');
|
||||
const request = index.get([sessionId, chunkIndex]);
|
||||
|
||||
request.onsuccess = () => resolve(request.result || null);
|
||||
request.onerror = (e) => {
|
||||
console.error('[db] Error getting chunk:', e);
|
||||
resolve(null);
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[db] Exception getting chunk:', error);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存 chunk
|
||||
*/
|
||||
async function saveChunk(database: IDBDatabase, chunk: EventChunk): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = database.transaction(STORE_NAME, 'readwrite');
|
||||
const store = tx.objectStore(STORE_NAME);
|
||||
const request = store.put(chunk);
|
||||
|
||||
request.onsuccess = () => resolve();
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新会话的 chunk 计数
|
||||
*/
|
||||
async function updateSessionChunkCount(
|
||||
database: IDBDatabase,
|
||||
sessionId: string,
|
||||
chunkCount: number,
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = database.transaction('sessions', 'readwrite');
|
||||
const store = tx.objectStore('sessions');
|
||||
const getRequest = store.get(sessionId);
|
||||
|
||||
getRequest.onsuccess = () => {
|
||||
const session = getRequest.result as RecordingSession | undefined;
|
||||
if (session) {
|
||||
session.chunkCount = chunkCount;
|
||||
const putRequest = store.put(session);
|
||||
putRequest.onsuccess = () => resolve();
|
||||
putRequest.onerror = () => reject(putRequest.error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
getRequest.onerror = () => reject(getRequest.error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式读取所有事件(按 chunk 顺序)
|
||||
* 使用回调方式避免一次性加载所有事件到内存
|
||||
*/
|
||||
export async function streamAllEvents(
|
||||
sessionId: string,
|
||||
onChunk: (events: unknown[]) => void | Promise<void>,
|
||||
): Promise<void> {
|
||||
// 参数验证
|
||||
if (!sessionId || typeof sessionId !== 'string') {
|
||||
console.error('[db] Invalid sessionId for streamAllEvents:', sessionId);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const database = await openDB();
|
||||
const chunkCount = await getSessionChunkCount(sessionId);
|
||||
|
||||
console.log(`[db] Streaming ${chunkCount} chunks for session ${sessionId}`);
|
||||
|
||||
for (let i = 0; i < chunkCount; i++) {
|
||||
const chunk = await getChunk(database, sessionId, i);
|
||||
if (chunk && chunk.events.length > 0) {
|
||||
await onChunk(chunk.events);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[db] Error streaming events:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有事件(用于下载,一次性加载)
|
||||
* 注意:这会将所有事件加载到内存,仅在需要导出时调用
|
||||
*/
|
||||
export async function getAllEvents(sessionId: string): Promise<unknown[]> {
|
||||
// 参数验证
|
||||
if (!sessionId || typeof sessionId !== 'string') {
|
||||
console.error('[db] Invalid sessionId for getAllEvents:', sessionId);
|
||||
return [];
|
||||
}
|
||||
|
||||
const allEvents: unknown[] = [];
|
||||
|
||||
await streamAllEvents(sessionId, (events) => {
|
||||
allEvents.push(...events);
|
||||
});
|
||||
|
||||
return allEvents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除录制会话的所有数据
|
||||
*/
|
||||
export async function deleteRecordingSession(sessionId: string): Promise<void> {
|
||||
const database = await openDB();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// 删除所有相关的 chunk
|
||||
const chunkTx = database.transaction(STORE_NAME, 'readwrite');
|
||||
const chunkStore = chunkTx.objectStore(STORE_NAME);
|
||||
const index = chunkStore.index('sessionId');
|
||||
const request = index.openCursor(IDBKeyRange.only(sessionId));
|
||||
|
||||
request.onsuccess = (event) => {
|
||||
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
|
||||
if (cursor) {
|
||||
cursor.delete();
|
||||
cursor.continue();
|
||||
}
|
||||
};
|
||||
|
||||
chunkTx.oncomplete = () => {
|
||||
// 删除会话记录
|
||||
const sessionTx = database.transaction('sessions', 'readwrite');
|
||||
const sessionStore = sessionTx.objectStore('sessions');
|
||||
const deleteRequest = sessionStore.delete(sessionId);
|
||||
|
||||
deleteRequest.onsuccess = () => resolve();
|
||||
deleteRequest.onerror = () => reject(deleteRequest.error);
|
||||
};
|
||||
|
||||
chunkTx.onerror = () => reject(chunkTx.error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有录制会话列表
|
||||
*/
|
||||
export async function getAllSessions(): Promise<RecordingSession[]> {
|
||||
const database = await openDB();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = database.transaction('sessions', 'readonly');
|
||||
const store = tx.objectStore('sessions');
|
||||
const index = store.index('startTime');
|
||||
const request = index.openCursor(null, 'prev'); // 按时间倒序排列
|
||||
const sessions: RecordingSession[] = [];
|
||||
|
||||
request.onsuccess = (event) => {
|
||||
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
|
||||
if (cursor) {
|
||||
sessions.push(cursor.value);
|
||||
cursor.continue();
|
||||
} else {
|
||||
resolve(sessions);
|
||||
}
|
||||
};
|
||||
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成唯一的会话 ID
|
||||
*/
|
||||
export function generateSessionId(): string {
|
||||
return `session_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理数据库(用于调试或重置)
|
||||
*/
|
||||
export async function clearDatabase(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.deleteDatabase(DB_NAME);
|
||||
request.onsuccess = () => {
|
||||
db = null;
|
||||
resolve();
|
||||
};
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
export const downloadHtmlInBackground = (events: unknown[]) => {
|
||||
if (!events || events.length === 0) return;
|
||||
|
||||
// 安全转义
|
||||
const safeEventsString = JSON.stringify(events)
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/`/g, '\\`')
|
||||
.replace(/<\/script>/g, '<\\/script>');
|
||||
|
||||
// 使用固定版本的 rrweb-player,确保与项目使用的 rrweb 版本兼容
|
||||
const htmlContent = `
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>RRWeb 回放</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/rrweb-player@1.0.0-alpha.4/dist/style.css" />
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
background: #f0f2f5;
|
||||
}
|
||||
#player {
|
||||
width: 100%;
|
||||
max-width: 1024px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="player"></div>
|
||||
|
||||
<!-- 使用与项目 rrweb 版本匹配的 rrweb-player -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/rrweb-player@1.0.0-alpha.4/dist/index.js"></script>
|
||||
|
||||
<script>
|
||||
try {
|
||||
// 解析事件数据
|
||||
const events = JSON.parse(\`${safeEventsString}\`);
|
||||
console.log('Loaded events count:', events.length);
|
||||
|
||||
// 创建播放器
|
||||
const player = new rrwebPlayer({
|
||||
target: document.getElementById('player'),
|
||||
props: {
|
||||
events: events,
|
||||
width: 1024,
|
||||
height: 576,
|
||||
autoPlay: true,
|
||||
showController: true,
|
||||
UNSAFE_replayCanvas: true
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Player created successfully');
|
||||
} catch (error) {
|
||||
console.error('Replay error:', error);
|
||||
document.body.innerHTML = \`
|
||||
<div style="padding: 20px; text-align: center; color: #d32f2f;">
|
||||
<h2>回放失败</h2>
|
||||
<p>\${error.message}</p>
|
||||
<p>请检查控制台(F12)获取详细错误信息。</p>
|
||||
</div>
|
||||
\`;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
// 下载逻辑
|
||||
const blob = new Blob([htmlContent], { type: 'text/html' });
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
chrome.downloads
|
||||
.download({
|
||||
url: reader.result as string,
|
||||
filename: `replay-${Date.now()}.html`,
|
||||
saveAs: true,
|
||||
})
|
||||
.then((r) => console.log('Download started:', r));
|
||||
};
|
||||
reader.readAsDataURL(blob);
|
||||
};
|
||||
@@ -1,35 +0,0 @@
|
||||
import { browser } from 'wxt/browser';
|
||||
|
||||
/**
|
||||
* 获取当前活动标签页的 ID
|
||||
* @returns Promise<number | undefined> 返回活动标签页的 ID,如果未找到则返回 undefined
|
||||
*/
|
||||
export async function getActiveTabId(): Promise<number | undefined> {
|
||||
const activeTabs = await browser.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true,
|
||||
});
|
||||
const activeTab = activeTabs[0];
|
||||
|
||||
if (!activeTab) {
|
||||
throw new Error('No active tab found.');
|
||||
}
|
||||
|
||||
// 检查URL是否受限
|
||||
if (activeTab?.url) {
|
||||
const restrictedProtocols = [
|
||||
'chrome:',
|
||||
'chrome-extension:',
|
||||
'about:',
|
||||
'edge:',
|
||||
'view-source:',
|
||||
'data:',
|
||||
'file:',
|
||||
];
|
||||
if (restrictedProtocols.some((protocol) => activeTab.url!.startsWith(protocol))) {
|
||||
throw new Error(`Cannot send message to a restricted URL: ${activeTab.url}`);
|
||||
}
|
||||
}
|
||||
|
||||
return activeTab.id;
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
import * as rrweb from 'rrweb';
|
||||
import { listenerHandler } from '@rrweb/types';
|
||||
import { getRecordConsolePlugin } from '@rrweb/rrweb-plugin-console-record';
|
||||
import { sendMessage } from '@/utils/messages';
|
||||
|
||||
export const createRecorder = () => {
|
||||
let stopFn: listenerHandler | null = null;
|
||||
|
||||
const startRecord = async () => {
|
||||
try {
|
||||
const handler = rrweb.record({
|
||||
emit(event) {
|
||||
sendMessage('content:save-track-events', event);
|
||||
},
|
||||
plugins: [getRecordConsolePlugin()],
|
||||
});
|
||||
|
||||
stopFn = handler || null;
|
||||
|
||||
console.log('[content] rrweb started');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Failed to start recording:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const stopRecord = () => {
|
||||
if (stopFn) {
|
||||
stopFn();
|
||||
stopFn = null;
|
||||
console.log('[content] rrweb stopped');
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return { startRecord, stopRecord };
|
||||
};
|
||||
Reference in New Issue
Block a user