Skip to content
This repository was archived by the owner on Sep 17, 2021. It is now read-only.

Commit 8899d33

Browse files
authored
Merge pull request #12 from CodeMooseUS/addAttach
Adding an attach feature to the launch.json per Issue #10.
2 parents f8a5027 + 5360feb commit 8899d33

File tree

6 files changed

+104
-40
lines changed

6 files changed

+104
-40
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ You can launch the Chrome DevTools hosted in VS Code like you would a debugger,
2222
To do this in your `launch.json` add a new debug config with two parameters.
2323
- `type` - The name of the debugger which must be `devtools-for-chrome`. Required.
2424
- `url` - The url to launch Chrome at. Optional.
25-
- `request` - Whether a new tab in Chrome should be opened `launch` or to use an exsisting tab `attach` matched by URI. Optional.
25+
- `file` - The local file path to launch Chrome at. Optional.
26+
- `request` - Whether a new tab in Chrome should be opened `launch` or to use an exsisting tab `attach` matched on URL. Optional.
2627
- `name` - A friendly name to show in the VS Code UI. Required.
2728
```
2829
{
@@ -31,8 +32,14 @@ To do this in your `launch.json` add a new debug config with two parameters.
3132
{
3233
"type": "devtools-for-chrome",
3334
"request": "launch",
34-
"name": "Launch Chrome",
35-
"url": "${workspaceFolder}/index.html"
35+
"name": "Launch Chrome DevTools",
36+
"file": "${workspaceFolder}/index.html"
37+
},
38+
{
39+
"type": "devtools-for-chrome",
40+
"request": "attach",
41+
"name": "Attach Chrome DevTools",
42+
"url": "http://localhost:8000/"
3643
}
3744
]
3845
}

package-lock.json

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,35 @@
5959
"label": "DevTools for Chrome",
6060
"configurationAttributes": {
6161
"launch": {
62-
"required": [
63-
"url"
64-
],
6562
"properties": {
6663
"url": {
6764
"type": "string",
6865
"description": "Absolute uri to launch.",
6966
"default": "http://localhost:8080"
7067
},
71-
"request": {
68+
"file": {
7269
"type": "string",
73-
"description": "A value representing whether the debugger should launch a new Chrome tab or attach to an exsisting one.",
74-
"default": "launch"
70+
"description": "File path to launch.",
71+
"default": "${workspaceFolder}/index.html"
72+
},
73+
"chromePath": {
74+
"type": "string",
75+
"description": "Absolute path to the Chrome instance to launch.",
76+
"default": ""
77+
}
78+
}
79+
},
80+
"attach": {
81+
"properties": {
82+
"url": {
83+
"type": "string",
84+
"description": "Absolute uri to launch.",
85+
"default": "http://localhost:8080"
86+
},
87+
"file": {
88+
"type": "string",
89+
"description": "File path to launch.",
90+
"default": "${workspaceFolder}/index.html"
7591
},
7692
"chromePath": {
7793
"type": "string",
@@ -126,6 +142,7 @@
126142
"@types/node": "^10.5.2",
127143
"@types/shelljs": "^0.8.0",
128144
"@types/ws": "^6.0.1",
145+
"@types/applicationinsights": "^0.20.0",
129146
"shelljs": "^0.8.2",
130147
"ts-node": "^7.0.1",
131148
"tslint": "^5.11.0",

src/extension.ts

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface IPackageInfo {
1212
}
1313

1414
const debuggerType: string = 'devtools-for-chrome';
15+
const defaultUrl: string = 'about:blank';
1516
let telemetryReporter: TelemetryReporter;
1617

1718
export function activate(context: vscode.ExtensionContext) {
@@ -31,7 +32,7 @@ export function activate(context: vscode.ExtensionContext) {
3132
}));
3233

3334
context.subscriptions.push(vscode.commands.registerCommand('devtools-for-chrome.attach', async () => {
34-
attach(context, /* viaConfig= */ false);
35+
attach(context, /* viaConfig= */ false, defaultUrl);
3536
}));
3637

3738
vscode.debug.registerDebugConfigurationProvider(debuggerType, {
@@ -45,22 +46,18 @@ export function activate(context: vscode.ExtensionContext) {
4546
},
4647

4748
resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration> {
48-
if (config && config.type == debuggerType) {
49-
if (config.request && config.request.localeCompare('attach', 'en', { sensitivity: 'base' }) == 0) {
50-
attach(context, /* viaConfig= */ true);
51-
} else {
52-
let launchUri: string = '';
53-
if (folder.uri.scheme == 'file') {
54-
const baseUrl: string = config.file || config.url;
55-
const replacedUri: string = baseUrl.replace('${workspaceFolder}', folder.uri.path);
56-
launchUri = utils.pathToFileURL(replacedUri);
57-
} else {
58-
launchUri = config.url;
59-
}
60-
launch(context, launchUri, config.chromePath);
49+
if (config && config.type === debuggerType) {
50+
const targetUri: string = utils.getUrlFromConfig(folder, config);
51+
if (config.request && config.request.localeCompare('attach', 'en', { sensitivity: 'base' }) === 0) {
52+
attach(context, /* viaConfig= */ true, targetUri);
53+
telemetryReporter.sendTelemetryEvent('launch/command/attach');
54+
} else if (config.request && config.request.localeCompare('launch', 'en', { sensitivity: 'base' }) === 0) {
55+
launch(context, targetUri, config.chromePath);
56+
telemetryReporter.sendTelemetryEvent('launch/command/launch');
6157
}
6258
} else {
6359
vscode.window.showErrorMessage('No supported launch config was found.');
60+
telemetryReporter.sendTelemetryEvent('launch/error/config_not_found');
6461
}
6562
return;
6663
}
@@ -84,21 +81,21 @@ async function launch(context: vscode.ExtensionContext, launchUrl?: string, chro
8481
return;
8582
}
8683

87-
utils.launchLocalChrome(pathToChrome, port, 'about:blank');
84+
utils.launchLocalChrome(pathToChrome, port, defaultUrl);
8885
}
8986

9087
const target = JSON.parse(await utils.getURL(`http://${hostname}:${port}/json/new?${launchUrl}`));
9188

92-
if (!target || !target.webSocketDebuggerUrl || target.webSocketDebuggerUrl == '') {
89+
if (!target || !target.webSocketDebuggerUrl || target.webSocketDebuggerUrl === '') {
9390
vscode.window.showErrorMessage(`Could not find the launched Chrome tab: (${launchUrl}).`);
9491
telemetryReporter.sendTelemetryEvent('launch/error/tab_not_found', telemetryProps);
95-
attach(context, viaConfig);
92+
attach(context, viaConfig, defaultUrl);
9693
} else {
9794
DevToolsPanel.createOrShow(context, target.webSocketDebuggerUrl);
9895
}
9996
}
10097

101-
async function attach(context: vscode.ExtensionContext, viaConfig: boolean) {
98+
async function attach(context: vscode.ExtensionContext, viaConfig: boolean, targetUrl: string) {
10299
const telemetryProps = { viaConfig: `${viaConfig}` };
103100
telemetryReporter.sendTelemetryEvent('attach', telemetryProps);
104101

@@ -111,14 +108,32 @@ async function attach(context: vscode.ExtensionContext, viaConfig: boolean) {
111108

112109
responseArray.forEach(i => {
113110
i = utils.fixRemoteUrl(hostname, port, i);
114-
items.push({ label: i.title, description: i.url, detail: i.webSocketDebuggerUrl });
111+
items.push({
112+
label: i.title,
113+
description: i.url,
114+
detail: i.webSocketDebuggerUrl
115+
});
115116
});
116117

117-
vscode.window.showQuickPick(items).then((selection) => {
118-
if (selection) {
119-
DevToolsPanel.createOrShow(context, selection.detail as string);
118+
let targetWebsocketUrl = '';
119+
if (typeof targetUrl === 'string' && targetUrl.length > 0 && targetUrl !== defaultUrl) {
120+
const matches = items.filter(i => targetUrl.localeCompare(i.description, 'en', { sensitivity: 'base' }) === 0);
121+
if (matches && matches.length > 0 ) {
122+
targetWebsocketUrl = matches[0].detail;
123+
} else {
124+
vscode.window.showErrorMessage(`Couldn't attach to ${targetUrl}.`);
120125
}
121-
});
126+
}
127+
128+
if (targetWebsocketUrl && targetWebsocketUrl.length > 0) {
129+
DevToolsPanel.createOrShow(context, targetWebsocketUrl as string);
130+
} else {
131+
vscode.window.showQuickPick(items).then((selection) => {
132+
if (selection) {
133+
DevToolsPanel.createOrShow(context, selection.detail as string);
134+
}
135+
});
136+
}
122137
} else {
123138
telemetryReporter.sendTelemetryEvent('attach/error/no_json_array', telemetryProps);
124139
}
@@ -331,10 +346,10 @@ class DevToolsPanel {
331346
private _getDevtoolsState() {
332347
const allPrefsKey = 'devtools-preferences';
333348
const allPrefs: any = this._context.workspaceState.get(allPrefsKey) ||
334-
{
335-
uiTheme: '"dark"',
336-
screencastEnabled: false
337-
};
349+
{
350+
uiTheme: '"dark"',
351+
screencastEnabled: false
352+
};
338353
this._panel.webview.postMessage(`preferences:${JSON.stringify(allPrefs)}`);
339354
}
340355

@@ -359,7 +374,7 @@ class DevToolsPanel {
359374
content = '';
360375
}
361376

362-
this._panel.webview.postMessage(`setUrl:${JSON.stringify({id: request.id, content})}`);
377+
this._panel.webview.postMessage(`setUrl:${JSON.stringify({ id: request.id, content })}`);
363378
}
364379

365380
private _update() {

src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as net from 'net';
66
import * as os from 'os';
77
import * as path from 'path';
88
import * as url from 'url';
9+
import * as vscode from 'vscode';
910

1011
export function getURL(aUrl: string, options: https.RequestOptions = {}): Promise<string> {
1112
return new Promise((resolve, reject) => {
@@ -140,4 +141,18 @@ export function forceForwardSlashes(aUrl: string): string {
140141
return aUrl
141142
.replace(/\\\//g, '/') // Replace \/ (unnecessarily escaped forward slash)
142143
.replace(/\\/g, '/');
144+
}
145+
146+
export function getUrlFromConfig(folder: vscode.WorkspaceFolder, config: vscode.DebugConfiguration): string {
147+
let outUrlString = '';
148+
149+
if (config.file) {
150+
outUrlString = config.file;
151+
outUrlString = outUrlString.replace('${workspaceFolder}', folder.uri.path);
152+
outUrlString = pathToFileURL(outUrlString);
153+
} else if (config.url) {
154+
outUrlString = config.url;
155+
}
156+
157+
return outUrlString;
143158
}

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"no-internal-module": true,
6363
"no-trailing-whitespace": true,
6464
"no-null-keyword": true,
65-
"prefer-const": true
65+
"prefer-const": true,
66+
"triple-equals": true
6667
}
6768
}

0 commit comments

Comments
 (0)