11import * as path from 'path' ;
22import * as vscode from 'vscode' ;
3+ import { ChromeDevToolsConfigurationProvider } from './ChromeDevToolsConfigurationProvider'
34import WebSocket from 'ws' ;
45import QuickPickItem = vscode . QuickPickItem ;
5- import QuickPickOptions = vscode . QuickPickOptions ;
66import * as utils from './utils' ;
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 ( ) ;
1112
1213export 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
2229async function launch ( context : vscode . ExtensionContext ) {
@@ -34,15 +41,8 @@ async function launch(context: vscode.ExtensionContext) {
3441}
3542
3643async 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+
6296class DevToolsPanel {
6397 private static currentPanel : DevToolsPanel ;
6498 private readonly _panel : vscode . WebviewPanel ;
0 commit comments