From 54f45dc935ba34f63f91291f35a5eeb3ee0e80ff Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 18 Dec 2020 22:16:24 +0100 Subject: [PATCH] Don't dispatch a "doc/Open" event in the sandbox when creating it failed There's really no point, as far as I can tell, to attempt to dispatch an event in a non-existent sandbox. Generally speaking, even trying to do this *could* possibly even lead to errors in some cases. Furthermore, utilize optional chaining to simplify some `dispatchEventInSandbox` calls throughout the viewer. Finally, replace superfluous `return` statements with `break` in the switch-statement in the `updateFromSandbox` event-handler. --- web/app.js | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/web/app.js b/web/app.js index 672d03edb..4623b3632 100644 --- a/web/app.js +++ b/web/app.js @@ -1036,13 +1036,10 @@ const PDFViewerApplication = { this.download({ sourceEventType }); return; } - - if (this._scriptingInstance) { - this._scriptingInstance.scripting.dispatchEventInSandbox({ - id: "doc", - name: "WillSave", - }); - } + this._scriptingInstance?.scripting.dispatchEventInSandbox({ + id: "doc", + name: "WillSave", + }); this._saveInProgress = true; this.pdfDocument @@ -1051,12 +1048,10 @@ const PDFViewerApplication = { const blob = new Blob([data], { type: "application/pdf" }); downloadManager.download(blob, url, filename, sourceEventType); - if (this._scriptingInstance) { - this._scriptingInstance.scripting.dispatchEventInSandbox({ - id: "doc", - name: "DidSave", - }); - } + this._scriptingInstance?.scripting.dispatchEventInSandbox({ + id: "doc", + name: "DidSave", + }); }) .catch(() => { this.download({ sourceEventType }); @@ -1514,15 +1509,15 @@ const PDFViewerApplication = { break; case "layout": this.pdfViewer.spreadMode = apiPageLayoutToSpreadMode(value); - return; + break; case "page-num": this.pdfViewer.currentPageNumber = value + 1; - return; + break; case "print": this.pdfViewer.pagesPromise.then(() => { this.triggerPrinting(); }); - return; + break; case "println": console.log(value); break; @@ -1608,7 +1603,9 @@ const PDFViewerApplication = { } } catch (error) { console.error(`_initializeJavaScript: "${error?.message}".`); + this._destroyScriptingInstance(); + return; } scripting.dispatchEventInSandbox({ @@ -2033,21 +2030,17 @@ const PDFViewerApplication = { if (!this.supportsPrinting) { return; } - if (this._scriptingInstance) { - this._scriptingInstance.scripting.dispatchEventInSandbox({ - id: "doc", - name: "WillPrint", - }); - } + this._scriptingInstance?.scripting.dispatchEventInSandbox({ + id: "doc", + name: "WillPrint", + }); window.print(); - if (this._scriptingInstance) { - this._scriptingInstance.scripting.dispatchEventInSandbox({ - id: "doc", - name: "DidPrint", - }); - } + this._scriptingInstance?.scripting.dispatchEventInSandbox({ + id: "doc", + name: "DidPrint", + }); }, bindEvents() {