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

Commit d4484fd

Browse files
committed
Work in progress.
1 parent 7029658 commit d4484fd

File tree

4 files changed

+93
-13
lines changed

4 files changed

+93
-13
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"activationEvents": [
3636
"onCommand:devtools-for-chrome.launch",
3737
"onCommand:devtools-for-chrome.attach",
38-
"onWebviewPanel:devtools-for-chrome"
38+
"onWebviewPanel:devtools-for-chrome",
39+
"onDebug"
3940
],
4041
"main": "./out/extension",
4142
"contributes": {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as vscode from 'vscode';
2+
import * as utils from './utils';
3+
4+
export class ChromeDevToolsConfigurationProvider implements vscode.DebugConfigurationProvider {
5+
public TargetUri:string;
6+
7+
public constructor() {
8+
9+
}
10+
11+
provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration[]> {
12+
return;
13+
}
14+
15+
resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<vscode.DebugConfiguration> {
16+
this.TargetUri = '';
17+
if(config && config.type == 'chrome'){
18+
if(folder.uri.scheme == 'file'){
19+
const baseUrl:string = (config.file)? config.file: config.url;
20+
const replacedUri:string = baseUrl.replace('${workspaceFolder}', folder.uri.path);
21+
this.TargetUri = utils.pathToFileURL(replacedUri);
22+
} else {
23+
this.TargetUri = config.url;
24+
}
25+
}
26+
return;
27+
}
28+
}

src/extension.ts

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
import * as path from 'path';
22
import * as vscode from 'vscode';
3+
import {ChromeDevToolsConfigurationProvider} from './ChromeDevToolsConfigurationProvider'
34
import WebSocket from 'ws';
45
import QuickPickItem = vscode.QuickPickItem;
5-
import QuickPickOptions = vscode.QuickPickOptions;
66
import * as utils from './utils';
77

88
const settings = vscode.workspace.getConfiguration('vscode-devtools-for-chrome');
99
const hostname = settings.get('hostname') as string || 'localhost';
1010
const port = settings.get('port') as number || 9222;
11+
const debugConfigProvider:ChromeDevToolsConfigurationProvider = new ChromeDevToolsConfigurationProvider();
1112

1213
export function activate(context: vscode.ExtensionContext) {
14+
context.subscriptions.push(vscode.commands.registerCommand('devtools-for-chrome.launch', async () => {
15+
launch(context);
16+
}));
17+
1318
context.subscriptions.push(vscode.commands.registerCommand('devtools-for-chrome.attach', async () => {
1419
attach(context);
1520
}));
1621

17-
context.subscriptions.push(vscode.commands.registerCommand('devtools-for-chrome.launch', async () => {
18-
launch(context);
19-
}));
22+
vscode.debug.onDidStartDebugSession( async (e: vscode.DebugSession) => {
23+
debugSessionStart(e, context);
24+
});
25+
26+
vscode.debug.registerDebugConfigurationProvider('chrome', debugConfigProvider);
2027
}
2128

2229
async function launch(context: vscode.ExtensionContext) {
@@ -34,15 +41,8 @@ async function launch(context: vscode.ExtensionContext) {
3441
}
3542

3643
async function attach(context: vscode.ExtensionContext) {
44+
const responseArray = getListOfTargets();
3745

38-
const checkDiscoveryEndpoint = (url: string) => {
39-
return utils.getURL(url, { headers: { Host: 'localhost' } });
40-
};
41-
42-
const jsonResponse = await checkDiscoveryEndpoint(`http://${hostname}:${port}/json/list`)
43-
.catch(() => checkDiscoveryEndpoint(`http://${hostname}:${port}/json`));
44-
45-
const responseArray = JSON.parse(jsonResponse);
4646
if (Array.isArray(responseArray)) {
4747
const items: QuickPickItem[] = [];
4848

@@ -59,6 +59,40 @@ async function attach(context: vscode.ExtensionContext) {
5959
}
6060
}
6161

62+
async function debugSessionStart(e: vscode.DebugSession, context: vscode.ExtensionContext) {
63+
if(e.type == 'chrome') {
64+
debugger;
65+
console.log(arguments);
66+
let targetUrl = debugConfigProvider.TargetUri;
67+
let webSocketUri:string = await getWebSocketUri(targetUrl);
68+
if(!webSocketUri || webSocketUri == ''){
69+
vscode.window.showErrorMessage(`Could not find the launched Chrome tab: (${targetUrl}).`);
70+
} else {
71+
DevToolsPanel.createOrShow(context.extensionPath, webSocketUri);
72+
}
73+
}
74+
}
75+
76+
async function getWebSocketUri(targetUrl: string): Promise<string> {
77+
const responseArray = await getListOfTargets();
78+
79+
// Always return the first match which is the logic in the Chrome debug extension too
80+
const match = responseArray.find(i => i.url.indexOf(targetUrl) !== -1);
81+
82+
return (match && match.webSocketUri)? match.webSocketUri: '';
83+
}
84+
85+
async function getListOfTargets(): Promise<Array<any>> {
86+
const checkDiscoveryEndpoint = (url: string) => {
87+
return utils.getURL(url, { headers: { Host: 'localhost' } });
88+
};
89+
90+
const jsonResponse = await checkDiscoveryEndpoint(`http://${hostname}:${port}/json/list`)
91+
.catch(() => checkDiscoveryEndpoint(`http://${hostname}:${port}/json`));
92+
93+
return JSON.parse(jsonResponse);
94+
}
95+
6296
class DevToolsPanel {
6397
private static currentPanel: DevToolsPanel;
6498
private readonly _panel: vscode.WebviewPanel;

src/utils.ts

Lines changed: 17 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 { Uri } from 'vscode';
910

1011
export function getURL(aUrl: string, options: https.RequestOptions = {}): Promise<string> {
1112
return new Promise((resolve, reject) => {
@@ -121,4 +122,20 @@ export function getPathToChrome(): string {
121122
} else {
122123
return existsSync(DEFAULT_CHROME_PATH.LINUX) ? DEFAULT_CHROME_PATH.LINUX : '';
123124
}
125+
}
126+
127+
export function pathToFileURL(absPath: string, normalize?: boolean): string {
128+
if (normalize) {
129+
absPath = path.normalize(absPath);
130+
absPath = forceForwardSlashes(absPath);
131+
}
132+
133+
absPath = (absPath.startsWith('/') ? 'file://' : 'file:///') + absPath;
134+
return encodeURI(absPath);
135+
}
136+
137+
export function forceForwardSlashes(aUrl: string): string {
138+
return aUrl
139+
.replace(/\\\//g, '/') // Replace \/ (unnecessarily escaped forward slash)
140+
.replace(/\\/g, '/');
124141
}

0 commit comments

Comments
 (0)