Path: blob/main/replay/injected-scripts/domReplayerSubscribe.ts
1029 views
import { ipcRenderer } from 'electron';12declare global {3interface Window {4replayDomChanges(...args: any[]);5replayInteractions(...args: any[]);6getIsMainFrame: () => boolean;7debugLogs: any[];8debugToConsole: boolean;9}10}1112window.getIsMainFrame = function () {13return process.isMainFrame;14};15window.debugToConsole = false;1617if (process.isMainFrame) {18console.log(`19__, _,20( _/_ / | _/_21\`. _ _, _ _ / /--| _, _ _ _ /22(___)(/_(__/ (_(/_(__ _/ |_(_)_(/_/ / /_(__23/|24(/25`);26console.group('About SecretAgent Replay');27const osMessage =28process.platform === 'win32'29? `\n\nWindows users: you can add this line to the beginning of your script 'process.env.SA_SHOW_BROWSER="true"'.`30: '';31console.log(32`This is a high fidelity %c"Replay"%c of your scraping session.3334It 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 :).3536To launch a real browser, use the env variable %cSA_SHOW_BROWSER=true${osMessage}`,37'font-weight:bold',38'font-weight:normal',39'background:#eee;padding:2px;',40);41console.groupEnd();42}4344function debugLog(message: string, ...args: any[]) {45if (window.debugToConsole) {46console.log(...arguments);47}48if (!window.debugLogs) window.debugLogs = [];49window.debugLogs.push({ message, args });50}5152debugLog(53'Loaded: isMain=%s, pid=%s, href=%s',54window.getIsMainFrame(),55process?.pid,56window.location.href,57);5859if (process.isMainFrame) {60ipcRenderer.on(61'dom:apply',62(event, columns, rawDomChanges, resultNodeIds, mouseEvent, scrollEvent) => {63debugLog(64'Events: changes=%s, highlighted=%s, hasMouse=%s, hasScroll=%s',65rawDomChanges?.length ?? 0,66resultNodeIds ? JSON.stringify(resultNodeIds) : '[]',67!!mouseEvent,68!!scrollEvent,69);70if (rawDomChanges?.length) {71const domChanges = [];72for (const change of rawDomChanges) {73const record: any = {};74for (let i = 0; i < columns.length; i += 1) {75record[columns[i]] = change[i];76}77domChanges.push(record);78}79window.replayDomChanges(domChanges);80}81window.replayInteractions(resultNodeIds, mouseEvent, scrollEvent);82},83);84}858687