koishi-plugin-maichuni-scorehelper 0.0.13-test → 0.0.14-test
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -304,13 +304,17 @@ class MaimaiStatus extends koishi_1.Service {
|
|
|
304
304
|
this.clientLogger.warn(`Heartbeat JSON parse failed: ${err.message}; snippet=${snippet}`);
|
|
305
305
|
// 如果返回的是 CC HTML,尝试使用 Puppeteer 绕过
|
|
306
306
|
if (this.ctx.puppeteer && snippet.includes('<!DOCTYPE')) {
|
|
307
|
+
let page = null;
|
|
307
308
|
try {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
await page.
|
|
309
|
+
page = await this.ctx.puppeteer.page();
|
|
310
|
+
if (page.setUserAgent) {
|
|
311
|
+
await page.setUserAgent(this.UA);
|
|
312
|
+
}
|
|
313
|
+
await page.goto(this.API_URL, { waitUntil: 'networkidle0', timeout: 15000 });
|
|
314
|
+
const body = await page.evaluate(() => {
|
|
315
|
+
const text = document.body?.innerText || document.body?.textContent || '';
|
|
316
|
+
return text;
|
|
317
|
+
});
|
|
314
318
|
try {
|
|
315
319
|
data = JSON.parse(body);
|
|
316
320
|
}
|
|
@@ -323,6 +327,14 @@ class MaimaiStatus extends koishi_1.Service {
|
|
|
323
327
|
this.clientLogger.warn(`Puppeteer heartbeat fetch failed: ${pe.message}`);
|
|
324
328
|
return null;
|
|
325
329
|
}
|
|
330
|
+
finally {
|
|
331
|
+
if (page) {
|
|
332
|
+
try {
|
|
333
|
+
await page.close();
|
|
334
|
+
}
|
|
335
|
+
catch { /* ignore */ }
|
|
336
|
+
}
|
|
337
|
+
}
|
|
326
338
|
}
|
|
327
339
|
else {
|
|
328
340
|
return null;
|
package/package.json
CHANGED
|
@@ -360,28 +360,36 @@ export class MaimaiStatus extends Service {
|
|
|
360
360
|
} catch (err) {
|
|
361
361
|
const snippet = data.slice(0, 200)
|
|
362
362
|
this.clientLogger.warn(`Heartbeat JSON parse failed: ${(err as any).message}; snippet=${snippet}`)
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
const page = await this.ctx.puppeteer.page()
|
|
367
|
-
const body = await page.evaluate(async (url: string, ua: string) => {
|
|
368
|
-
const res = await fetch(url, { headers: { 'User-Agent': ua } })
|
|
369
|
-
return await res.text()
|
|
370
|
-
}, this.API_URL, this.UA)
|
|
371
|
-
await page.close()
|
|
363
|
+
// 如果返回的是 CC HTML,尝试使用 Puppeteer 绕过
|
|
364
|
+
if (this.ctx.puppeteer && snippet.includes('<!DOCTYPE')) {
|
|
365
|
+
let page: any = null
|
|
372
366
|
try {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
367
|
+
page = await this.ctx.puppeteer.page()
|
|
368
|
+
if (page.setUserAgent) {
|
|
369
|
+
await page.setUserAgent(this.UA)
|
|
370
|
+
}
|
|
371
|
+
await page.goto(this.API_URL, { waitUntil: 'networkidle0', timeout: 15000 })
|
|
372
|
+
const body = await page.evaluate(() => {
|
|
373
|
+
const text = document.body?.innerText || document.body?.textContent || ''
|
|
374
|
+
return text
|
|
375
|
+
})
|
|
376
|
+
try {
|
|
377
|
+
data = JSON.parse(body)
|
|
378
|
+
} catch (err2) {
|
|
379
|
+
this.clientLogger.warn(`Puppeteer heartbeat parse failed: ${(err2 as any).message}; body=${body.slice(0,200)}`)
|
|
380
|
+
return null
|
|
381
|
+
}
|
|
382
|
+
} catch (pe) {
|
|
383
|
+
this.clientLogger.warn(`Puppeteer heartbeat fetch failed: ${(pe as any).message}`)
|
|
376
384
|
return null
|
|
385
|
+
} finally {
|
|
386
|
+
if (page) {
|
|
387
|
+
try { await page.close() } catch { /* ignore */ }
|
|
388
|
+
}
|
|
377
389
|
}
|
|
378
|
-
}
|
|
379
|
-
this.clientLogger.warn(`Puppeteer heartbeat fetch failed: ${(pe as any).message}`)
|
|
390
|
+
} else {
|
|
380
391
|
return null
|
|
381
392
|
}
|
|
382
|
-
} else {
|
|
383
|
-
return null
|
|
384
|
-
}
|
|
385
393
|
}
|
|
386
394
|
}
|
|
387
395
|
|