Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/replay/injected-scripts/domReplayerSubscribe.ts
1029 views
1
import { ipcRenderer } from 'electron';
2
3
declare global {
4
interface Window {
5
replayDomChanges(...args: any[]);
6
replayInteractions(...args: any[]);
7
getIsMainFrame: () => boolean;
8
debugLogs: any[];
9
debugToConsole: boolean;
10
}
11
}
12
13
window.getIsMainFrame = function () {
14
return process.isMainFrame;
15
};
16
window.debugToConsole = false;
17
18
if (process.isMainFrame) {
19
console.log(`
20
__, _,
21
( _/_ / | _/_
22
\`. _ _, _ _ / /--| _, _ _ _ /
23
(___)(/_(__/ (_(/_(__ _/ |_(_)_(/_/ / /_(__
24
/|
25
(/
26
`);
27
console.group('About SecretAgent Replay');
28
const osMessage =
29
process.platform === 'win32'
30
? `\n\nWindows users: you can add this line to the beginning of your script 'process.env.SA_SHOW_BROWSER="true"'.`
31
: '';
32
console.log(
33
`This is a high fidelity %c"Replay"%c of your scraping session.
34
35
It is NOT a live Chrome view, so if you notice Javascript is not loaded or cookies/console are not working, that's just because this is not the headless Chrome running your actual script :).
36
37
To launch a real browser, use the env variable %cSA_SHOW_BROWSER=true${osMessage}`,
38
'font-weight:bold',
39
'font-weight:normal',
40
'background:#eee;padding:2px;',
41
);
42
console.groupEnd();
43
}
44
45
function debugLog(message: string, ...args: any[]) {
46
if (window.debugToConsole) {
47
console.log(...arguments);
48
}
49
if (!window.debugLogs) window.debugLogs = [];
50
window.debugLogs.push({ message, args });
51
}
52
53
debugLog(
54
'Loaded: isMain=%s, pid=%s, href=%s',
55
window.getIsMainFrame(),
56
process?.pid,
57
window.location.href,
58
);
59
60
if (process.isMainFrame) {
61
ipcRenderer.on(
62
'dom:apply',
63
(event, columns, rawDomChanges, resultNodeIds, mouseEvent, scrollEvent) => {
64
debugLog(
65
'Events: changes=%s, highlighted=%s, hasMouse=%s, hasScroll=%s',
66
rawDomChanges?.length ?? 0,
67
resultNodeIds ? JSON.stringify(resultNodeIds) : '[]',
68
!!mouseEvent,
69
!!scrollEvent,
70
);
71
if (rawDomChanges?.length) {
72
const domChanges = [];
73
for (const change of rawDomChanges) {
74
const record: any = {};
75
for (let i = 0; i < columns.length; i += 1) {
76
record[columns[i]] = change[i];
77
}
78
domChanges.push(record);
79
}
80
window.replayDomChanges(domChanges);
81
}
82
window.replayInteractions(resultNodeIds, mouseEvent, scrollEvent);
83
},
84
);
85
}
86
87