Ver Fonte

feat: enhance BaijiahaoAdapter to handle modal dialogs during video publishing

ethanfly há 3 semanas atrás
pai
commit
4e1e72d793
1 ficheiros alterados com 58 adições e 9 exclusões
  1. 58 9
      server/src/automation/platforms/baijiahao.ts

+ 58 - 9
server/src/automation/platforms/baijiahao.ts

@@ -674,6 +674,9 @@ export class BaijiahaoAdapter extends BasePlatformAdapter {
       const aiCheckInterval = 10000; // 每10秒使用AI检测一次
 
       while (Date.now() - startTime < maxWaitTime) {
+        // 关闭可能出现的弹窗(百家号上传期间可能弹出活动/通知)
+        await this.closeModalDialogs();
+
         // 检查上传进度(通过DOM)
         let progressDetected = false;
         const progressText = await this.page.locator('[class*="progress"]').first().textContent().catch(() => '');
@@ -731,22 +734,65 @@ export class BaijiahaoAdapter extends BasePlatformAdapter {
 
       onProgress?.(60, '正在填写视频信息...');
 
-      // 填写标题
-      const titleInput = this.page.locator('input[placeholder*="标题"], textarea[placeholder*="标题"]').first();
-      if (await titleInput.count() > 0) {
-        await titleInput.fill(params.title);
+      // 关闭可能出现的弹窗
+      await this.closeModalDialogs();
+
+      // 填写标题 — 遍历所有 frame(百家号编辑页可能在 iframe 中)
+      let titleFilled = false;
+      for (const frame of frames) {
+        if (titleFilled) break;
+        try {
+          const titleInput = frame.locator(
+            'input[placeholder*="标题"], textarea[placeholder*="标题"], ' +
+            '[class*="title"] input, [class*="title"] textarea, ' +
+            'input[name*="title"], textarea[name*="title"], ' +
+            'input[data-placeholder*="标题"], textarea[data-placeholder*="标题"]'
+          ).first();
+          if (await titleInput.count() > 0 && await titleInput.isVisible().catch(() => false)) {
+            await titleInput.fill(params.title);
+            titleFilled = true;
+            logger.info(`[Baijiahao Publish] Title filled in frame: ${frame.url()}`);
+          }
+        } catch { /* continue to next frame */ }
+      }
+      if (!titleFilled) {
+        logger.warn('[Baijiahao Publish] Title input not found in any frame');
       }
 
-      // 填写描述
+      // 填写描述 — 遍历所有 frame
       if (params.description) {
-        const descInput = this.page.locator('textarea[placeholder*="简介"], textarea[placeholder*="描述"]').first();
-        if (await descInput.count() > 0) {
-          await descInput.fill(params.description);
+        let descFilled = false;
+        for (const frame of frames) {
+          if (descFilled) break;
+          try {
+            const descInput = frame.locator(
+              'textarea[placeholder*="简介"], textarea[placeholder*="描述"], ' +
+              'textarea[placeholder*="介绍"], textarea[placeholder*="说明"], ' +
+              '[class*="desc"] textarea, [class*="intro"] textarea, ' +
+              '[class*="content"] textarea, [class*="editor"] textarea, ' +
+              'textarea[name*="desc"], textarea[name*="description"], ' +
+              'textarea[name*="intro"], textarea[name*="content"], ' +
+              'div[contenteditable="true"], ' +
+              '[data-placeholder*="简介"], [data-placeholder*="描述"]'
+            ).first();
+            if (await descInput.count() > 0 && await descInput.isVisible().catch(() => false)) {
+              await descInput.fill(params.description);
+              descFilled = true;
+              logger.info(`[Baijiahao Publish] Description filled in frame: ${frame.url()}`);
+            }
+          } catch { /* continue to next frame */ }
+        }
+        if (!descFilled) {
+          logger.warn('[Baijiahao Publish] Description input not found in any frame');
         }
       }
 
       onProgress?.(80, '正在发布...');
 
+      // 再次关闭弹窗,并在发布前稍作等待确保 UI 稳定
+      await this.closeModalDialogs();
+      await this.page.waitForTimeout(1000);
+
       // 点击发布按钮
       const publishBtn = this.page.locator('button:has-text("发布"), [class*="publish-btn"]').first();
       if (await publishBtn.count() > 0) {
@@ -764,7 +810,10 @@ export class BaijiahaoAdapter extends BasePlatformAdapter {
 
       while (Date.now() - publishStartTime < publishMaxWait) {
         await this.page.waitForTimeout(3000);
-        
+
+        // 先尝试关闭可能出现的弹窗
+        await this.closeModalDialogs();
+
         // 检查是否跳转到内容管理页面
         const currentUrl = this.page.url();
         if (currentUrl.includes('/content') || currentUrl.includes('/rc/home')) {