From caebe335b261961efc8ba1f1d521532a88054d0d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 9 Aug 2023 10:38:27 +0200 Subject: [PATCH] Re-factor `PDFViewerApplication._initializeAutoPrint` slightly (PR 16779 follow-up) After the `src/core/`-changes in PR 16779 the `PDFDocumentProxy.getJSActions` method should no longer be able to return *empty* entries, which means that we can simplify the "JavaScript support is not enabled"-warning in the viewer. Furthermore, improve the auto-printing hack used when scripting is disabled. --- web/app.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/web/app.js b/web/app.js index 8053e6381..3db6bf719 100644 --- a/web/app.js +++ b/web/app.js @@ -1545,29 +1545,32 @@ const PDFViewerApplication = { async _initializeAutoPrint(pdfDocument, openActionPromise) { const [openAction, jsActions] = await Promise.all([ openActionPromise, - !this.pdfViewer.enableScripting ? pdfDocument.getJSActions() : null, + this.pdfViewer.enableScripting ? null : pdfDocument.getJSActions(), ]); if (pdfDocument !== this.pdfDocument) { return; // The document was closed while the auto print data resolved. } - let triggerAutoPrint = false; + let triggerAutoPrint = openAction?.action === "Print"; - if (openAction?.action === "Print") { - triggerAutoPrint = true; - } if (jsActions) { - for (const name in jsActions) { - if (jsActions[name]) { - console.warn("Warning: JavaScript support is not enabled"); - break; - } - } + console.warn("Warning: JavaScript support is not enabled"); // Hack to support auto printing. - triggerAutoPrint ||= !!( - jsActions.OpenAction && AutoPrintRegExp.test(jsActions.OpenAction) - ); + for (const name in jsActions) { + if (triggerAutoPrint) { + break; + } + switch (name) { + case "WillClose": + case "WillSave": + case "DidSave": + case "WillPrint": + case "DidPrint": + continue; + } + triggerAutoPrint = jsActions[name].some(js => AutoPrintRegExp.test(js)); + } } if (triggerAutoPrint) {