@@ -2,39 +2,36 @@ function getAbsoluteUrl(url) {
2
2
return new URL ( url , window . location ) . href ;
3
3
}
4
4
5
- window . onload = ( ) => {
6
- const socket = new WebSocket ( `ws://${ window . location . host } /ws` ) ;
7
-
8
- socket . onmessage = ( event ) => {
9
- if ( event . data === "reload" ) {
10
- window . location . reload ( ) ;
11
- } else if ( event . data . startsWith ( "update-css://" ) ) {
12
- const filePath = event . data . substring ( 13 ) ;
13
- const links = document . getElementsByTagName ( "link" ) ;
14
- for ( const link of links ) {
15
- if ( link . rel !== "stylesheet" ) continue ;
16
- const clonedLink = link . cloneNode ( true ) ;
17
- if ( getAbsoluteUrl ( link . href ) . startsWith ( getAbsoluteUrl ( filePath ) ) ) {
18
- const indexOf = link . href . indexOf ( "?" ) ;
19
- if ( indexOf !== - 1 && link . href . slice ( indexOf ) . includes ( "counter=" ) ) {
20
- const url = new URL ( link . href ) ;
21
- const params = new URLSearchParams ( url . search ) ;
22
- const counter = params . get ( "counter" ) ;
23
- if ( counter ) {
24
- params . set ( "counter" , Number . parseInt ( counter ) + 1 ) ;
25
- }
26
- url . search = params . toString ( ) ;
27
- clonedLink . href = url . toString ( ) ;
28
- } else {
29
- clonedLink . href += `${ indexOf !== - 1 ? "&" : "?" } counter=1` ;
5
+ const socket = new WebSocket ( `ws://${ window . location . host } /ws` ) ;
6
+ socket . onmessage = ( event ) => {
7
+ if ( event . data === "reload" ) {
8
+ window . location . reload ( ) ;
9
+ } else if ( event . data . startsWith ( "update-css://" ) ) {
10
+ const filePath = event . data . substring ( 13 ) ;
11
+ const links = document . getElementsByTagName ( "link" ) ;
12
+ for ( const link of links ) {
13
+ if ( link . rel !== "stylesheet" ) continue ;
14
+ const clonedLink = link . cloneNode ( true ) ;
15
+ if ( getAbsoluteUrl ( link . href ) . startsWith ( getAbsoluteUrl ( filePath ) ) ) {
16
+ const indexOf = link . href . indexOf ( "?" ) ;
17
+ if ( indexOf !== - 1 && link . href . slice ( indexOf ) . includes ( "counter=" ) ) {
18
+ const url = new URL ( link . href ) ;
19
+ const params = new URLSearchParams ( url . search ) ;
20
+ const counter = params . get ( "counter" ) ;
21
+ if ( counter ) {
22
+ params . set ( "counter" , Number . parseInt ( counter ) + 1 ) ;
30
23
}
24
+ url . search = params . toString ( ) ;
25
+ clonedLink . href = url . toString ( ) ;
26
+ } else {
27
+ clonedLink . href += `${ indexOf !== - 1 ? "&" : "?" } counter=1` ;
31
28
}
32
- link . replaceWith ( clonedLink ) ;
33
29
}
30
+ link . replaceWith ( clonedLink ) ;
34
31
}
35
- } ;
32
+ }
33
+ } ;
36
34
37
- socket . onerror = ( error ) => {
38
- console . error ( "Reload WebSocket error:" , error ) ;
39
- } ;
35
+ socket . onerror = ( error ) => {
36
+ console . error ( "Reload WebSocket error:" , error ) ;
40
37
} ;
0 commit comments