export class Loader { protected loadingFinished : any ; protected totalRequired : any ; protected loadCount : number ; public require ( scripts , loadingFinished ) { this . loadCount = 0 ; this . totalRequired = scripts . length ; this . loadingFinished = loadingFinished ; for ( let i = 0 ; i < scripts . length ; i ++ ) { this . writeScript ( chrome . extension . getURL ( scripts [ i ] ) ) ; } } public loaded ( ) { this . loadCount ++ ; if ( this . loadCount == this . totalRequired && typeof this . loadingFinished === "function" ) { this . loadingFinished ( ) ; } } public writeScript ( src ) { const ext = src . substr ( src . lastIndexOf ( "." ) + 1 ) ; const head = document . getElementsByTagName ( "head" ) [ 0 ] ; if ( ext === "js" ) { const s = document . createElement ( "script" ) ; s . type = "text/javascript" ; s . async = false ; s . src = src ; s . addEventListener ( "load" , ( ) => { this . loaded ( ) ; } , false ) ; head . appendChild ( s ) ; } else if ( ext === "css" ) { const link = document . createElement ( "link" ) ; link . href = src ; link . addEventListener ( "load" , ( ) => { this . loaded ( ) ; } , false ) ; link . type = "text/css" ; link . rel = "stylesheet" ; head . appendChild ( link ) ; } } public injectJS ( codeString ) { const inner = document . createElement ( "script" ) ; inner . textContent = codeString ; ( document . head || document . documentElement ) . appendChild ( inner ) ; } }