-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.js
More file actions
executable file
·144 lines (124 loc) · 4.77 KB
/
Copy pathinject.js
File metadata and controls
executable file
·144 lines (124 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(() => {
if (window.__recorderAttached) return;
window.__recorderAttached = true;
// lightweight id
const pid = (crypto?.randomUUID?.() || Math.random().toString(16).slice(2)).slice(0,8);
const log = (obj) => {
try { console.log(JSON.stringify({ __rec: 1, page_id: pid, ...obj })); }
catch { console.log(JSON.stringify({ __rec: 1, page_id: pid, type: "log_error" })); }
};
const isText = (el) => {
if (!el || !el.tagName) return false;
const t = el.tagName.toLowerCase();
if (t === 'textarea') return true;
if (t !== 'input') return false;
const typ = (el.type || 'text').toLowerCase();
return ['text','search','email','url','tel','number'].includes(typ);
};
const isPassword = (el) => el?.tagName?.toLowerCase()==='input' && (el.type||'').toLowerCase()==='password';
let lastMouse = 0;
document.addEventListener('mousemove', (e) => {
const now = Date.now();
if (now - lastMouse > 100) {
log({ type:'mouse_move', x:e.clientX, y:e.clientY, ts: now });
lastMouse = now;
}
}, { capture:true, passive:true });
document.addEventListener('click', (e) => {
const a = e.target?.closest?.('a');
log({
type:'click',
x:e.clientX, y:e.clientY,
element: e.target?.tagName || 'UNKNOWN',
href: a ? (a.href || null) : null,
ts: Date.now()
});
}, { capture:true });
let scrolling = false, sx=0, sy=0, st=0, tmr;
addEventListener('scroll', () => {
const now = Date.now();
if (!scrolling) {
scrolling = true; sx=scrollX; sy=scrollY; st=now;
log({ type:'scroll_start', startX:sx, startY:sy, ts: now });
}
clearTimeout(tmr);
tmr = setTimeout(() => {
const ex = scrollX, ey = scrollY, dur = Date.now() - st;
log({ type:'scroll_end', startX:sx, startY:sy, endX:ex, endY:ey, dx:ex-sx, dy:ey-sy, duration_ms:dur, ts: Date.now() });
scrolling = false;
}, 400);
}, { capture:true, passive:true });
document.addEventListener('input', (e) => {
const el = e.target;
if (!isText(el) || isPassword(el)) return;
let val = ''; try { val = String(el.value ?? ''); } catch {}
log({ type:'type', element: el.tagName, id: el.id || null, name: el.name || null, value: val, ts: Date.now() });
}, { capture:true });
document.addEventListener('keydown', (e) => {
const el = e.target;
if (!isText(el) || isPassword(el)) return;
if (e.key === 'Enter') {
let val = ''; try { val = String(el.value ?? ''); } catch {}
log({ type:'type_commit', key:'Enter', element: el.tagName, id: el.id || null, name: el.name || null, value: val, ts: Date.now() });
}
}, { capture:true });
document.addEventListener('blur', (e) => {
const el = e.target;
if (!isText(el) || isPassword(el)) return;
let val = ''; try { val = String(el.value ?? ''); } catch {}
log({ type:'type_commit', key:'blur', element: el.tagName, id: el.id || null, name: el.name || null, value: val, ts: Date.now() });
}, true);
window.addEventListener('focus', () => {
log({ type: 'window_focus', ts: Date.now() });
}, true);
window.addEventListener('blur', () => {
log({ type: 'window_blur', ts: Date.now() });
}, true);
document.addEventListener('visibilitychange', () => {
log({
type: document.hidden ? 'tab_hidden' : 'tab_visible',
visibility: document.visibilityState,
url: window.location.href,
title: document.title,
ts: Date.now()
});
}, true);
let lastUrl = window.location.href;
const checkUrlChange = () => {
const currentUrl = window.location.href;
if (currentUrl !== lastUrl) {
log({
type: 'navigation',
from: lastUrl,
to: currentUrl,
ts: Date.now()
});
lastUrl = currentUrl;
}
};
// Override history methods to catch SPA navigation
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
history.pushState = function(...args) {
originalPushState.apply(this, args);
checkUrlChange();
};
history.replaceState = function(...args) {
originalReplaceState.apply(this, args);
checkUrlChange();
};
// Also listen for popstate (back/forward buttons)
window.addEventListener('popstate', () => {
checkUrlChange();
});
// Initial page load
log({
type: 'recorder_init',
url: window.location.href,
title: document.title,
userAgent: navigator.userAgent,
viewport: { width: window.innerWidth, height: window.innerHeight },
ts: Date.now(),
t_since_nav_ms: tRel()
});
})();