verify-hxcs-article-add.mjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env node
  2. import { chromium } from "playwright";
  3. const BASE = process.env.ZQ_BASE_URL || "https://hxcs.feifan.net";
  4. async function login(page) {
  5. await page.goto(`${BASE}/apps/workbench/?errpage=toLogin`);
  6. await page.fill("#uname", process.env.ZQ_USER || "a");
  7. await page.evaluate(() => document.getElementById("upwd").removeAttribute("readonly"));
  8. await page.fill("#upwd", process.env.ZQ_PASS || "111111");
  9. await page.click(".submit");
  10. await page.waitForTimeout(3000);
  11. await page.goto(`${BASE}/apps/workbench/`, { waitUntil: "networkidle" });
  12. }
  13. async function articleListUrl(page) {
  14. return page.evaluate(() => {
  15. const raw = document.body.innerHTML.match(/var moudulesData = '([^']+)'/);
  16. const data = JSON.parse(raw[1]);
  17. const walk = (items) => {
  18. for (const it of items || []) {
  19. if ((it.name || "").includes("文章")) return it.url;
  20. const u = walk(it.menuList);
  21. if (u) return u;
  22. }
  23. return null;
  24. };
  25. for (const m of data) {
  26. const u = walk(m.menuList);
  27. if (u) return u;
  28. }
  29. return null;
  30. });
  31. }
  32. async function main() {
  33. const browser = await chromium.launch({ headless: true });
  34. const page = await browser.newPage();
  35. await login(page);
  36. const artUrl = await articleListUrl(page);
  37. await page.goto(`${BASE}${artUrl}`, { waitUntil: "networkidle" });
  38. await page.locator('button:has-text("录入")').first().click();
  39. let found = false;
  40. for (let i = 0; i < 40; i++) {
  41. await page.waitForTimeout(1000);
  42. for (const frame of page.frames()) {
  43. if (!frame.url().includes("apps?token")) continue;
  44. const title = await frame.title().catch(() => "");
  45. if (!title.includes("发文章")) continue;
  46. const ck = await frame.locator(".ck-editor").count();
  47. const video = await frame.locator("text=上传视频").count();
  48. const pdf = await frame.locator("text=上传PDF").count();
  49. const layout = await frame.evaluate(() =>
  50. [...document.scripts].map((s) => s.src).find((s) => s.includes("layout"))
  51. );
  52. console.log("PASS 发文章弹窗", { title, ck, video, pdf, layout });
  53. found = ck > 0 && video > 0 && pdf > 0;
  54. if (!found) {
  55. console.error("FAIL 发文章编辑器不完整", { ck, video, pdf });
  56. process.exit(1);
  57. }
  58. await browser.close();
  59. return;
  60. }
  61. }
  62. const frames = [];
  63. for (const f of page.frames()) {
  64. frames.push(`${await f.title().catch(() => "")} | ${f.url()}`);
  65. }
  66. console.error("FAIL 未打开发文章弹窗", frames);
  67. await browser.close();
  68. process.exit(1);
  69. }
  70. main().catch((e) => {
  71. console.error(e);
  72. process.exit(1);
  73. });