From 40a3de8c37f6a08f773b747aef990cbd13cad249 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 9 Feb 2023 11:12:20 +0100 Subject: [PATCH] Move `rewriteUrlClosure` into the `web/chromecom.js` file Given that this is only used for the Chrome-addon, we can move this code into the relevant file instead. --- web/chromecom.js | 17 +++++++++++++++++ web/viewer.js | 20 -------------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/web/chromecom.js b/web/chromecom.js index a5e337fbc..bfd0d95f3 100644 --- a/web/chromecom.js +++ b/web/chromecom.js @@ -27,6 +27,23 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) { ); } +(function rewriteUrlClosure() { + // Run this code outside DOMContentLoaded to make sure that the URL + // is rewritten as soon as possible. + const queryString = document.location.search.slice(1); + const m = /(^|&)file=([^&]*)/.exec(queryString); + const defaultUrl = m ? decodeURIComponent(m[2]) : ""; + + // Example: chrome-extension://.../http://example.com/file.pdf + const humanReadableUrl = "/" + defaultUrl + location.hash; + history.replaceState(history.state, "", humanReadableUrl); + if (top === window) { + chrome.runtime.sendMessage("showPageAction"); + } + + AppOptions.set("defaultUrl", defaultUrl); +})(); + const ChromeCom = { /** * Creates an event that the extension is listening for and will diff --git a/web/viewer.js b/web/viewer.js index 96af87021..7a8dabf53 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -36,26 +36,6 @@ window.PDFViewerApplication = PDFViewerApplication; window.PDFViewerApplicationConstants = AppConstants; window.PDFViewerApplicationOptions = AppOptions; -if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) { - (function rewriteUrlClosure() { - // Run this code outside DOMContentLoaded to make sure that the URL - // is rewritten as soon as possible. - const queryString = document.location.search.slice(1); - const m = /(^|&)file=([^&]*)/.exec(queryString); - const defaultUrl = m ? decodeURIComponent(m[2]) : ""; - - // Example: chrome-extension://.../http://example.com/file.pdf - const humanReadableUrl = "/" + defaultUrl + location.hash; - history.replaceState(history.state, "", humanReadableUrl); - if (top === window) { - // eslint-disable-next-line no-undef - chrome.runtime.sendMessage("showPageAction"); - } - - AppOptions.set("defaultUrl", defaultUrl); - })(); -} - function getViewerConfiguration() { return { appContainer: document.body,