reverse-name/node_run/test.js

73 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-03-18 22:42:56 +08:00
const {
JSDOM
} = require('jsdom');
const jsdom = require('jsdom');
2024-03-18 21:10:19 +08:00
const fs = require('fs');
const path = require('path');
2024-03-18 22:42:56 +08:00
async function test(str) {
2024-03-18 21:10:19 +08:00
const localStorageMock = {
setItem: function (key, value) {
2024-03-18 22:42:56 +08:00
this[key] = value;
2024-03-18 21:10:19 +08:00
},
getItem: function (key) {
2024-03-18 22:42:56 +08:00
return this[key] || null;
2024-03-18 21:10:19 +08:00
},
removeItem: function (key) {
2024-03-18 22:42:56 +08:00
delete this[key];
2024-03-18 21:10:19 +08:00
},
clear: function () {
2024-03-18 22:42:56 +08:00
for (const key in this) {
if (this.hasOwnProperty(key) && key !== 'setItem' && key !== 'getItem' && key !== 'removeItem' && key !== 'clear') {
delete this[key];
}
2024-03-18 21:10:19 +08:00
}
}
};
// 在你的 JSDOM 初始化代码之前
let resolvePromise;
// 创建一个 Promise它会在 window.resolve 被调用时解决
const resolver = new Promise(resolve => {
resolvePromise = resolve;
});
const virtualConsole = new jsdom.VirtualConsole();
const dom = new JSDOM(fs.readFileSync(path.join(__dirname, 'static/md5.html'), 'utf-8'), {
url: 'file://' + path.join(__dirname, 'static/'),
2024-03-18 22:42:56 +08:00
console: virtualConsole,
2024-03-18 21:10:19 +08:00
runScripts: 'dangerously',
resources: 'usable',
beforeParse(window) {
window.FakelocalStorage = localStorageMock;
2024-03-18 22:42:56 +08:00
window.config = [{
2024-03-19 20:28:38 +08:00
"count": 10000,
2024-03-18 22:42:56 +08:00
"score": 10
}];
2024-03-18 21:10:19 +08:00
window.name_input = str;
window.stage = 0;
window.skillData = [];
window.resolve = (...args) => {
2024-03-18 22:35:48 +08:00
console.log(args);
resolvePromise(args);
2024-03-18 21:10:19 +08:00
window.close();
2024-03-18 22:40:41 +08:00
};
window.output = (...message) => {
2024-03-18 22:55:48 +08:00
console.log('page:', message);
2024-03-18 22:40:41 +08:00
};
2024-03-18 21:10:19 +08:00
}
});
virtualConsole.on('log', (message) => {
2024-03-18 22:55:48 +08:00
console.log('page:', message);
2024-03-18 21:10:19 +08:00
});
const result = await resolver
2024-03-18 22:35:48 +08:00
console.log(result)
2024-03-18 21:10:19 +08:00
}
//str="!test!\n!\n\n11@qwerb\n\n1233";
2024-03-20 13:28:40 +08:00
str="11@qwerb\n\n1233";
test(str)