mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 10:08:06 +02:00
JS - Add the basic architecture to be able to execute embedded js
This commit is contained in:
parent
a373137304
commit
e76a96892a
18 changed files with 823 additions and 0 deletions
59
web/app.js
59
web/app.js
|
@ -19,6 +19,7 @@ import {
|
|||
AutoPrintRegExp,
|
||||
DEFAULT_SCALE_VALUE,
|
||||
EventBus,
|
||||
generateRandomStringForSandbox,
|
||||
getActiveOrFocusedElement,
|
||||
getPDFFileNameFromURL,
|
||||
isValidRotation,
|
||||
|
@ -179,6 +180,10 @@ class DefaultExternalServices {
|
|||
static get isInAutomation() {
|
||||
return shadow(this, "isInAutomation", false);
|
||||
}
|
||||
|
||||
static get scripting() {
|
||||
throw new Error("Not implemented: scripting");
|
||||
}
|
||||
}
|
||||
|
||||
const PDFViewerApplication = {
|
||||
|
@ -1333,6 +1338,60 @@ const PDFViewerApplication = {
|
|||
|
||||
this._initializePageLabels(pdfDocument);
|
||||
this._initializeMetadata(pdfDocument);
|
||||
this._initializeJavaScript(pdfDocument);
|
||||
},
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
async _initializeJavaScript(pdfDocument) {
|
||||
if (!AppOptions.get("enableScripting")) {
|
||||
return;
|
||||
}
|
||||
const objects = await pdfDocument.getFieldObjects();
|
||||
const scripting = this.externalServices.scripting;
|
||||
|
||||
window.addEventListener("updateFromSandbox", function (event) {
|
||||
const detail = event.detail;
|
||||
const id = detail.id;
|
||||
if (!id) {
|
||||
switch (detail.command) {
|
||||
case "println":
|
||||
console.log(detail.value);
|
||||
break;
|
||||
case "clear":
|
||||
console.clear();
|
||||
break;
|
||||
case "alert":
|
||||
// eslint-disable-next-line no-alert
|
||||
window.alert(detail.value);
|
||||
break;
|
||||
case "error":
|
||||
console.error(detail.value);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const element = document.getElementById(id);
|
||||
if (element) {
|
||||
element.dispatchEvent(new CustomEvent("updateFromSandbox", { detail }));
|
||||
} else {
|
||||
const value = detail.value;
|
||||
if (value !== undefined && value !== null) {
|
||||
// the element hasn't been rendered yet so use annotation storage
|
||||
pdfDocument.annotationStorage.setValue(id, detail.value);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener("dispatchEventInSandbox", function (event) {
|
||||
scripting.dispatchEventInSandbox(event.detail);
|
||||
});
|
||||
|
||||
const dispatchEventName = generateRandomStringForSandbox(objects);
|
||||
const calculationOrder = [];
|
||||
scripting.createSandbox({ objects, dispatchEventName, calculationOrder });
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue