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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import browser from 'webextension-polyfill';
import type { UserlandBrowser } from './routes/userland';
console.debug('Loading Extension');
setTimeout(async () => {
const { sc } = await browser.storage.local.get('sc');
if (typeof sc === 'undefined') {
console.warn('No value set!');
} else if (typeof sc !== 'string') {
throw new Error('typeof sc is not str nor undefined');
} else {
console.debug('Found Script (length', sc.length, ':3)');
console.debug('Loading esbuild-wasm');
const { initialize, transform } = await import('esbuild-wasm').catch(
(e) => {
console.error('Failed to load esbuild-wasm!');
throw e;
}
);
console.debug("Loading esbuild's WASM Module");
const wasmModule = new WebAssembly.Module(
await fetch('./generated/esbuild.wasm')
.catch((e) => {
console.error('Failed to fetch() the esbuild wasm file.');
throw e;
})
.then((v) => v.arrayBuffer())
);
console.debug('Attempting to initialize esbuild-wasm');
try {
await initialize({
wasmModule,
worker: false,
});
} catch (error) {
console.error(error);
throw new Error('Failed to initialize esbuild - see above error');
}
console.debug('Transforming Content');
const built = await transform(sc, {
loader: 'ts',
});
console.debug('Got result', built);
const b = browser;
const brow: UserlandBrowser = {
get webRequest() {
return b.webRequest;
},
};
const f = new Function(
'browser',
built.code.includes('await')
? `return (async()=>{${built.code}
})();`
: built.code
);
console.debug('Environment Information:', {
func: f,
browser: brow,
realBrowser: b,
});
(globalThis as any).browser = brow;
console.debug('Evaluating...');
await f(brow);
console.debug('Function Exited');
}
}, 0);
|