@@ -42,6 +42,10 @@ BookReader.prototype.setup = (function(super_) {
4242 this . locationPollId = null ;
4343 this . oldLocationHash = null ;
4444 this . oldUserHash = null ;
45+ // Should include the :~:text= prefix
46+ this . textFragment = null ;
47+ // Tracks the original textFragment page num when first loaded
48+ this . textFragmentPage = null ;
4549 } ;
4650} ) ( BookReader . prototype . setup ) ;
4751
@@ -167,6 +171,14 @@ BookReader.prototype.urlUpdateFragment = function() {
167171 const newFragmentWithSlash = newFragment === '' ? '' : `/${ newFragment } ` ;
168172 const textFragment = this . urlPlugin . retrieveTextFragment ( newQueryString ) ;
169173 const newUrlPath = `${ baseWithoutSlash } ${ newFragmentWithSlash } ${ newQueryString } ` ;
174+ let extractedPage = newFragmentWithSlash . match ( / (?< = \/ ) n ? \d + (? = \/ ) / ) ;
175+ if ( extractedPage ) {
176+ extractedPage = extractedPage [ 0 ] ;
177+ }
178+ if ( ! this . textFragmentPage && textFragment ) {
179+ this . textFragmentPage = extractedPage ? extractedPage : null ;
180+ this . textFragment = `:~:text=${ textFragment } ` ;
181+ }
170182 try {
171183 window . history . replaceState ( { } , null , newUrlPath ) ;
172184 this . oldLocationHash = newFragment + newQueryString ;
@@ -183,8 +195,20 @@ BookReader.prototype.urlUpdateFragment = function() {
183195 if ( this . options . urlMode === 'hash' ) {
184196 const newQueryStringSearch = this . urlParamsFiltersOnlySearch ( this . readQueryString ( ) ) ;
185197 let textFragment = this . urlPlugin . retrieveTextFragment ( this . readQueryString ( ) ) ;
198+ let extractedPage = newFragment . match ( / (?< = \/ ) n ? \d + (? = \/ ) / ) ;
199+ if ( extractedPage ) {
200+ extractedPage = extractedPage [ 0 ] ;
201+ }
186202 if ( textFragment ) {
187203 textFragment = `:~:text=${ textFragment [ 0 ] } ` ;
204+ } else {
205+ textFragment = '' ;
206+ }
207+ if ( ! this . textFragmentPage && textFragment ) {
208+ this . textFragmentPage = extractedPage ? extractedPage : null ;
209+ this . textFragment = textFragment ;
210+ } else if ( this . textFragmentPage && extractedPage != this . textFragmentPage ) {
211+ textFragment = '' ;
188212 }
189213 window . location . replace ( '#' + newFragment + newQueryStringSearch + textFragment ) ;
190214 this . oldLocationHash = newFragment + newQueryStringSearch + textFragment ;
@@ -234,15 +258,17 @@ export class BookreaderUrlPlugin extends BookReader {
234258 this . urlPlugin = new UrlPlugin ( this . options ) ;
235259 const location = this . getLocationSearch ( ) ;
236260 if ( location . includes ( "text=" ) ) {
237- this . on ( 'textLayerRendered' , ( _ , { pageIndex, container} ) => {
261+ this . on ( 'textLayerVisible' , async ( _ , { pageContainerEl} ) => {
262+ const visiblePageNum = pageContainerEl . getAttribute ( 'data-page-num' ) ;
263+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
238264 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 } ` ) ;
265+ if ( this . textFragment && this . textFragmentPage == visiblePageNum ) {
266+ window . location . replace ( `#${ this . textFragment } ` ) ;
243267 }
244268 } else {
245- window . location . replace ( `#${ this . oldLocationHash } ` ) ;
269+ if ( this . textFragment && this . textFragmentPage == visiblePageNum ) {
270+ window . location . replace ( `#${ this . oldLocationHash } ` ) ;
271+ }
246272 }
247273 } ) ;
248274 }
0 commit comments