From 7bab8350c0d7c479234e1474d0681f52c599bd9d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 21 Dec 2020 11:43:54 +0100 Subject: [PATCH] Avoid the `getJavaScript` API-call in `PDFViewerApplication._initializeAutoPrint` when "enableScripting" is set Rather than calling `getJavaScript` in the API and then ignoring the result, when "enableScripting" is set, it should be more efficient/faster to simply skip it altogether instead. Finally, the `setTimeout` call at the end of `PDFViewerApplication._initializeAutoPrint` is removed, since it doesn't seem necessary any more as far as I can tell.[1] Note that when this functionality was originally added, back in PR 2839, it seems that `pagesPromise` simply waited for the `getPage` calls of *all* pages to resolve. Today, on the other hand, the viewer fetches *and* renders the first page *before* doing the remaining `getPage` calls, and only afterwards is `pagesPromise` resolved. Hence it's not really clear why we now need to delay printing even further with a `setTimeout` call. --- [1] The patch was tested with the following documents: https://github.com/mozilla/pdf.js/blob/master/test/pdfs/bug1001080.pdf and https://github.com/mozilla/pdf.js/blob/master/test/pdfs/issue6106.pdf --- web/app.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/web/app.js b/web/app.js index a57c8a5dfa456..c2ddd2b48d604 100644 --- a/web/app.js +++ b/web/app.js @@ -1652,7 +1652,7 @@ const PDFViewerApplication = { async _initializeAutoPrint(pdfDocument, openActionPromise) { const [openAction, javaScript] = await Promise.all([ openActionPromise, - pdfDocument.getJavaScript(), + !AppOptions.get("enableScripting") ? pdfDocument.getJavaScript() : null, ]); if (pdfDocument !== this.pdfDocument) { @@ -1660,10 +1660,10 @@ const PDFViewerApplication = { } let triggerAutoPrint = false; - if (openAction && openAction.action === "Print") { + if (openAction?.action === "Print") { triggerAutoPrint = true; } - if (javaScript && !AppOptions.get("enableScripting")) { + if (javaScript) { javaScript.some(js => { if (!js) { // Don't warn/fallback for empty JavaScript actions. @@ -1686,9 +1686,7 @@ const PDFViewerApplication = { } if (triggerAutoPrint) { - setTimeout(() => { - this.triggerPrinting(); - }); + this.triggerPrinting(); } },