forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnativeBrowserElementsMainService.ts
More file actions
500 lines (438 loc) · 17.2 KB
/
nativeBrowserElementsMainService.ts
File metadata and controls
500 lines (438 loc) · 17.2 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IElementData, INativeBrowserElementsService, IBrowserTargetLocator } from '../common/browserElements.js';
import { CancellationToken } from '../../../base/common/cancellation.js';
import { IRectangle } from '../../window/common/window.js';
import { BrowserWindow, webContents } from 'electron';
import { IAuxiliaryWindow } from '../../auxiliaryWindow/electron-main/auxiliaryWindow.js';
import { ICodeWindow } from '../../window/electron-main/window.js';
import { IAuxiliaryWindowsMainService } from '../../auxiliaryWindow/electron-main/auxiliaryWindows.js';
import { IWindowsMainService } from '../../windows/electron-main/windows.js';
import { createDecorator } from '../../instantiation/common/instantiation.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { AddFirstParameterToFunctions } from '../../../base/common/types.js';
import { IBrowserViewMainService } from '../../browserView/electron-main/browserViewMainService.js';
export const INativeBrowserElementsMainService = createDecorator<INativeBrowserElementsMainService>('browserElementsMainService');
export interface INativeBrowserElementsMainService extends AddFirstParameterToFunctions<INativeBrowserElementsService, Promise<unknown> /* only methods, not events */, number | undefined /* window ID */> { }
interface NodeDataResponse {
outerHTML: string;
computedStyle: string;
bounds: IRectangle;
}
export class NativeBrowserElementsMainService extends Disposable implements INativeBrowserElementsMainService {
_serviceBrand: undefined;
constructor(
@IWindowsMainService private readonly windowsMainService: IWindowsMainService,
@IAuxiliaryWindowsMainService private readonly auxiliaryWindowsMainService: IAuxiliaryWindowsMainService,
@IBrowserViewMainService private readonly browserViewMainService: IBrowserViewMainService
) {
super();
}
get windowId(): never { throw new Error('Not implemented in electron-main'); }
/**
* Find the webview target that matches the given locator.
* Checks either webviewId or browserViewId depending on what's provided.
*/
async findWebviewTarget(debuggers: Electron.Debugger, locator: IBrowserTargetLocator): Promise<string | undefined> {
const { targetInfos } = await debuggers.sendCommand('Target.getTargets');
if (locator.webviewId) {
let extensionId = '';
for (const targetInfo of targetInfos) {
try {
const url = new URL(targetInfo.url);
if (url.searchParams.get('id') === locator.webviewId) {
extensionId = url.searchParams.get('extensionId') || '';
break;
}
} catch (err) {
// ignore
}
}
if (!extensionId) {
return undefined;
}
// search for webview via search parameters
const target = targetInfos.find((targetInfo: { url: string }) => {
try {
const url = new URL(targetInfo.url);
const isLiveServer = extensionId === 'ms-vscode.live-server' && url.searchParams.get('serverWindowId') === locator.webviewId;
const isSimpleBrowser = extensionId === 'vscode.simple-browser' && url.searchParams.get('id') === locator.webviewId && url.searchParams.has('vscodeBrowserReqId');
if (isLiveServer || isSimpleBrowser) {
return true;
}
return false;
} catch (e) {
return false;
}
});
return target?.targetId;
}
if (locator.browserViewId) {
const webContentsInstance = this.browserViewMainService.tryGetBrowserView(locator.browserViewId)?.webContents;
const target = targetInfos.find((targetInfo: { targetId: string; type: string }) => {
if (targetInfo.type !== 'page') {
return false;
}
return webContents.fromDevToolsTargetId(targetInfo.targetId) === webContentsInstance;
});
return target?.targetId;
}
return undefined;
}
async waitForWebviewTargets(debuggers: Electron.Debugger, locator: IBrowserTargetLocator): Promise<string | undefined> {
const start = Date.now();
const timeout = 10000;
while (Date.now() - start < timeout) {
const targetId = await this.findWebviewTarget(debuggers, locator);
if (targetId) {
return targetId;
}
// Wait for a short period before checking again
await new Promise(resolve => setTimeout(resolve, 500));
}
debuggers.detach();
return undefined;
}
async startDebugSession(windowId: number | undefined, token: CancellationToken, locator: IBrowserTargetLocator, cancelAndDetachId?: number): Promise<void> {
const window = this.windowById(windowId);
if (!window?.win) {
return undefined;
}
// Find the simple browser webview
const allWebContents = webContents.getAllWebContents();
const simpleBrowserWebview = allWebContents.find(webContent => webContent.id === window.id);
if (!simpleBrowserWebview) {
return undefined;
}
const debuggers = simpleBrowserWebview.debugger;
if (!debuggers.isAttached()) {
debuggers.attach();
}
try {
const matchingTargetId = await this.waitForWebviewTargets(debuggers, locator);
if (!matchingTargetId) {
if (debuggers.isAttached()) {
debuggers.detach();
}
throw new Error('No target found');
}
} catch (e) {
if (debuggers.isAttached()) {
debuggers.detach();
}
throw new Error('No target found');
}
if (token.isCancellationRequested) {
return;
}
window.win.webContents.on('ipc-message', async (event, channel, closedCancelAndDetachId) => {
if (channel === `vscode:cancelCurrentSession${cancelAndDetachId}`) {
if (cancelAndDetachId !== closedCancelAndDetachId) {
return;
}
if (debuggers.isAttached()) {
debuggers.detach();
}
if (window.win) {
window.win.webContents.removeAllListeners('ipc-message');
}
}
});
}
async finishOverlay(debuggers: Electron.Debugger, sessionId: string | undefined): Promise<void> {
if (debuggers.isAttached() && sessionId) {
await debuggers.sendCommand('Overlay.setInspectMode', {
mode: 'none',
highlightConfig: {
showInfo: false,
showStyles: false
}
}, sessionId);
await debuggers.sendCommand('Overlay.hideHighlight', {}, sessionId);
await debuggers.sendCommand('Overlay.disable', {}, sessionId);
debuggers.detach();
}
}
async getElementData(windowId: number | undefined, rect: IRectangle, token: CancellationToken, locator: IBrowserTargetLocator, cancellationId?: number): Promise<IElementData | undefined> {
const window = this.windowById(windowId);
if (!window?.win) {
return undefined;
}
// Find the simple browser webview
const allWebContents = webContents.getAllWebContents();
const simpleBrowserWebview = allWebContents.find(webContent => webContent.id === window.id);
if (!simpleBrowserWebview) {
return undefined;
}
const debuggers = simpleBrowserWebview.debugger;
if (!debuggers.isAttached()) {
debuggers.attach();
}
let targetSessionId: string | undefined = undefined;
try {
const targetId = await this.findWebviewTarget(debuggers, locator);
const { sessionId } = await debuggers.sendCommand('Target.attachToTarget', {
targetId: targetId,
flatten: true,
});
targetSessionId = sessionId;
await debuggers.sendCommand('DOM.enable', {}, sessionId);
await debuggers.sendCommand('CSS.enable', {}, sessionId);
await debuggers.sendCommand('Overlay.enable', {}, sessionId);
await debuggers.sendCommand('Debugger.enable', {}, sessionId);
await debuggers.sendCommand('Runtime.enable', {}, sessionId);
await debuggers.sendCommand('Runtime.evaluate', {
expression: `(function() {
const style = document.createElement('style');
style.id = '__pseudoBlocker__';
style.textContent = '*::before, *::after { pointer-events: none !important; }';
document.head.appendChild(style);
})();`,
}, sessionId);
// slightly changed default CDP debugger inspect colors
await debuggers.sendCommand('Overlay.setInspectMode', {
mode: 'searchForNode',
highlightConfig: {
showInfo: true,
showRulers: false,
showStyles: true,
showAccessibilityInfo: true,
showExtensionLines: false,
contrastAlgorithm: 'aa',
contentColor: { r: 173, g: 216, b: 255, a: 0.8 },
paddingColor: { r: 150, g: 200, b: 255, a: 0.5 },
borderColor: { r: 120, g: 180, b: 255, a: 0.7 },
marginColor: { r: 200, g: 220, b: 255, a: 0.4 },
eventTargetColor: { r: 130, g: 160, b: 255, a: 0.8 },
shapeColor: { r: 130, g: 160, b: 255, a: 0.8 },
shapeMarginColor: { r: 130, g: 160, b: 255, a: 0.5 },
gridHighlightConfig: {
rowGapColor: { r: 140, g: 190, b: 255, a: 0.3 },
rowHatchColor: { r: 140, g: 190, b: 255, a: 0.7 },
columnGapColor: { r: 140, g: 190, b: 255, a: 0.3 },
columnHatchColor: { r: 140, g: 190, b: 255, a: 0.7 },
rowLineColor: { r: 120, g: 180, b: 255 },
columnLineColor: { r: 120, g: 180, b: 255 },
rowLineDash: true,
columnLineDash: true
},
flexContainerHighlightConfig: {
containerBorder: {
color: { r: 120, g: 180, b: 255 },
pattern: 'solid'
},
itemSeparator: {
color: { r: 140, g: 190, b: 255 },
pattern: 'solid'
},
lineSeparator: {
color: { r: 140, g: 190, b: 255 },
pattern: 'solid'
},
mainDistributedSpace: {
hatchColor: { r: 140, g: 190, b: 255, a: 0.7 },
fillColor: { r: 140, g: 190, b: 255, a: 0.4 }
},
crossDistributedSpace: {
hatchColor: { r: 140, g: 190, b: 255, a: 0.7 },
fillColor: { r: 140, g: 190, b: 255, a: 0.4 }
},
rowGapSpace: {
hatchColor: { r: 140, g: 190, b: 255, a: 0.7 },
fillColor: { r: 140, g: 190, b: 255, a: 0.4 }
},
columnGapSpace: {
hatchColor: { r: 140, g: 190, b: 255, a: 0.7 },
fillColor: { r: 140, g: 190, b: 255, a: 0.4 }
}
},
flexItemHighlightConfig: {
baseSizeBox: {
hatchColor: { r: 130, g: 170, b: 255, a: 0.6 }
},
baseSizeBorder: {
color: { r: 120, g: 180, b: 255 },
pattern: 'solid'
},
flexibilityArrow: {
color: { r: 130, g: 190, b: 255 }
}
},
},
}, sessionId);
} catch (e) {
debuggers.detach();
throw new Error('No target found', e);
}
if (!targetSessionId) {
debuggers.detach();
throw new Error('No target session id found');
}
const nodeData = await this.getNodeData(targetSessionId, debuggers, window.win, cancellationId);
await this.finishOverlay(debuggers, targetSessionId);
const zoomFactor = simpleBrowserWebview.getZoomFactor();
const absoluteBounds = {
x: rect.x + nodeData.bounds.x,
y: rect.y + nodeData.bounds.y,
width: nodeData.bounds.width,
height: nodeData.bounds.height
};
const clippedBounds = {
x: Math.max(absoluteBounds.x, rect.x),
y: Math.max(absoluteBounds.y, rect.y),
width: Math.max(0, Math.min(absoluteBounds.x + absoluteBounds.width, rect.x + rect.width) - Math.max(absoluteBounds.x, rect.x)),
height: Math.max(0, Math.min(absoluteBounds.y + absoluteBounds.height, rect.y + rect.height) - Math.max(absoluteBounds.y, rect.y))
};
const scaledBounds = {
x: clippedBounds.x * zoomFactor,
y: clippedBounds.y * zoomFactor,
width: clippedBounds.width * zoomFactor,
height: clippedBounds.height * zoomFactor
};
return { outerHTML: nodeData.outerHTML, computedStyle: nodeData.computedStyle, bounds: scaledBounds };
}
async getNodeData(sessionId: string, debuggers: Electron.Debugger, window: BrowserWindow, cancellationId?: number): Promise<NodeDataResponse> {
return new Promise((resolve, reject) => {
const onMessage = async (event: Electron.Event, method: string, params: { backendNodeId: number }) => {
if (method === 'Overlay.inspectNodeRequested') {
debuggers.off('message', onMessage);
await debuggers.sendCommand('Runtime.evaluate', {
expression: `(() => {
const style = document.getElementById('__pseudoBlocker__');
if (style) style.remove();
})();`,
}, sessionId);
const backendNodeId = params?.backendNodeId;
if (!backendNodeId) {
throw new Error('Missing backendNodeId in inspectNodeRequested event');
}
try {
await debuggers.sendCommand('DOM.getDocument', {}, sessionId);
const { nodeIds } = await debuggers.sendCommand('DOM.pushNodesByBackendIdsToFrontend', { backendNodeIds: [backendNodeId] }, sessionId);
if (!nodeIds || nodeIds.length === 0) {
throw new Error('Failed to get node IDs.');
}
const nodeId = nodeIds[0];
const { model } = await debuggers.sendCommand('DOM.getBoxModel', { nodeId }, sessionId);
if (!model) {
throw new Error('Failed to get box model.');
}
const content = model.content;
const margin = model.margin;
const x = Math.min(margin[0], content[0]);
const y = Math.min(margin[1], content[1]);
const width = Math.max(margin[2] - margin[0], content[2] - content[0]);
const height = Math.max(margin[5] - margin[1], content[5] - content[1]);
const matched = await debuggers.sendCommand('CSS.getMatchedStylesForNode', { nodeId }, sessionId);
if (!matched) {
throw new Error('Failed to get matched css.');
}
const formatted = this.formatMatchedStyles(matched);
const { outerHTML } = await debuggers.sendCommand('DOM.getOuterHTML', { nodeId }, sessionId);
if (!outerHTML) {
throw new Error('Failed to get outerHTML.');
}
resolve({
outerHTML,
computedStyle: formatted,
bounds: { x, y, width, height }
});
} catch (err) {
debuggers.off('message', onMessage);
debuggers.detach();
reject(err);
}
}
};
window.webContents.on('ipc-message', async (event, channel, closedCancellationId) => {
if (channel === `vscode:cancelElementSelection${cancellationId}`) {
if (cancellationId !== closedCancellationId) {
return;
}
debuggers.off('message', onMessage);
await this.finishOverlay(debuggers, sessionId);
window.webContents.removeAllListeners('ipc-message');
}
});
debuggers.on('message', onMessage);
});
}
formatMatchedStyles(matched: { inlineStyle?: { cssProperties?: Array<{ name: string; value: string }> }; matchedCSSRules?: Array<{ rule: { selectorList: { selectors: Array<{ text: string }> }; origin: string; style: { cssProperties: Array<{ name: string; value: string }> } } }>; inherited?: Array<{ inlineStyle?: { cssText: string }; matchedCSSRules?: Array<{ rule: { selectorList: { selectors: Array<{ text: string }> }; origin: string; style: { cssProperties: Array<{ name: string; value: string }> } } }> }> }): string {
const lines: string[] = [];
// inline
if (matched.inlineStyle?.cssProperties?.length) {
lines.push('/* Inline style */');
lines.push('element {');
for (const prop of matched.inlineStyle.cssProperties) {
if (prop.name && prop.value) {
lines.push(` ${prop.name}: ${prop.value};`);
}
}
lines.push('}\n');
}
// matched
if (matched.matchedCSSRules?.length) {
for (const ruleEntry of matched.matchedCSSRules) {
const rule = ruleEntry.rule;
const selectors = rule.selectorList.selectors.map(s => s.text).join(', ');
lines.push(`/* Matched Rule from ${rule.origin} */`);
lines.push(`${selectors} {`);
for (const prop of rule.style.cssProperties) {
if (prop.name && prop.value) {
lines.push(` ${prop.name}: ${prop.value};`);
}
}
lines.push('}\n');
}
}
// inherited rules
if (matched.inherited?.length) {
let level = 1;
for (const inherited of matched.inherited) {
const inline = inherited.inlineStyle;
if (inline) {
lines.push(`/* Inherited from ancestor level ${level} (inline) */`);
lines.push('element {');
lines.push(inline.cssText);
lines.push('}\n');
}
const rules = inherited.matchedCSSRules || [];
for (const ruleEntry of rules) {
const rule = ruleEntry.rule;
const selectors = rule.selectorList.selectors.map(s => s.text).join(', ');
lines.push(`/* Inherited from ancestor level ${level} (${rule.origin}) */`);
lines.push(`${selectors} {`);
for (const prop of rule.style.cssProperties) {
if (prop.name && prop.value) {
lines.push(` ${prop.name}: ${prop.value};`);
}
}
lines.push('}\n');
}
level++;
}
}
return '\n' + lines.join('\n');
}
private windowById(windowId: number | undefined, fallbackCodeWindowId?: number): ICodeWindow | IAuxiliaryWindow | undefined {
return this.codeWindowById(windowId) ?? this.auxiliaryWindowById(windowId) ?? this.codeWindowById(fallbackCodeWindowId);
}
private codeWindowById(windowId: number | undefined): ICodeWindow | undefined {
if (typeof windowId !== 'number') {
return undefined;
}
return this.windowsMainService.getWindowById(windowId);
}
private auxiliaryWindowById(windowId: number | undefined): IAuxiliaryWindow | undefined {
if (typeof windowId !== 'number') {
return undefined;
}
const contents = webContents.fromId(windowId);
if (!contents) {
return undefined;
}
return this.auxiliaryWindowsMainService.getWindowByWebContents(contents);
}
}