From 545bd931b5eae9530b0e5677390ff112358d72de Mon Sep 17 00:00:00 2001 From: ananyapahwa Date: Fri, 5 Dec 2025 12:42:46 +0530 Subject: [PATCH] Enable historyUpdateUrl for local files --- web/pdf_history.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/web/pdf_history.js b/web/pdf_history.js index bed96f370ddc8..9e7552d7d2a86 100644 --- a/web/pdf_history.js +++ b/web/pdf_history.js @@ -88,6 +88,12 @@ class PDFHistory { * @param {InitializeParameters} params */ initialize({ fingerprint, resetHistory = false, updateUrl = false }) { + const isLocalFile = window.location.protocol === "file:"; + this._isLocalFile = isLocalFile; + if (isLocalFile) { + this._loadLocalPosition(); + } + if (!fingerprint || typeof fingerprint !== "string") { console.error( 'PDFHistory.initialize: The "fingerprint" must be a non-empty string.' @@ -668,6 +674,43 @@ class PDFHistory { if (!this._destination || this._destination.temporary) { this.#tryPushCurrentPosition(); } + if (this._isLocalFile) { + this._saveLocalPosition(); + } + } + + _saveLocalPosition() { + if (!this._isLocalFile || !this._position) { + return; + } + const data = { + page: this._position.page || this.linkService.page, + rotation: this.linkService.rotation, + }; + localStorage.setItem( + `pdfjs_last_position_${this._fingerprint}`, + JSON.stringify(data) + ); + } + + _loadLocalPosition() { + if (!this._isLocalFile) { + return; + } + const saved = localStorage.getItem( + `pdfjs_last_position_${this._fingerprint}` + ); + if (saved) { + try { + const { page, rotation } = JSON.parse(saved); + this.linkService.page = page; + if (isValidRotation(rotation)) { + this.linkService.rotation = rotation; + } + } catch (err) { + console.warn("[pdfjs] Failed to parse saved local PDF position:", err); + } + } } #bindEvents() {