forked from pysunday/sdenv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (40 loc) · 1.48 KB
/
index.js
File metadata and controls
46 lines (40 loc) · 1.48 KB
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
try{require('module-alias')()}catch(err){};
const fs = require('fs');
const path = require('path');
const { Script } = require("vm");
const logger = require('sdenv/utils/logger');
const { jsdomFromText } = require('sdenv');
const baseUrl = "https://wcjs.sbj.cnipa.gov.cn"
const files = {
// 此处的文件可以通过运行npx rs-reverse makecode tarurl自动生成
html: path.resolve(__dirname, 'output/makecode_input_html.html'),
js: path.resolve(__dirname, 'output/makecode_input_js.js'),
ts: path.resolve(__dirname, 'output/makecode_input_ts.json'),
}
function getFile(name) {
const filepath = files[name];
if (!filepath) throw new Error(`getFile: ${name}错误`);
if (!fs.existsSync(filepath)) throw new Error(`文件${filepath}不存在,请使用rs-reverse工具先获取文件`);
return fs.readFileSync(filepath);
}
function initBrowser(window, cookieJar) {
window.$_ts = JSON.parse(getFile('ts'));
window.addEventListener('sdenv:location.replace', (e) => {
const cookies = cookieJar.getCookieStringSync(baseUrl);
logger.debug('生成cookie:', cookies);
window.close();
})
}
async function loadPages() {
const htmltext = getFile('html');
const jstext = getFile('js');
const dom = jsdomFromText(htmltext, {
url: `${baseUrl}/sgtmi`,
referrer: `${baseUrl}/sgtmi`,
contentType: "text/html",
runScripts: "outside-only",
})
initBrowser(dom.window, dom.cookieJar);
new Script(jstext).runInContext(dom.getInternalVMContext());
}
loadPages()