11import * as path from 'path' ;
22import * as vscode from 'vscode' ;
3- import { ChromeDevToolsConfigurationProvider } from './ChromeDevToolsConfigurationProvider'
43import WebSocket from 'ws' ;
54import QuickPickItem = vscode . QuickPickItem ;
65import * as utils from './utils' ;
6+ import { isRegExp } from 'util' ;
77
88const settings = vscode . workspace . getConfiguration ( 'vscode-devtools-for-chrome' ) ;
99const hostname = settings . get ( 'hostname' ) as string || 'localhost' ;
1010const port = settings . get ( 'port' ) as number || 9222 ;
11- const debugConfigProvider :ChromeDevToolsConfigurationProvider = new ChromeDevToolsConfigurationProvider ( ) ;
11+ const debuggerType = 'devtools-for-chrome' ;
12+ const DEFAULT_CONFIG = {
13+ type : debuggerType ,
14+ name : 'Launch Chrome against localhost' ,
15+ url : 'http://localhost:8080' ,
16+ } ;
1217
1318export function activate ( context : vscode . ExtensionContext ) {
1419 context . subscriptions . push ( vscode . commands . registerCommand ( 'devtools-for-chrome.launch' , async ( ) => {
@@ -20,28 +25,58 @@ export function activate(context: vscode.ExtensionContext) {
2025 } ) ) ;
2126
2227 vscode . debug . onDidStartDebugSession ( async ( e : vscode . DebugSession ) => {
23- debugSessionStart ( e , context ) ;
28+ // debugSessionStart(e, context);
2429 } ) ;
2530
26- vscode . debug . registerDebugConfigurationProvider ( 'chrome' , debugConfigProvider ) ;
31+ vscode . debug . registerDebugConfigurationProvider ( debuggerType , {
32+ provideDebugConfigurations ( folder : vscode . WorkspaceFolder | undefined , token ?: vscode . CancellationToken ) : vscode . ProviderResult < vscode . DebugConfiguration [ ] > {
33+ return ;
34+ } ,
35+
36+ resolveDebugConfiguration ( folder : vscode . WorkspaceFolder | undefined , config : vscode . DebugConfiguration , token ?: vscode . CancellationToken ) : vscode . ProviderResult < vscode . DebugConfiguration > {
37+ if ( config && config . type == debuggerType ) {
38+ let launchUri :string = '' ;
39+ if ( folder . uri . scheme == 'file' ) {
40+ const baseUrl :string = config . file || config . url ;
41+ const replacedUri :string = baseUrl . replace ( '${workspaceFolder}' , folder . uri . path ) ;
42+ launchUri = utils . pathToFileURL ( replacedUri ) ;
43+ } else {
44+ launchUri = config . url ;
45+ }
46+ launch ( context , launchUri , config . chromePath ) ;
47+ }
48+ return ;
49+ }
50+ } ) ;
2751}
2852
29- async function launch ( context : vscode . ExtensionContext ) {
53+ async function launch ( context : vscode . ExtensionContext , launchUrl ?: string , chromePathFromLaunchConfig ?: string ) {
3054 const portFree = await utils . isPortFree ( hostname , port ) ;
55+
3156 if ( portFree ) {
32- const pathToChrome = settings . get ( 'chromePath' ) as string || utils . getPathToChrome ( ) ;
33- if ( pathToChrome || utils . existsSync ( pathToChrome ) ) {
34- vscode . window . showErrorMessage ( 'Chrome was not found. Chrome must be installed for this extension to function. If you have Chrome installed at a custom location you can speficy it in the \'chromePath\' setting.' ) ;
57+ const pathToChrome = settings . get ( 'chromePath' ) as string || chromePathFromLaunchConfig || utils . getPathToChrome ( ) ;
58+
59+ if ( ! pathToChrome || ! utils . existsSync ( pathToChrome ) ) {
60+ vscode . window . showErrorMessage ( 'Chrome was not found. Chrome must be installed for this extension to function. If you have Chrome installed at a custom location you can specify it in the \'chromePath\' setting.' ) ;
3561 return ;
3662 }
37- utils . launchLocalChrome ( pathToChrome , port , 'about:blank' ) ;
63+
64+ utils . launchLocalChrome ( pathToChrome , port , 'about:blank' ) ;
3865 }
66+
67+ ///json/new?{url}
68+ const target = JSON . parse ( await utils . getURL ( `http://${ hostname } :${ port } /json/new?${ launchUrl } ` ) ) ;
3969
40- attach ( context ) ;
70+ if ( ! target || ! target . webSocketDebuggerUrl || target . webSocketDebuggerUrl == '' ) {
71+ vscode . window . showErrorMessage ( `Could not find the launched Chrome tab: (${ launchUrl } ).` ) ;
72+ attach ( context ) ;
73+ } else {
74+ DevToolsPanel . createOrShow ( context . extensionPath , target . webSocketDebuggerUrl ) ;
75+ }
4176}
4277
4378async function attach ( context : vscode . ExtensionContext ) {
44- const responseArray = getListOfTargets ( ) ;
79+ const responseArray = await getListOfTargets ( ) ;
4580
4681 if ( Array . isArray ( responseArray ) ) {
4782 const items : QuickPickItem [ ] = [ ] ;
@@ -59,20 +94,6 @@ async function attach(context: vscode.ExtensionContext) {
5994 }
6095}
6196
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-
7697async function getWebSocketUri ( targetUrl : string ) : Promise < string > {
7798 const responseArray = await getListOfTargets ( ) ;
7899
0 commit comments