import * as fs from 'fs' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { ConnectionManager } from '../../infoManagers/connectionManager' ; import { HTTP_URL_PLACEHOLDER , WS_URL_PLACEHOLDER , } from '../../utils/constants' ; import { Disposable } from '../../utils/dispose' ; description interface replaceObj { original : string ; replacement : string ; } description export class HTMLInjector extends Disposable { private _script : string | undefined ; public rawScript : string ; description public get script ( ) : string | undefined { return this . _script ; } constructor ( _extensionUri , private readonly _connectionManager ) { super ( ) ; const scriptPath = path . join ( _extensionUri . fsPath , 'media' , 'injectScript.js' ) ; this . rawScript = fs . readFileSync ( scriptPath , 'utf8' ) . toString ( ) ; this . initScript ( this . rawScript , undefined , undefined ) ; this . _register ( this . _connectionManager . onConnected ( ( e ) => { this . refresh ( e . httpURI , e . wsURI ) ; } ) ) ; } description param { string } fileString private async initScript ( fileString , httpUri , wsUri ) { if ( ! httpUri ) { httpUri = await this . _connectionManager . resolveExternalHTTPUri ( ) ; } if ( ! wsUri ) { wsUri = await this . _connectionManager . resolveExternalWSUri ( ) ; } const wsURL = `ws://${ wsUri . authority }${ wsUri . path }` ; let httpURL = `${ httpUri . scheme }://${ httpUri . authority }` ; if ( httpURL . endsWith ( '/' ) ) { httpURL = httpURL . substr ( httpURL . length - 1 ) ; } const replacements = [ { original : WS_URL_PLACEHOLDER , replacement : wsURL } , { original : HTTP_URL_PLACEHOLDER , replacement : httpURL } , ] ; this . _script = this . replace ( fileString , replacements ) ; } param { string } script param { replaceObj [ ] } replaces returns { string } private replace ( script , replaces ) { for ( const i in replaces ) { const replace = replaces [ i ] ; const placeHolderIndex = script . indexOf ( replace . original ) ; script = script . substr ( 0 , placeHolderIndex ) + replace . replacement + script . substr ( placeHolderIndex + replace . original . length ) ; } return script ; } description private refresh ( httpUri , wsUri ) { this . initScript ( this . rawScript , httpUri , wsUri ) ; } }