|
| 1 | +import * as CSInterface from "./v9"; |
| 2 | + |
| 3 | +export function evalScript(script: string, callback: (result?: any) => void): Promise<any> { |
| 4 | + /** |
| 5 | + * |
| 6 | + * @param {*} value |
| 7 | + */ |
| 8 | + function escape(value: undefined | string | number | object | boolean): any { |
| 9 | + |
| 10 | + switch (typeof value) { |
| 11 | + case 'undefined': |
| 12 | + return '"null"'; |
| 13 | + case 'string': |
| 14 | + return JSON.stringify(value); |
| 15 | + case 'number': { |
| 16 | + |
| 17 | + if (Number.isNaN(value) || Infinity === value) { |
| 18 | + return 0; |
| 19 | + } |
| 20 | + |
| 21 | + return value; |
| 22 | + } |
| 23 | + case 'object': { |
| 24 | + if (value === null) { |
| 25 | + return '"null"'; |
| 26 | + } |
| 27 | + |
| 28 | + return escape(JSON.stringify(value)); |
| 29 | + } |
| 30 | + case 'boolean': { |
| 31 | + return value; |
| 32 | + } |
| 33 | + default: |
| 34 | + throw Error(`Nicht unterstützter Typ "${typeof value}"`); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * Versucht eine Funktion oder einen Befehl auszuführen. |
| 41 | + * |
| 42 | + * Wird mehr als ein Parameter übergeben, ist der erste Parameter der Funktionsname |
| 43 | + * und alle weiteren Parameter werden jeweils als JSON-String hinzugefügt. |
| 44 | + * |
| 45 | + * @param {...any} |
| 46 | + * |
| 47 | + * @return {Promise<any>} |
| 48 | + */ |
| 49 | + |
| 50 | + const args = Array.from(arguments); |
| 51 | + |
| 52 | + // Hole den Befehl oder den Funktionsnamen |
| 53 | + let eval_string = args.shift().trim(); |
| 54 | + |
| 55 | + // Der Nutzer hat mehr als 1 Parameter angegeben, |
| 56 | + // a) Entweder eine callback, wenn du Funktion wie die original Function von CSInterface verwendet wird |
| 57 | + // b) Andere Typen wenn die Funktion automatisch die Parameter einer Funktion escapen soll |
| 58 | + let user_callback: any = null; |
| 59 | + if (args.length > 0) { |
| 60 | + |
| 61 | + if (typeof args[0] === 'function') { |
| 62 | + // a) |
| 63 | + user_callback = args.shift(); |
| 64 | + |
| 65 | + } else { |
| 66 | + // b) |
| 67 | + |
| 68 | + // Prüfe ob der Nutzer vielleicht bereits die runden Klammern "()" angegeben hat und entferne die |
| 69 | + if (eval_string.length >= 3 && eval_string.substr(-3) === '();') { |
| 70 | + eval_string = eval_string.substr(0, eval_string.length - 3); |
| 71 | + } else if (eval_string.length >= 2 && eval_string.substr(-2) === '()') { |
| 72 | + eval_string = eval_string.substr(0, eval_string.length - 2); |
| 73 | + } |
| 74 | + |
| 75 | + eval_string += `(${args.map(param => escape(param)).join(', ')});`; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return new Promise((resolve, reject) => { |
| 80 | + |
| 81 | + // Ermöglicht es eine genauere Fehlermeldung zu erhalten ("e" besitzt noch folgende Eigenschaften: start, end, source, fileName, number) |
| 82 | + // Von der Fehlerzeile müssen wir 1 abziehen, da der Try/Catch-Block mitgezählt wird |
| 83 | + const script = `try { |
| 84 | + ${eval_string} |
| 85 | + } catch(e) { |
| 86 | + '{"error": "' + e.name + '", "message": "' + e.message.replace(/"/g, \"'\") + '", "line": "' + (e.line ? e.line - 1: -1) + '", "stack": "' + (e.stack ? e.stack.replace(/"/g, \"'\") : \"\") + '"}' |
| 87 | + }`; |
| 88 | + |
| 89 | + window.__adobe_cep__.evalScript(script, (result: any) => { |
| 90 | + |
| 91 | + // Wenn der Nutzer eine eigene Callback angegeben hat (evalScript legacy support) |
| 92 | + if (user_callback) { |
| 93 | + return resolve(user_callback(result) || result); |
| 94 | + } |
| 95 | + |
| 96 | + if (typeof result === 'string' && result === CSInterface.EvalScript_ErrMessage) { |
| 97 | + return reject(result); |
| 98 | + } |
| 99 | + |
| 100 | + if (typeof result === 'string' && result.length > 0) { |
| 101 | + try { |
| 102 | + const obj = JSON.parse(result); |
| 103 | + |
| 104 | + if (typeof obj.error === 'string') { |
| 105 | + return reject(obj); |
| 106 | + } |
| 107 | + |
| 108 | + return resolve(JSON.parse(result)); |
| 109 | + } catch (e) { |
| 110 | + // Ignoriere Parse Error und gebe den String zurück |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return resolve(result); |
| 115 | + }); |
| 116 | + }); |
| 117 | +} |
| 118 | + |
0 commit comments