@@ -43,27 +43,27 @@ const utils = (function () {
4343 function parseRequestHeader ( headerText ) {
4444 const header = { } ;
4545
46- header . method = headerText . substring ( 0 , headerText . indexOf ( ' ' ) ) ;
47- headerText = headerText . substring ( headerText . indexOf ( ' ' ) + 1 ) ;
46+ header . method = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( ' ' ) ) ) ;
47+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( ' ' ) + 1 ) ) ;
4848
49- header . uri = headerText . substring ( 0 , headerText . indexOf ( ' ' ) ) ;
50- headerText = headerText . substring ( headerText . indexOf ( ' ' ) + 1 ) ;
49+ header . uri = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( ' ' ) ) ) ;
50+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( ' ' ) + 1 ) ) ;
5151
52- header . version = headerText . substring ( 0 , headerText . indexOf ( '\r' ) ) ;
53- headerText = headerText . substring ( headerText . indexOf ( '\n' ) + 1 ) ;
52+ header . version = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( '\r' ) ) ) ;
53+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( '\n' ) + 1 ) ) ;
5454
5555 header . fields = { } ;
5656 while ( headerText !== '' ) {
57- const field = headerText . substring ( 0 , headerText . indexOf ( ':' ) ) ;
58- headerText = headerText . substring ( headerText . indexOf ( ':' ) + 2 ) ;
57+ const field = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( ':' ) ) ) ;
58+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( ':' ) + 2 ) ) ;
5959 let value ;
6060
61- if ( headerText . indexOf ( '\n' ) < 0 ) {
61+ if ( ! headerText . includes ( '\n' ) ) {
6262 value = headerText ;
6363 headerText = '' ;
6464 } else {
65- value = headerText . substring ( 0 , headerText . indexOf ( '\n' ) ) ;
66- headerText = headerText . substring ( headerText . indexOf ( '\n' ) + 1 ) ;
65+ value = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( '\n' ) ) ) ;
66+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( '\n' ) + 1 ) ) ;
6767 }
6868
6969 header . fields [ field ] = value ;
@@ -78,22 +78,22 @@ const utils = (function () {
7878 function parseResponseHeader ( headerText ) {
7979 const header = { } ;
8080
81- header . version = headerText . substring ( 0 , headerText . indexOf ( ' ' ) ) ;
82- headerText = headerText . substring ( headerText . indexOf ( ' ' ) + 1 ) ;
81+ header . version = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( ' ' ) ) ) ;
82+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( ' ' ) + 1 ) ) ;
8383
84- header . status = headerText . substring ( 0 , headerText . indexOf ( ' ' ) ) ;
85- headerText = headerText . substring ( headerText . indexOf ( ' ' ) + 1 ) ;
84+ header . status = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( ' ' ) ) ) ;
85+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( ' ' ) + 1 ) ) ;
8686
87- header . reason = headerText . substring ( 0 , headerText . indexOf ( ' ' ) ) ;
88- headerText = headerText . substring ( headerText . indexOf ( ' ' ) + 1 ) ;
87+ header . reason = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( ' ' ) ) ) ;
88+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( ' ' ) + 1 ) ) ;
8989
9090 header . fields = { } ;
9191 while ( headerText !== '' ) {
92- const field = headerText . substring ( 0 , headerText . indexOf ( ':' ) ) ;
93- headerText = headerText . substring ( headerText . indexOf ( ':' ) + 2 ) ;
92+ const field = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( ':' ) ) ) ;
93+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( ':' ) + 2 ) ) ;
9494
95- const value = headerText . substring ( 0 , headerText . indexOf ( '\n' ) ) ;
96- headerText = headerText . substring ( headerText . indexOf ( '\n' ) + 1 ) ;
95+ const value = headerText . slice ( 0 , Math . max ( 0 , headerText . indexOf ( '\n' ) ) ) ;
96+ headerText = headerText . slice ( Math . max ( 0 , headerText . indexOf ( '\n' ) + 1 ) ) ;
9797
9898 header . fields [ field ] = value ;
9999 }
@@ -121,22 +121,22 @@ const utils = (function () {
121121 hostname = hostname . split ( '?' ) [ 0 ] ;
122122 hostname = hostname . split ( '#' ) [ 0 ] ;
123123
124- // Remove port if present
125- hostname = hostname . split ( ':' ) [ 0 ] ;
124+ // Remove port if present
125+ hostname = hostname . split ( ':' ) [ 0 ] ;
126126
127127 // Split the hostname into parts
128- const parts = hostname . split ( '.' ) ;
128+ const parts = hostname . split ( '.' ) ;
129129
130130 // If the hostname has more than two parts, return the last two parts as the domain
131131 if ( parts . length > 2 ) {
132- return parts . slice ( - 2 ) . join ( '.' ) ;
133- }
132+ return parts . slice ( - 2 ) . join ( '.' ) ;
133+ }
134134
135135 return hostname ;
136136 }
137137
138138 function hasScheme ( url ) {
139- return url . indexOf ( '://' ) > - 1 ;
139+ return url . includes ( '://' ) ;
140140 }
141141
142142 /*
@@ -163,7 +163,7 @@ const utils = (function () {
163163 * Initialize all of the info that will be stored in indexeddb.
164164 */
165165 function initializeHUD ( leftTools , rightTools , drawer ) {
166- if ( IS_DEV_MODE && leftTools . indexOf ( 'hudErrors' ) < 0 ) {
166+ if ( IS_DEV_MODE && ! leftTools . includes ( 'hudErrors' ) ) {
167167 // Always add the error tool in dev mode
168168 leftTools . push ( 'hudErrors' ) ;
169169 }
@@ -206,8 +206,8 @@ const utils = (function () {
206206 function setDefaultTools ( leftTools , rightTools ) {
207207 const promises = [ ] ;
208208
209- for ( let i = 0 ; i < leftTools . length ; i ++ ) {
210- loadTool ( leftTools [ i ] )
209+ for ( const [ i , leftTool ] of leftTools . entries ( ) ) {
210+ loadTool ( leftTool )
211211 . then ( tool => {
212212 if ( ! tool ) {
213213 log ( LOG_ERROR , 'utils.setDefaultTools' , 'Failed to load tool.' , tool . name ) ;
@@ -223,8 +223,8 @@ const utils = (function () {
223223 . catch ( errorHandler ) ;
224224 }
225225
226- for ( let i = 0 ; i < rightTools . length ; i ++ ) {
227- loadTool ( rightTools [ i ] )
226+ for ( const [ i , rightTool ] of rightTools . entries ( ) ) {
227+ loadTool ( rightTool )
228228 . then ( tool => {
229229 if ( ! tool ) {
230230 log ( LOG_ERROR , 'utils.setDefaultTools' , 'Failed to load tool.' , tool . name ) ;
@@ -441,8 +441,7 @@ const utils = (function () {
441441 function messageFrame ( tabId , frameId , message ) {
442442 return clients . matchAll ( { includeUncontrolled : true } )
443443 . then ( clients => {
444- for ( let i = 0 ; i < clients . length ; i ++ ) {
445- const client = clients [ i ] ;
444+ for ( const client of clients ) {
446445 const parameters = new URL ( client . url ) . searchParams ;
447446
448447 const tid = parameters . get ( 'tabId' ) ;
@@ -480,8 +479,7 @@ const utils = (function () {
480479 . then ( clients => {
481480 const frameClients = [ ] ;
482481
483- for ( let i = 0 ; i < clients . length ; i ++ ) {
484- const client = clients [ i ] ;
482+ for ( const client of clients ) {
485483 const parameters = new URL ( client . url ) . searchParams ;
486484
487485 const fid = parameters . get ( 'frameId' ) ;
@@ -500,9 +498,7 @@ const utils = (function () {
500498 } )
501499 . then ( clients => {
502500 return new Promise ( ( ( resolve , reject ) => {
503- for ( let i = 0 ; i < clients . length ; i ++ ) {
504- const client = clients [ i ] ;
505-
501+ for ( const client of clients ) {
506502 const channel = new MessageChannel ( ) ;
507503 channel . port1 . start ( ) ;
508504 channel . port2 . start ( ) ;
@@ -535,8 +531,7 @@ const utils = (function () {
535531 . then ( clients => {
536532 const frameClients = [ ] ;
537533
538- for ( let i = 0 ; i < clients . length ; i ++ ) {
539- const client = clients [ i ] ;
534+ for ( const client of clients ) {
540535 const parameters = new URL ( client . url ) . searchParams ;
541536
542537 const fid = parameters . get ( 'frameId' ) ;
@@ -666,7 +661,7 @@ const utils = (function () {
666661 scheme = 'http' ;
667662 }
668663
669- return scheme + '://' + domain + url . substring ( url . indexOf ( domain ) + domain . length ) ;
664+ return scheme + '://' + domain + url . slice ( Math . max ( 0 , url . indexOf ( domain ) + domain . length ) ) ;
670665 } )
671666 . catch ( errorHandler ) ;
672667 }
@@ -681,7 +676,7 @@ const utils = (function () {
681676 // Construct the stack trace
682677 const lines = error . stack . split ( '\n' ) . slice ( 0 , - 1 ) ;
683678 lines . forEach ( line => {
684- const functionName = line . substring ( 0 , line . indexOf ( '/' ) ) ;
679+ const functionName = line . slice ( 0 , Math . max ( 0 , line . indexOf ( '/' ) ) ) ;
685680 const urlAndLineNo = line . substring ( line . indexOf ( 'http' ) , line . length - 1 ) ;
686681 const parts = urlAndLineNo . split ( ':' ) ;
687682 let url = parts [ 0 ] + ':' + parts [ 1 ] ;
@@ -746,43 +741,43 @@ const utils = (function () {
746741 return dateObject . toISOString ( ) . slice ( 11 , 23 ) ;
747742 }
748743
749- if ( typeof module == 'object' ) // This if check is for unit tests as they work in Node environment only because module.exports is not supported in browser
750- {
751- module . exports = {
752- parseRequestHeader,
753- parseResponseHeader,
754- isFromTrustedOrigin,
755- parseDomainFromUrl,
756- getParameter,
757- isHUDInitialized,
758- initializeHUD,
759- loadFrame,
760- saveFrame,
761- registerTool,
762- registerTools,
763- loadTool,
764- writeTool,
765- loadPanelTools,
766- loadAllTools,
767- addToolToPanel,
768- removeToolFromPanel,
769- messageFrame,
770- messageAllTabs,
771- getAllClients,
772- getWindowVisibilityState,
773- messageWindow,
774- sortToolsByPosition,
775- configureButtonHtml,
776- getUpgradedDomain,
777- getUpgradedUrl,
778- errorHandler,
779- getZapFilePath,
780- getZapImagePath,
781- zapApiErrorDialog,
782- log,
783- timestampToTimeString
784- } ;
744+ if ( typeof module === 'object' ) { // This if check is for unit tests as they work in Node environment only because module.exports is not supported in browser
745+ module . exports = {
746+ parseRequestHeader,
747+ parseResponseHeader,
748+ isFromTrustedOrigin,
749+ parseDomainFromUrl,
750+ getParameter,
751+ isHUDInitialized,
752+ initializeHUD,
753+ loadFrame,
754+ saveFrame,
755+ registerTool,
756+ registerTools,
757+ loadTool,
758+ writeTool,
759+ loadPanelTools,
760+ loadAllTools,
761+ addToolToPanel,
762+ removeToolFromPanel,
763+ messageFrame,
764+ messageAllTabs,
765+ getAllClients,
766+ getWindowVisibilityState,
767+ messageWindow,
768+ sortToolsByPosition,
769+ configureButtonHtml,
770+ getUpgradedDomain,
771+ getUpgradedUrl,
772+ errorHandler,
773+ getZapFilePath,
774+ getZapImagePath,
775+ zapApiErrorDialog,
776+ log,
777+ timestampToTimeString
778+ } ;
785779 }
780+
786781 return {
787782 parseRequestHeader,
788783 parseResponseHeader,
0 commit comments