@@ -9,7 +9,7 @@ function trackLocationSearchChanges() {
99window . addEventListener ( 'popstate' , function ( event ) {
1010 if ( event . state ) {
1111 $ ( '#cypht-main' ) . replaceWith ( event . state . main ) ;
12- loadCustomScripts ( event . state . head ) ;
12+ loadCustomScripts ( event . state . scripts ) ;
1313 }
1414
1515 window . location . next = window . location . search ;
@@ -29,7 +29,7 @@ window.addEventListener('popstate', function(event) {
2929
3030window . addEventListener ( 'load' , function ( ) {
3131 const unMountCallback = renderPage ( window . location . href ) ;
32- history . replaceState ( { main : $ ( '#cypht-main' ) . prop ( 'outerHTML' ) , head : $ ( 'head' ) . prop ( 'outerHTML' ) } , "" ) ;
32+ history . replaceState ( { main : $ ( '#cypht-main' ) . prop ( 'outerHTML' ) , scripts : extractCustomScripts ( $ ( document ) ) } , "" ) ;
3333
3434 if ( unMountCallback ) {
3535 unMountSubscribers [ window . location . search ] = unMountCallback ;
@@ -74,16 +74,16 @@ async function navigate(url, loaderMessage) {
7474 document . title = title . replace ( / < [ ^ > ] * > / g, '' ) ;
7575
7676 // load custom javascript
77- const head = html . match ( / < h e a d [ ^ > ] * > ( ( . | [ \n \r ] ) * ) < \/ h e a d > / i ) [ 0 ] ;
78- loadCustomScripts ( head ) ;
77+ const scripts = extractCustomScripts ( $ ( html ) ) ;
78+ loadCustomScripts ( scripts ) ;
7979
8080 window . location . next = url ;
8181
8282 scrollTo ( 0 , 0 ) ;
8383
8484 const unMountCallback = renderPage ( url ) ;
8585
86- history . pushState ( { main : cyphtMain , head } , "" , url ) ;
86+ history . pushState ( { main : cyphtMain , scripts } , "" , url ) ;
8787
8888 if ( unMountCallback ) {
8989 unMountSubscribers [ url ] = unMountCallback ;
@@ -101,11 +101,33 @@ async function navigate(url, loaderMessage) {
101101 }
102102}
103103
104- function loadCustomScripts ( head ) {
105- const newHead = $ ( '<div>' ) . append ( head ) ;
106- $ ( document . head ) . find ( 'script#data-store' ) . replaceWith ( newHead . find ( 'script#data-store' ) ) ;
107- $ ( document . head ) . find ( 'script#search-data' ) . replaceWith ( newHead . find ( 'script#search-data' ) ) ;
108- $ ( document . head ) . find ( 'script#inline-msg-state' ) . replaceWith ( newHead . find ( 'script#inline-msg-state' ) ) ;
104+ function extractCustomScripts ( $el ) {
105+ const scripts = [ ] ;
106+ let candidates = [ ] ;
107+ if ( $el . length == 1 ) {
108+ for ( const el of $el . find ( 'script' ) ) {
109+ candidates . push ( el ) ;
110+ }
111+ } else {
112+ for ( const el of $el ) {
113+ if ( $ ( el ) . is ( 'script' ) ) {
114+ candidates . push ( el ) ;
115+ }
116+ }
117+ }
118+ for ( const script of candidates ) {
119+ if ( [ 'data-store' , 'search-data' , 'inline-msg-state' ] . indexOf ( $ ( script ) . attr ( 'id' ) ) >= 0 ) {
120+ scripts . push ( $ ( script ) . prop ( 'outerHTML' ) ) ;
121+ }
122+ }
123+ return scripts ;
124+ }
125+
126+ function loadCustomScripts ( scripts ) {
127+ for ( const script of scripts ) {
128+ const id = $ ( script ) . attr ( 'id' ) ;
129+ $ ( 'script#' + id ) . replaceWith ( script ) ;
130+ }
109131}
110132
111133function renderPage ( href ) {
0 commit comments