@@ -140,11 +140,21 @@ BookReader.prototype.urlUpdateFragment = function() {
140140 return validParams ;
141141 } , { } ) ;
142142
143+ // eg 'page/3/mode/2up'; no query params (in hash mode, it might have /search/term)
144+ // Does NOT have the :~:text fragment
143145 const newFragment = this . fragmentFromParams ( params , this . options . urlMode ) ;
146+ // eg 'page/3/mode/2up'; no query params
147+ // WILL CONTAIN the :~:text fragment in hash mode (!)
144148 const currFragment = this . urlReadFragment ( ) ;
149+ // This should have both ?q=foo&text=bar (and any other params) as an encoded string
145150 const currQueryString = this . getLocationSearch ( ) ;
151+ // Eg ?q=foo&text=bar; only query params, no fragment
146152 const newQueryString = this . queryStringFromParams ( params , currQueryString , this . options . urlMode ) ;
147- const hasTextParam = currQueryString . includes ( "text" ) ;
153+
154+ // NOTE: If ?text is in the URL, we will fire fragment change events on every render; which is
155+ // not desireable, but currently don't have a way to handle re-writing ?text to the hash text
156+ // fragment form, :~:text=foo.
157+ const hasTextParam = this . urlPlugin . retrieveTextFragment ( currQueryString ) ;
148158 if ( currFragment === newFragment && currQueryString === newQueryString && ! hasTextParam ) {
149159 return ;
150160 }
@@ -155,21 +165,14 @@ BookReader.prototype.urlUpdateFragment = function() {
155165 } else {
156166 const baseWithoutSlash = this . options . urlHistoryBasePath . replace ( / \/ + $ / , '' ) ;
157167 const newFragmentWithSlash = newFragment === '' ? '' : `/${ newFragment } ` ;
158- let textFragment = "" ;
159- if ( this . urlPlugin . retrieveTextFragment ( newQueryString ) ) {
160- textFragment = this . urlParamsFiltersOnlySearch ( this . readQueryString ( ) ) ;
161- // newQueryString = newQueryString.replace(this.urlPlugin.retrieveTextFragment(newQueryString)[0], "").replace(/(\?text=)/, "")
162- }
163- console . log ( "this is newQueryString" , newQueryString ) ;
168+ const textFragment = this . urlPlugin . retrieveTextFragment ( newQueryString ) ;
164169 const newUrlPath = `${ baseWithoutSlash } ${ newFragmentWithSlash } ${ newQueryString } ` ;
165- console . log ( "this is newURLPath" , newUrlPath ) ;
166170 try {
167171 window . history . replaceState ( { } , null , newUrlPath ) ;
168- this . oldLocationHash = newFragment + newQueryString + textFragment ;
169- // if (textFragment) {
170- // window.location.replace('#' + textFragment);
171- // this.oldLocationHash = textFragment;
172- // }
172+ this . oldLocationHash = newFragment + newQueryString ;
173+ if ( textFragment ) {
174+ this . oldLocationHash += `:~:text=${ textFragment [ 0 ] } ` ;
175+ }
173176 } catch ( e ) {
174177 // DOMException on Chrome when in sandboxed iframe
175178 this . options . urlMode = 'hash' ;
@@ -179,8 +182,12 @@ BookReader.prototype.urlUpdateFragment = function() {
179182
180183 if ( this . options . urlMode === 'hash' ) {
181184 const newQueryStringSearch = this . urlParamsFiltersOnlySearch ( this . readQueryString ( ) ) ;
182- window . location . replace ( '#' + newFragment + newQueryStringSearch ) ;
183- this . oldLocationHash = newFragment + newQueryStringSearch ;
185+ let textFragment = this . urlPlugin . retrieveTextFragment ( this . readQueryString ( ) ) ;
186+ if ( textFragment ) {
187+ textFragment = `:~:text=${ textFragment [ 0 ] } ` ;
188+ }
189+ window . location . replace ( '#' + newFragment + newQueryStringSearch + textFragment ) ;
190+ this . oldLocationHash = newFragment + newQueryStringSearch + textFragment ;
184191 }
185192} ;
186193
@@ -194,11 +201,9 @@ BookReader.prototype.urlUpdateFragment = function() {
194201
195202// testing with this URL http://127.0.0.1:8000/BookReaderDemo/demo-internetarchive.html?ocaid=adventureofsherl0000unse&text=Well%2C I found my plans very seriously menaced.&q=breaking the law#page/18/mode/2up
196203BookReader . prototype . urlParamsFiltersOnlySearch = function ( url ) {
197- const text = this . urlPlugin . retrieveTextFragment ( url ) ;
198204 const params = new URLSearchParams ( url ) ;
199205 let output = '' ;
200206 output += params . has ( 'q' ) ? `?${ new URLSearchParams ( { q : params . get ( 'q' ) } ) } ` : '' ;
201- output += text ? `:~:text=${ text [ 0 ] } ` : '' ;
202207 return output ;
203208} ;
204209
@@ -230,10 +235,15 @@ export class BookreaderUrlPlugin extends BookReader {
230235 const location = this . getLocationSearch ( ) ;
231236 if ( location . includes ( "text=" ) ) {
232237 this . on ( 'textLayerRendered' , ( _ , { pageIndex, container} ) => {
233- window . location . replace ( `#${ this . oldLocationHash } ` ) ;
234- } ) ;
235- this . on ( 'pageVisible' , ( _ ) => {
236- window . location . replace ( `#${ this . oldLocationHash } ` ) ;
238+ if ( this . options . urlMode === 'history' ) {
239+ // only want the text fragment forwarded
240+ if ( this . oldLocationHash . includes ( ':~:' ) ) {
241+ const newHash = this . oldLocationHash . split ( ':~:' ) [ 1 ] ;
242+ window . location . replace ( `#:~:${ newHash } ` ) ;
243+ }
244+ } else {
245+ window . location . replace ( `#${ this . oldLocationHash } ` ) ;
246+ }
237247 } ) ;
238248 }
239249 this . bind ( BookReader . eventNames . PostInit , ( ) => {
0 commit comments