init the repo and integrate rrweb-snapshot

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
commit e2fbecea96
9 changed files with 126 additions and 0 deletions

3
src/index.ts Normal file
View File

@@ -0,0 +1,3 @@
import record from './record';
export { record };

35
src/record.ts Normal file
View File

@@ -0,0 +1,35 @@
import { snapshot } from 'rrweb-snapshot';
import { EventType, event } from './types';
function on(
type: string,
fn: EventListenerOrEventListenerObject,
target = document,
) {
target.addEventListener(type, fn);
}
function createEvent(type: EventType, data: any): event {
return {
type,
data,
timestamp: Date.now(),
};
}
function emit(e: event) {}
function record() {
on('DOMContentLoaded', () => {
emit(
createEvent(EventType.DomContentLoaded, { href: window.location.href }),
);
});
on('load', () => {
emit(createEvent(EventType.Load, null));
const node = snapshot(document);
emit(createEvent(EventType.FullSnapshot, { node }));
});
}
export default record;

12
src/types.ts Normal file
View File

@@ -0,0 +1,12 @@
export enum EventType {
DomContentLoaded,
Load,
FullSnapshot,
IncrementalSnapshot,
}
export type event = {
type: EventType;
timestamp: number;
data: any;
};