-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeShell.ts
37 lines (30 loc) · 1.13 KB
/
codeShell.ts
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
const code = `(function() {
globalThis.codeShell = function(code, env = Object.create(null), p = {}) {
if(p.insulate ?? true) {
env = new Proxy(env, {
has: () => true,
get: (target, key, receiver) => key === Symbol.unscopables ? void 0 : Reflect.get(target, key, receiver)
});
}
return eval(\`with(env) { (\${p.async ? 'async ':''}function\${p.generator ? '* ':''}(\${p.arguments || ''}) { \${p.strict ?? true ? "'use strict'; " : ''}\${code} }); } //# sourceURL=\${p.source || 'code'}\`);
}
})();`;
let script: any = document.createElement('script');
script.textContent = code;
document.head.append(script);
script.remove();
script = null;
interface codeShell {
<T extends (this: any, ...args: any[]) => any = () => void>(code: string, env?: object, p?: {
strict?: boolean;
async?: boolean;
generator?: boolean;
arguments?: string;
insulate?: boolean;
source?: string;
}): T;
from: (code: (...args: any[]) => any) => string;
}
export let codeShell: codeShell = (globalThis as any).codeShell as codeShell;
delete (globalThis as any).codeShell;
codeShell.from = code => code.toString().replace(/^function.+?\{(.*)\}$/s, '$1');