From 9d9a6f62c9a28899fffbd896c01e015d01ccd970 Mon Sep 17 00:00:00 2001 From: "949036910@qq.com" <> Date: Wed, 3 Jun 2026 15:27:21 +0800 Subject: [PATCH] 11 --- scripts/nodejs/publish_platform.js | 10 ++++- scripts/nodejs/publish_to_douyin.js | 60 ++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/scripts/nodejs/publish_platform.js b/scripts/nodejs/publish_platform.js index 8b73d3d..4ee4052 100644 --- a/scripts/nodejs/publish_platform.js +++ b/scripts/nodejs/publish_platform.js @@ -14,10 +14,16 @@ async function publishToPlatform(platform, accountId, cookies, params) { const label = PLATFORM_LABELS[platform] || platform; if (platform === "douyin") { const page = await newPublishPage(platform, accountId, cookies); + let result; try { - return await publishToDouyin(page, params); + result = await publishToDouyin(page, params); + return result; } finally { - if (!params.keepPageOpen) { + const keepOpen = + Boolean(params.keepPageOpen) || + Boolean(result?.keepPageOpen) || + result?.status === "manual_pending"; + if (!keepOpen) { try { await page.close(); } catch { diff --git a/scripts/nodejs/publish_to_douyin.js b/scripts/nodejs/publish_to_douyin.js index 749f5d4..6d05657 100644 --- a/scripts/nodejs/publish_to_douyin.js +++ b/scripts/nodejs/publish_to_douyin.js @@ -17,6 +17,54 @@ async function clickIfVisible(page, selector) { } } +function isPrimaryPublishLabel(text) { + const t = String(text || "").replace(/\s+/g, ""); + if (!t.includes("发布")) return false; + if (t.includes("定时") || t.includes("草稿") || t.includes("取消")) return false; + return ( + t === "发布" || + t === "确定发布" || + t === "立即发布" || + t.endsWith("发布") + ); +} + +async function clickPublishButton(page) { + const handles = await page.$$('button, [role="button"]'); + for (const btn of handles) { + let text = ""; + let visible = false; + try { + text = await page.evaluate((el) => (el.textContent || "").trim(), btn); + visible = await page.evaluate((el) => { + const rect = el.getBoundingClientRect(); + if (rect.width < 2 || rect.height < 2) return false; + const style = window.getComputedStyle(el); + return ( + style.display !== "none" && + style.visibility !== "hidden" && + !el.disabled && + el.getAttribute("aria-disabled") !== "true" + ); + }, btn); + } catch { + continue; + } + if (!visible || !isPrimaryPublishLabel(text)) continue; + try { + await btn.evaluate((el) => + el.scrollIntoView({ block: "center", inline: "center" }), + ); + await delay(300); + await btn.click(); + return true; + } catch { + /* try next */ + } + } + return false; +} + async function typeIntoFirst(page, selectors, text) { for (const sel of selectors) { const el = await page.$(sel); @@ -165,16 +213,7 @@ async function publishToDouyin(page, params) { } globalThis.__native?.emitProgress?.("正在点击发布…"); - const publishBtns = await page.$$("button"); - let clicked = false; - for (const btn of publishBtns) { - const text = (await page.evaluate((el) => el.textContent || "", btn)).trim(); - if (text === "发布" || text === "确定发布") { - await btn.click(); - clicked = true; - break; - } - } + const clicked = await clickPublishButton(page); if (!clicked) { return { @@ -182,6 +221,7 @@ async function publishToDouyin(page, params) { status: "manual_pending", message: "未找到发布按钮,请手动发布", videoUrl: page.url(), + keepPageOpen: true, }; }