forked from KNXCloud/lowcode-engine-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ts
More file actions
47 lines (37 loc) · 965 Bytes
/
script.ts
File metadata and controls
47 lines (37 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { createDefer } from './create-defer';
export function evaluate(script: string) {
const scriptEl = document.createElement('script');
scriptEl.text = script;
document.head.appendChild(scriptEl);
document.head.removeChild(scriptEl);
}
export function load(url: string) {
const node: any = document.createElement('script');
node.onload = onload;
node.onerror = onload;
const i = createDefer();
function onload(e: any) {
node.onload = null;
node.onerror = null;
if (e.type === 'load') {
i.resolve();
} else {
i.reject();
}
}
node.src = url;
node.async = false;
document.head.appendChild(node);
return i.promise();
}
export function evaluateExpression(expr: string) {
return new Function(expr)();
}
export function newFunction(args: string, code: string) {
try {
return new Function(args, code);
} catch (e) {
console.warn('Caught error, Cant init func');
return null;
}
}