From 044525aa1397cbd0a7d4d46a24495c93ca47422a Mon Sep 17 00:00:00 2001 From: William Chong Date: Tue, 23 Apr 2024 00:26:43 +0800 Subject: [PATCH] Add openBase64File postMessage support --- web/app.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/web/app.js b/web/app.js index d5803c01ec128..a0cc5419af369 100644 --- a/web/app.js +++ b/web/app.js @@ -2008,6 +2008,7 @@ const PDFViewerApplication = { window.addEventListener("click", webViewerClick); window.addEventListener("keydown", webViewerKeyDown); window.addEventListener("keyup", webViewerKeyUp); + window.addEventListener("message", webViewerPostMessage); window.addEventListener("resize", _boundEvents.windowResize); window.addEventListener("hashchange", _boundEvents.windowHashChange); window.addEventListener("beforeprint", _boundEvents.windowBeforePrint); @@ -2138,6 +2139,7 @@ const PDFViewerApplication = { window.removeEventListener("click", webViewerClick); window.removeEventListener("keydown", webViewerKeyDown); window.removeEventListener("keyup", webViewerKeyUp); + window.removeEventListener("message", webViewerPostMessage); window.removeEventListener("resize", _boundEvents.windowResize); window.removeEventListener("hashchange", _boundEvents.windowHashChange); window.removeEventListener("beforeprint", _boundEvents.windowBeforePrint); @@ -3309,6 +3311,24 @@ function webViewerKeyDown(evt) { } } +function webViewerPostMessage(evt) { + if (evt && evt.data && typeof evt.data === "string") { + try { + const { action, data } = JSON.parse(evt.data); + switch (action) { + case "openBase64File": + PDFViewerApplication.open({ + data: atob(data.data), + originalUrl: data.name, + }); + break; + } + } catch (ex) { + console.error(`webViewerPostMessage: ${ex}`); + } + } +} + function beforeUnload(evt) { evt.preventDefault(); evt.returnValue = "";