11
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user