@@ -171,10 +171,7 @@ BookReader.prototype.urlUpdateFragment = function() {
171171 const newFragmentWithSlash = newFragment === '' ? '' : `/${ newFragment } ` ;
172172 const textFragment = this . urlPlugin . retrieveTextFragment ( newQueryString ) ;
173173 const newUrlPath = `${ baseWithoutSlash } ${ newFragmentWithSlash } ${ newQueryString } ` ;
174- let extractedPage = newFragmentWithSlash . match ( / (?< = \/ ) n ? \d + (? = \/ ) / ) ;
175- if ( extractedPage ) {
176- extractedPage = extractedPage [ 0 ] ;
177- }
174+ const extractedPage = this . urlPlugin . urlStringToUrlState ( newFragmentWithSlash ) ?. page ;
178175 if ( ! this . textFragmentPage && textFragment ) {
179176 this . textFragmentPage = extractedPage ? extractedPage : null ;
180177 this . textFragment = `:~:text=${ textFragment } ` ;
@@ -261,14 +258,13 @@ export class BookreaderUrlPlugin extends BookReader {
261258 this . on ( 'textLayerVisible' , async ( _ , { pageContainerEl} ) => {
262259 const visiblePageNum = pageContainerEl . getAttribute ( 'data-page-num' ) ;
263260 await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
261+ // No textFragment found or the textFragment stored doesn't match current visible page loaded
262+ if ( ! this . textFragment || this . textFragmentPage !== visiblePageNum ) return ;
264263 if ( this . options . urlMode === 'history' ) {
265- if ( this . textFragment && this . textFragmentPage == visiblePageNum ) {
266- window . location . replace ( `#${ this . textFragment } ` ) ;
267- }
264+ window . location . replace ( `#${ this . textFragment } ` ) ;
268265 } else {
269- if ( this . textFragment && this . textFragmentPage == visiblePageNum ) {
270- window . location . replace ( `#${ this . oldLocationHash } ` ) ;
271- }
266+ // for urlMode hash, textFragment is stored in oldLocationHash already
267+ window . location . replace ( `#${ this . oldLocationHash } ` ) ;
272268 }
273269 } ) ;
274270 }
0 commit comments