From 64e8557fb52bb7fceac9b5e98dca225a1875ec27 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 1 Aug 2023 09:02:05 +0200 Subject: [PATCH] [api-minor] Deprecate the `PDFDocumentProxy.getJavaScript` method This method is very old, however with the exception of the auto-print hack (when scripting is disabled) in the viewer it's never actually been used. Most likely the idea with `PDFDocumentProxy.getJavaScript` was that it'd be useful if scripting support was added, however it turned out that it was a bit too simplistic and instead a number of new methods were added for the scripting use-cases. --- src/core/catalog.js | 19 ++++++------------- src/core/worker.js | 4 ---- src/display/api.js | 35 +++++++++++++++++++++-------------- test/unit/api_spec.js | 11 +++-------- web/app.js | 31 ++++++++++++------------------- 5 files changed, 42 insertions(+), 58 deletions(-) diff --git a/src/core/catalog.js b/src/core/catalog.js index 32e3b6477..51537cabf 100644 --- a/src/core/catalog.js +++ b/src/core/catalog.js @@ -987,7 +987,10 @@ class Catalog { return; } js = stringToPDFString(js).replaceAll("\x00", ""); - (javaScript ||= new Map()).set(name, js); + // Skip empty entries, similar to the `_collectJS` function. + if (js) { + (javaScript ||= new Map()).set(name, js); + } } if (obj instanceof Dict && obj.has("JavaScript")) { @@ -1005,15 +1008,6 @@ class Catalog { return javaScript; } - get javaScript() { - const javaScript = this._collectJavaScript(); - return shadow( - this, - "javaScript", - javaScript ? [...javaScript.values()] : null - ); - } - get jsActions() { const javaScript = this._collectJavaScript(); let actions = collectActions( @@ -1023,9 +1017,8 @@ class Catalog { ); if (javaScript) { - if (!actions) { - actions = Object.create(null); - } + actions ||= Object.create(null); + for (const [key, val] of javaScript) { if (key in actions) { actions[key].push(val); diff --git a/src/core/worker.js b/src/core/worker.js index 04dd77e80..9230652ce 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -463,10 +463,6 @@ class WorkerMessageHandler { return pdfManager.ensureCatalog("attachments"); }); - handler.on("GetJavaScript", function (data) { - return pdfManager.ensureCatalog("javaScript"); - }); - handler.on("GetDocJSActions", function (data) { return pdfManager.ensureCatalog("jsActions"); }); diff --git a/src/display/api.js b/src/display/api.js index 61a95bef6..913752d78 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -760,6 +760,26 @@ class PDFDocumentProxy { this._pdfInfo = pdfInfo; this._transport = transport; + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { + Object.defineProperty(this, "getJavaScript", { + value: () => { + deprecated( + "`PDFDocumentProxy.getJavaScript`, " + + "please use `PDFDocumentProxy.getJSActions` instead." + ); + return this.getJSActions().then(js => { + if (!js) { + return js; + } + const jsArr = []; + for (const name in js) { + jsArr.push(...js[name]); + } + return jsArr; + }); + }, + }); + } if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { // For testing purposes. Object.defineProperty(this, "getXFADatasets", { @@ -917,19 +937,10 @@ class PDFDocumentProxy { return this._transport.getAttachments(); } - /** - * @returns {Promise | null>} A promise that is resolved with - * an {Array} of all the JavaScript strings in the name tree, or `null` - * if no JavaScript exists. - */ - getJavaScript() { - return this._transport.getJavaScript(); - } - /** * @returns {Promise} A promise that is resolved with * an {Object} with the JavaScript actions: - * - from the name tree (like getJavaScript); + * - from the name tree. * - from A or AA entries in the catalog dictionary. * , or `null` if no JavaScript exists. */ @@ -3016,10 +3027,6 @@ class WorkerTransport { return this.messageHandler.sendWithPromise("GetAttachments", null); } - getJavaScript() { - return this.messageHandler.sendWithPromise("GetJavaScript", null); - } - getDocJSActions() { return this.messageHandler.sendWithPromise("GetDocJSActions", null); } diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 3b42e32e1..ac68f80aa 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -1358,21 +1358,16 @@ describe("api", function () { await loadingTask.destroy(); }); - it("gets javascript", async function () { - const javascript = await pdfDocument.getJavaScript(); - expect(javascript).toEqual(null); - }); - it("gets javascript with printing instructions (JS action)", async function () { // PDF document with "JavaScript" action in the OpenAction dictionary. const loadingTask = getDocument(buildGetDocumentParams("issue6106.pdf")); const pdfDoc = await loadingTask.promise; - const javascript = await pdfDoc.getJavaScript(); + const { OpenAction } = await pdfDoc.getJSActions(); - expect(javascript).toEqual([ + expect(OpenAction).toEqual([ "this.print({bUI:true,bSilent:false,bShrinkToFit:true});", ]); - expect(javascript[0]).toMatch(AutoPrintRegExp); + expect(OpenAction[0]).toMatch(AutoPrintRegExp); await loadingTask.destroy(); }); diff --git a/web/app.js b/web/app.js index b3a1cf055..8053e6381 100644 --- a/web/app.js +++ b/web/app.js @@ -1543,9 +1543,9 @@ const PDFViewerApplication = { * @private */ async _initializeAutoPrint(pdfDocument, openActionPromise) { - const [openAction, javaScript] = await Promise.all([ + const [openAction, jsActions] = await Promise.all([ openActionPromise, - !this.pdfViewer.enableScripting ? pdfDocument.getJavaScript() : null, + !this.pdfViewer.enableScripting ? pdfDocument.getJSActions() : null, ]); if (pdfDocument !== this.pdfDocument) { @@ -1556,25 +1556,18 @@ const PDFViewerApplication = { if (openAction?.action === "Print") { triggerAutoPrint = true; } - if (javaScript) { - javaScript.some(js => { - if (!js) { - // Don't warn/fallback for empty JavaScript actions. - return false; - } - console.warn("Warning: JavaScript support is not enabled"); - return true; - }); - - if (!triggerAutoPrint) { - // Hack to support auto printing. - for (const js of javaScript) { - if (js && AutoPrintRegExp.test(js)) { - triggerAutoPrint = true; - break; - } + if (jsActions) { + for (const name in jsActions) { + if (jsActions[name]) { + console.warn("Warning: JavaScript support is not enabled"); + break; } } + + // Hack to support auto printing. + triggerAutoPrint ||= !!( + jsActions.OpenAction && AutoPrintRegExp.test(jsActions.OpenAction) + ); } if (triggerAutoPrint) {