_check_console.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const fs = require('fs');
  2. const { chromium } = require('playwright-core');
  3. const OUT = 'd:\\AI文件夹\\预算报价\\_console_result.json';
  4. const EDGE = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe';
  5. const URL_ = 'http://127.0.0.1:8931/AI快速报价_Demo2.optimizing.html';
  6. (async () => {
  7. const logs = { console: [], pageerror: [], requestfailed: [] };
  8. const profileDir = 'd:\\AI文件夹\\预算报价\\.edge-console-' + Date.now();
  9. const ctx = await chromium.launchPersistentContext(profileDir, {
  10. executablePath: EDGE,
  11. headless: true,
  12. args: ['--no-sandbox', '--disable-gpu', '--disable-breakpad', '--disable-crash-reporter']
  13. });
  14. const page = ctx.pages()[0] || await ctx.newPage();
  15. page.on('console', msg => {
  16. logs.console.push({ type: msg.type(), text: msg.text().slice(0, 500) });
  17. });
  18. page.on('pageerror', err => {
  19. logs.pageerror.push(String(err && err.message || err).slice(0, 500) + ' | ' + String(err && err.stack || '').slice(0, 800));
  20. });
  21. page.on('requestfailed', req => {
  22. logs.requestfailed.push(req.url().slice(0, 200) + ' :: ' + (req.failure() && req.failure().errorText));
  23. });
  24. page.on('response', res => {
  25. if (res.status() >= 400) logs.requestfailed.push('HTTP ' + res.status() + ' ' + res.url().slice(0, 200));
  26. });
  27. try {
  28. await page.goto(URL_, { waitUntil: 'load', timeout: 30000 });
  29. await page.waitForTimeout(6000);
  30. // 尝试触发一次默认识别相关的初始化函数(若存在)
  31. try {
  32. await page.evaluate(() => {
  33. if (typeof qUpdateRuleDescription === 'function') { try { qUpdateRuleDescription(); } catch(e){} }
  34. });
  35. } catch (e) {}
  36. await page.waitForTimeout(2000);
  37. logs.title = await page.title();
  38. logs.ok = true;
  39. } catch (e) {
  40. logs.error = String(e).slice(0, 500);
  41. }
  42. fs.writeFileSync(OUT, JSON.stringify(logs, null, 2), 'utf8');
  43. await ctx.close();
  44. process.exit(0);
  45. })().catch(e => {
  46. try { fs.writeFileSync(OUT, JSON.stringify({ fatal: String(e).slice(0, 500) }), 'utf8'); } catch (_) {}
  47. process.exit(1);
  48. });