[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.
This commit is contained in:
Jonas Jenwald 2023-08-01 09:02:05 +02:00
parent 5b8f680480
commit 64e8557fb5
5 changed files with 42 additions and 58 deletions

View File

@ -987,7 +987,10 @@ class Catalog {
return; return;
} }
js = stringToPDFString(js).replaceAll("\x00", ""); 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")) { if (obj instanceof Dict && obj.has("JavaScript")) {
@ -1005,15 +1008,6 @@ class Catalog {
return javaScript; return javaScript;
} }
get javaScript() {
const javaScript = this._collectJavaScript();
return shadow(
this,
"javaScript",
javaScript ? [...javaScript.values()] : null
);
}
get jsActions() { get jsActions() {
const javaScript = this._collectJavaScript(); const javaScript = this._collectJavaScript();
let actions = collectActions( let actions = collectActions(
@ -1023,9 +1017,8 @@ class Catalog {
); );
if (javaScript) { if (javaScript) {
if (!actions) { actions ||= Object.create(null);
actions = Object.create(null);
}
for (const [key, val] of javaScript) { for (const [key, val] of javaScript) {
if (key in actions) { if (key in actions) {
actions[key].push(val); actions[key].push(val);

View File

@ -463,10 +463,6 @@ class WorkerMessageHandler {
return pdfManager.ensureCatalog("attachments"); return pdfManager.ensureCatalog("attachments");
}); });
handler.on("GetJavaScript", function (data) {
return pdfManager.ensureCatalog("javaScript");
});
handler.on("GetDocJSActions", function (data) { handler.on("GetDocJSActions", function (data) {
return pdfManager.ensureCatalog("jsActions"); return pdfManager.ensureCatalog("jsActions");
}); });

View File

@ -760,6 +760,26 @@ class PDFDocumentProxy {
this._pdfInfo = pdfInfo; this._pdfInfo = pdfInfo;
this._transport = transport; 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")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
// For testing purposes. // For testing purposes.
Object.defineProperty(this, "getXFADatasets", { Object.defineProperty(this, "getXFADatasets", {
@ -917,19 +937,10 @@ class PDFDocumentProxy {
return this._transport.getAttachments(); return this._transport.getAttachments();
} }
/**
* @returns {Promise<Array<string> | 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<Object | null>} A promise that is resolved with * @returns {Promise<Object | null>} A promise that is resolved with
* an {Object} with the JavaScript actions: * 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. * - from A or AA entries in the catalog dictionary.
* , or `null` if no JavaScript exists. * , or `null` if no JavaScript exists.
*/ */
@ -3016,10 +3027,6 @@ class WorkerTransport {
return this.messageHandler.sendWithPromise("GetAttachments", null); return this.messageHandler.sendWithPromise("GetAttachments", null);
} }
getJavaScript() {
return this.messageHandler.sendWithPromise("GetJavaScript", null);
}
getDocJSActions() { getDocJSActions() {
return this.messageHandler.sendWithPromise("GetDocJSActions", null); return this.messageHandler.sendWithPromise("GetDocJSActions", null);
} }

View File

@ -1358,21 +1358,16 @@ describe("api", function () {
await loadingTask.destroy(); 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 () { it("gets javascript with printing instructions (JS action)", async function () {
// PDF document with "JavaScript" action in the OpenAction dictionary. // PDF document with "JavaScript" action in the OpenAction dictionary.
const loadingTask = getDocument(buildGetDocumentParams("issue6106.pdf")); const loadingTask = getDocument(buildGetDocumentParams("issue6106.pdf"));
const pdfDoc = await loadingTask.promise; 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});", "this.print({bUI:true,bSilent:false,bShrinkToFit:true});",
]); ]);
expect(javascript[0]).toMatch(AutoPrintRegExp); expect(OpenAction[0]).toMatch(AutoPrintRegExp);
await loadingTask.destroy(); await loadingTask.destroy();
}); });

View File

@ -1543,9 +1543,9 @@ const PDFViewerApplication = {
* @private * @private
*/ */
async _initializeAutoPrint(pdfDocument, openActionPromise) { async _initializeAutoPrint(pdfDocument, openActionPromise) {
const [openAction, javaScript] = await Promise.all([ const [openAction, jsActions] = await Promise.all([
openActionPromise, openActionPromise,
!this.pdfViewer.enableScripting ? pdfDocument.getJavaScript() : null, !this.pdfViewer.enableScripting ? pdfDocument.getJSActions() : null,
]); ]);
if (pdfDocument !== this.pdfDocument) { if (pdfDocument !== this.pdfDocument) {
@ -1556,25 +1556,18 @@ const PDFViewerApplication = {
if (openAction?.action === "Print") { if (openAction?.action === "Print") {
triggerAutoPrint = true; triggerAutoPrint = true;
} }
if (javaScript) { if (jsActions) {
javaScript.some(js => { for (const name in jsActions) {
if (!js) { if (jsActions[name]) {
// Don't warn/fallback for empty JavaScript actions. console.warn("Warning: JavaScript support is not enabled");
return false; break;
}
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;
}
} }
} }
// Hack to support auto printing.
triggerAutoPrint ||= !!(
jsActions.OpenAction && AutoPrintRegExp.test(jsActions.OpenAction)
);
} }
if (triggerAutoPrint) { if (triggerAutoPrint) {