diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js index 6e87c3149..7a34117e9 100644 --- a/web/pdf_find_controller.js +++ b/web/pdf_find_controller.js @@ -771,6 +771,9 @@ class PDFFindController { } #calculateMatch(pageIndex) { + if (!this.#state) { + return; + } const query = this.#query; if (query.length === 0) { return; // Do nothing: the matches should be wiped out already. @@ -882,12 +885,17 @@ class PDFFindController { let deferred = Promise.resolve(); const textOptions = { disableNormalization: true }; + const pdfDoc = this._pdfDocument; for (let i = 0, ii = this._linkService.pagesCount; i < ii; i++) { const { promise, resolve } = Promise.withResolvers(); this._extractTextPromises[i] = promise; - deferred = deferred.then(() => - this._pdfDocument + deferred = deferred.then(async () => { + if (pdfDoc !== this._pdfDocument) { + resolve(); + return; + } + await pdfDoc .getPage(i + 1) .then(pdfPage => pdfPage.getTextContent(textOptions)) .then( @@ -920,8 +928,8 @@ class PDFFindController { this._hasDiacritics[i] = false; resolve(); } - ) - ); + ); + }); } }