From 6bfcc9665150a583f2e0e96506f1a946a29de05f Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 18 Apr 2023 20:49:37 +0200 Subject: [PATCH] Disable the "copy all text" feature when `enablePermissions` is set (PR 16286 follow-up) When permissions are enabled and the PDF document doesn't have the COPY-flag set, it shouldn't be possible for the user to trigger the "copy all text" feature. --- web/pdf_viewer.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/web/pdf_viewer.js b/web/pdf_viewer.js index 75f9c1156..9d299cd0e 100644 --- a/web/pdf_viewer.js +++ b/web/pdf_viewer.js @@ -255,11 +255,6 @@ class PDFViewer { throw new Error("The `container` must be absolutely positioned."); } } - const hiddenCopyElement = (this.#hiddenCopyElement = - document.createElement("div")); - hiddenCopyElement.id = "hiddenCopyElement"; - this.viewer.before(hiddenCopyElement); - this.#resizeObserver.observe(this.container); this.eventBus = options.eventBus; @@ -576,6 +571,15 @@ class PDFViewer { }; } + #createHiddenCopyElement() { + if (this.#hiddenCopyElement) { + return; + } + const element = (this.#hiddenCopyElement = document.createElement("div")); + element.id = "hiddenCopyElement"; + this.viewer.before(element); + } + /** * Currently only *some* permissions are supported. * @returns {Object} @@ -587,11 +591,14 @@ class PDFViewer { textLayerMode: this.textLayerMode, }; if (!permissions) { + this.#createHiddenCopyElement(); return params; } if (!permissions.includes(PermissionFlag.COPY)) { this.viewer.classList.add(ENABLE_PERMISSIONS_CLASS); + } else { + this.#createHiddenCopyElement(); } if (!permissions.includes(PermissionFlag.MODIFY_CONTENTS)) { @@ -1053,7 +1060,12 @@ class PDFViewer { // Reset all PDF document permissions. this.viewer.classList.remove(ENABLE_PERMISSIONS_CLASS); - document.removeEventListener("copy", this.#copyCallbackBound); + if (this.#hiddenCopyElement) { + document.removeEventListener("copy", this.#copyCallbackBound); + + this.#hiddenCopyElement.remove(); + this.#hiddenCopyElement = null; + } } #ensurePageViewVisible() {