forked from gajananpp/puppeteer-ide-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.ts
165 lines (141 loc) · 4.27 KB
/
background.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {ExtensionDebuggerTransport} from 'puppeteer-extension-transport';
export interface ExecutionCommand {
type: 'startExecution' | 'stopExecution';
tabId: number;
}
export interface ExecutionEvent {
type: 'executionStarted' | 'executionStopped';
tabId: number;
}
export interface CDPCommand {
type: 'cdpCommand';
command: string;
}
export interface CDPEvent {
type: 'cdpEvent';
data: string;
}
export interface ConnectionEvent {
type: 'connected';
}
export interface ConsoleCommand {
type: 'console';
level: 'log' | 'error';
args: string;
}
export type Message =
| ExecutionCommand
| CDPCommand
| ConsoleCommand
| CDPEvent
| ExecutionEvent
| ConnectionEvent;
interface Connections {
/** key is the stringified tabId */
[key: string]: chrome.runtime.Port;
}
const connections: Connections = {};
chrome.runtime.onConnect.addListener(port => {
connections[port.name] = port;
const connectedEvent: ConnectionEvent = {type: 'connected'};
port.postMessage(connectedEvent);
port.onMessage.addListener((message: Message) => {
message.type === 'startExecution'
? DebuggerHandler.create(message.tabId)
: null;
});
port.onDisconnect.addListener(port => delete connections[port.name]);
});
class DebuggerHandler {
private static _debuggerHandler: DebuggerHandler;
static isExecuting = false;
transport: ExtensionDebuggerTransport;
tabId: number;
commands: {command: any; response: any}[];
events: any[];
/**
* Starts debugger session, executes incoming cdp commands on target tab
* and emits events/responses back to command sender
* @param tabId - id of the target tab
*/
static async create(tabId: number) {
if (this.isExecuting && this._debuggerHandler) {
this._debuggerHandler._registerListeners();
return this._debuggerHandler;
} else {
const transport = await ExtensionDebuggerTransport.create(tabId);
this._debuggerHandler = new DebuggerHandler(tabId, transport);
return this._debuggerHandler;
}
}
constructor(tabId: number, transport: ExtensionDebuggerTransport) {
DebuggerHandler.isExecuting = true;
this.tabId = tabId;
this.transport = transport;
this.transport.delay = 0.05 * 1000;
this.commands = [];
this.events = [];
this._registerListeners();
}
private _registerListeners() {
const port = connections[this.tabId];
this.transport.onmessage = message => {
const cdpEvent: CDPEvent = {
type: 'cdpEvent',
data: message,
};
// send response/instrumentation event back
port?.postMessage(cdpEvent);
const parsedEvent = JSON.parse(message);
if (parsedEvent.id) {
// event is a response if contains `id` property which corresponds to a command
const cmdIdx = this.commands.findIndex(
commandObj => commandObj.command.id === parsedEvent.id
);
cmdIdx !== -1 ? (this.commands[cmdIdx].response = parsedEvent) : null;
} else {
this.events.push(parsedEvent);
}
};
this.transport.onclose = () => {
DebuggerHandler.isExecuting = false;
this._unregisterListeners();
const executionEvent: ExecutionEvent = {
type: 'executionStopped',
tabId: this.tabId,
};
port?.postMessage(executionEvent);
console.log('CDP LOGS');
console.log({
commands: this.commands,
events: this.events,
});
};
this._incomingMessageHandler = this._incomingMessageHandler.bind(this);
port?.onMessage.addListener(this._incomingMessageHandler);
const executionEvent: ExecutionEvent = {
type: 'executionStarted',
tabId: this.tabId,
};
port?.postMessage(executionEvent);
}
private _unregisterListeners() {
this.transport.onmessage = undefined;
this.transport.onclose = undefined;
connections[this.tabId]?.onMessage.removeListener(
this._incomingMessageHandler
);
}
private _incomingMessageHandler(message: Message) {
if (message.type === 'cdpCommand') {
// pass command to chrome.debugger
this.transport.send(message.command);
this.commands.push({
command: JSON.parse(message.command),
response: {},
});
} else if (message.type === 'stopExecution') {
this.transport.close();
}
}
}