-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
95 lines (87 loc) · 2.56 KB
/
content.js
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
let recording = false;
let actions = [];
let startTime;
// Listen for keyboard commands
document.addEventListener('keydown', (event) => {
if ((event.metaKey || event.ctrlKey) && event.shiftKey && event.key.toLowerCase() === 'o') {
event.preventDefault();
if (!recording) {
startRecording();
} else {
stopRecording();
}
} else if ((event.metaKey || event.ctrlKey) && event.shiftKey && event.key.toLowerCase() === 'p') {
event.preventDefault();
replayActions();
}
});
function startRecording() {
if (recording) return; // Prevent multiple recordings
recording = true;
actions = [];
startTime = Date.now();
document.addEventListener('mousemove', recordMouseMove);
document.addEventListener('click', recordClick);
console.log('Recording started');
}
function stopRecording() {
if (!recording) return; // Prevent stopping if not recording
recording = false;
document.removeEventListener('mousemove', recordMouseMove);
document.removeEventListener('click', recordClick);
chrome.storage.local.set({ actions: actions }, () => {
console.log('Recording stopped and actions saved');
});
}
function recordMouseMove(event) {
if (!recording) return;
actions.push({
type: 'mousemove',
time: Date.now() - startTime,
x: event.clientX,
y: event.clientY
});
}
function recordClick(event) {
if (!recording) return;
actions.push({
type: 'click',
time: Date.now() - startTime,
x: event.clientX,
y: event.clientY
});
}
function replayActions() {
chrome.storage.local.get(['actions'], (result) => {
if (!result.actions || result.actions.length === 0) {
console.log('No actions to replay');
return;
}
console.log('Replaying actions');
let startTime = Date.now();
result.actions.forEach(action => {
setTimeout(() => {
const simulatedEvent = new MouseEvent(action.type, {
clientX: action.x,
clientY: action.y,
bubbles: true,
cancelable: true,
view: window
});
try {
document.elementFromPoint(action.x, action.y)?.dispatchEvent(simulatedEvent);
} catch (error) {
console.error('Error dispatching event:', error);
}
}, action.time);
});
});
}
// Handle context invalidation (if extension is reloaded or tab is refreshed)
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'contextInvalidated') {
recording = false;
actions = [];
console.log('Extension context invalidated. Actions cleared.');
}
});