优化支付耗时和日志
This commit is contained in:
@@ -23,7 +23,7 @@ const htmlPath = getArg('--html');
|
||||
const goodsUrl = getArg('--goods-url');
|
||||
const cookiePath = getArg('--cookies');
|
||||
const outputPath = getArg('--output');
|
||||
const waitMs = Number.parseInt(getArg('--wait', '12000'), 10);
|
||||
const waitMs = Number.parseInt(getArg('--wait', '5000'), 10);
|
||||
const useArchivedAssets = argv.includes('--archived-assets');
|
||||
|
||||
function fail(message) {
|
||||
@@ -61,8 +61,18 @@ class GoodsLoader extends ResourceLoader {
|
||||
|
||||
const session = cookiePath ? JSON.parse(fs.readFileSync(cookiePath, 'utf8')) : {};
|
||||
const cookies = session.cookies || {};
|
||||
const captured = [];
|
||||
const resourceErrors = [];
|
||||
let capturedFp = null;
|
||||
let resolveFpCapture = null;
|
||||
const startedAt = Date.now();
|
||||
|
||||
function captureFp(url, body) {
|
||||
const values = Object.fromEntries(new URLSearchParams(body));
|
||||
if (!values.SessionID || !values.DeviceFP) return;
|
||||
capturedFp = { url, body };
|
||||
if (resolveFpCapture) resolveFpCapture();
|
||||
}
|
||||
|
||||
const dom = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
|
||||
url: goodsUrl,
|
||||
referrer: 'https://z.iwan.yyb.qq.com/',
|
||||
@@ -88,7 +98,7 @@ const dom = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
|
||||
send(body) {
|
||||
const requestBody = String(body || '');
|
||||
if (this.__yybUrl.includes('fp-behv.fcg')) {
|
||||
captured.push({ url: this.__yybUrl, body: requestBody });
|
||||
captureFp(this.__yybUrl, requestBody);
|
||||
}
|
||||
this.readyState = 4;
|
||||
this.status = 200;
|
||||
@@ -103,18 +113,23 @@ const dom = new JSDOM(fs.readFileSync(htmlPath, 'utf8'), {
|
||||
},
|
||||
});
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, Number.isFinite(waitMs) ? waitMs : 12000));
|
||||
const fp = captured.at(-1);
|
||||
if (!fp) {
|
||||
const timeoutMs = Number.isFinite(waitMs) && waitMs > 0 ? waitMs : 5000;
|
||||
let timeoutId;
|
||||
await Promise.race([
|
||||
new Promise(resolve => {
|
||||
resolveFpCapture = resolve;
|
||||
if (capturedFp) resolve();
|
||||
}),
|
||||
new Promise(resolve => { timeoutId = setTimeout(resolve, timeoutMs); }),
|
||||
]);
|
||||
clearTimeout(timeoutId);
|
||||
if (!capturedFp) {
|
||||
dom.window.close();
|
||||
fail(`未生成 fp-behv;加载错误: ${resourceErrors.slice(0, 3).join(' | ') || '无'}`);
|
||||
}
|
||||
|
||||
const fp = capturedFp;
|
||||
const values = Object.fromEntries(new URLSearchParams(fp.body));
|
||||
if (!values.SessionID || !values.DeviceFP) {
|
||||
dom.window.close();
|
||||
fail('fp-behv 缺少 SessionID 或 DeviceFP');
|
||||
}
|
||||
const result = {
|
||||
generated_at: new Date().toISOString(),
|
||||
goods_url: goodsUrl,
|
||||
@@ -130,3 +145,4 @@ dom.window.close();
|
||||
console.log(`DeviceFP 已保存: ${outputPath}`);
|
||||
console.log(`SessionID: ${result.session_id}`);
|
||||
console.log(`DeviceFP 长度: ${result.device_fp_length}`);
|
||||
console.log(`DeviceFP 捕获耗时: ${Date.now() - startedAt}ms`);
|
||||
|
||||
Reference in New Issue
Block a user