/
evaluate-runner.ts
47 lines (39 loc) 路 1.4 KB
/
evaluate-runner.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
38
39
40
41
42
43
44
45
46
47
import * as vm from 'vm';
import * as jsdom from 'jsdom/lib/old-api';
import * as jsdomutils from 'jsdom/lib/jsdom/living/generated/utils';
const run = (data) => {
const { source } = data;
const result = {
error: null,
evaluate: 'result'
};
const url = process.argv[2];
const waitFor = process.argv[3];
jsdom.env({
done: (error, window) => {
if (error) {
result.error = error;
return process.send(result);
}
/* Even though `done()` is called after window.onload (so all resoruces and scripts executed),
we might want to wait a few seconds if the site is lazy loading something. */
return setTimeout(async () => {
try {
const script: vm.Script = new vm.Script(source);
const evaluteResult = await script.runInContext(jsdomutils.implForWrapper(window.document)._global);
result.evaluate = evaluteResult;
} catch (err) {
result.error = err;
}
process.send(result);
}, waitFor);
},
features: {
FetchExternalResources: ['script', 'link', 'img'],
ProcessExternalResources: ['script'],
SkipExternalResources: false
},
url
});
};
process.on('message', run);