1
This commit is contained in:
@@ -87,9 +87,39 @@ async function getPublishBrowser(platform, accountId) {
|
|||||||
return browser;
|
return browser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function closeBlankPages(browser, keepPage) {
|
||||||
|
try {
|
||||||
|
const pages = await browser.pages();
|
||||||
|
for (const p of pages) {
|
||||||
|
if (keepPage && p === keepPage) continue;
|
||||||
|
const u = p.url();
|
||||||
|
if (!u || u === "about:blank") {
|
||||||
|
try {
|
||||||
|
await p.close();
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function newPublishPage(platform, accountId, cookies) {
|
async function newPublishPage(platform, accountId, cookies) {
|
||||||
const browser = await getPublishBrowser(platform, accountId);
|
const browser = await getPublishBrowser(platform, accountId);
|
||||||
const page = await browser.newPage();
|
const pages = await browser.pages();
|
||||||
|
let page =
|
||||||
|
pages.find((p) => {
|
||||||
|
try {
|
||||||
|
return p.url() === "about:blank";
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}) || null;
|
||||||
|
if (!page) {
|
||||||
|
page = await browser.newPage();
|
||||||
|
}
|
||||||
await page.setUserAgent(DEFAULT_USER_AGENT);
|
await page.setUserAgent(DEFAULT_USER_AGENT);
|
||||||
await page.evaluateOnNewDocument(STEALTH_INIT_SCRIPT);
|
await page.evaluateOnNewDocument(STEALTH_INIT_SCRIPT);
|
||||||
page.setDefaultNavigationTimeout(60000);
|
page.setDefaultNavigationTimeout(60000);
|
||||||
@@ -101,9 +131,22 @@ async function newPublishPage(platform, accountId, cookies) {
|
|||||||
console.warn("setCookie 失败:", e.message);
|
console.warn("setCookie 失败:", e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
await closeBlankPages(browser, page);
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function closePublishBrowser(platform, accountId) {
|
||||||
|
const key = contextKey(platform, accountId);
|
||||||
|
const browser = browsers.get(key);
|
||||||
|
if (!browser) return;
|
||||||
|
browsers.delete(key);
|
||||||
|
try {
|
||||||
|
await browser.close();
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const LOGIN_URLS = {
|
const LOGIN_URLS = {
|
||||||
douyin: "https://creator.douyin.com/",
|
douyin: "https://creator.douyin.com/",
|
||||||
kuaishou: "https://cp.kuaishou.com/",
|
kuaishou: "https://cp.kuaishou.com/",
|
||||||
@@ -118,4 +161,5 @@ module.exports = {
|
|||||||
userDataDir,
|
userDataDir,
|
||||||
LOGIN_URLS,
|
LOGIN_URLS,
|
||||||
getChromiumExecutablePath,
|
getChromiumExecutablePath,
|
||||||
|
closePublishBrowser,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { delay, newPublishPage, LOGIN_URLS } = require("./publish_browser.js");
|
const {
|
||||||
|
delay,
|
||||||
|
newPublishPage,
|
||||||
|
LOGIN_URLS,
|
||||||
|
closePublishBrowser,
|
||||||
|
} = require("./publish_browser.js");
|
||||||
|
|
||||||
const LOGIN_SELECTORS = {
|
const LOGIN_SELECTORS = {
|
||||||
douyin: ["#header-avatar", ".creator-avatar", 'input[type="file"]'],
|
douyin: ["#header-avatar", ".creator-avatar", 'input[type="file"]'],
|
||||||
@@ -28,6 +33,34 @@ async function checkLoggedIn(page, platform) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function resolveUid(page) {
|
||||||
|
try {
|
||||||
|
const uidFromDom = await page.evaluate(() => {
|
||||||
|
const node = document.querySelector("[data-user-id], [data-uid]");
|
||||||
|
return node?.getAttribute("data-user-id") || node?.getAttribute("data-uid") || "";
|
||||||
|
});
|
||||||
|
if (uidFromDom) return String(uidFromDom);
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const local = await page.evaluate(() => {
|
||||||
|
const keys = Object.keys(localStorage || {});
|
||||||
|
for (const k of keys) {
|
||||||
|
if (/(uid|user.?id|sec_uid|openid)/i.test(k)) {
|
||||||
|
const v = localStorage.getItem(k);
|
||||||
|
if (v) return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
if (local) return String(local).slice(0, 128);
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
globalThis.__nodejsMain = async function main(params) {
|
globalThis.__nodejsMain = async function main(params) {
|
||||||
const p = params || {};
|
const p = params || {};
|
||||||
const platform = String(p.platform || "").trim();
|
const platform = String(p.platform || "").trim();
|
||||||
@@ -48,6 +81,7 @@ globalThis.__nodejsMain = async function main(params) {
|
|||||||
while (Date.now() - start < maxMs) {
|
while (Date.now() - start < maxMs) {
|
||||||
if (await checkLoggedIn(page, platform)) {
|
if (await checkLoggedIn(page, platform)) {
|
||||||
const cookies = await page.cookies();
|
const cookies = await page.cookies();
|
||||||
|
const resolvedUid = await resolveUid(page);
|
||||||
let nickname = "";
|
let nickname = "";
|
||||||
try {
|
try {
|
||||||
nickname = await page.$eval(
|
nickname = await page.$eval(
|
||||||
@@ -57,11 +91,22 @@ globalThis.__nodejsMain = async function main(params) {
|
|||||||
} catch {
|
} catch {
|
||||||
nickname = "已登录用户";
|
nickname = "已登录用户";
|
||||||
}
|
}
|
||||||
|
let avatar = "";
|
||||||
|
try {
|
||||||
|
avatar =
|
||||||
|
(await page.$eval(
|
||||||
|
"img.avatar, img[class*='avatar'], .avatar img",
|
||||||
|
(el) => el?.src || "",
|
||||||
|
)) || "";
|
||||||
|
} catch {
|
||||||
|
avatar = "";
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "登录成功",
|
message: "登录成功",
|
||||||
nickname: nickname || "已登录用户",
|
nickname: nickname || "已登录用户",
|
||||||
uid: `user_${Date.now()}`,
|
uid: resolvedUid || `user_${Date.now()}`,
|
||||||
|
avatar,
|
||||||
cookies,
|
cookies,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -76,7 +121,12 @@ globalThis.__nodejsMain = async function main(params) {
|
|||||||
try {
|
try {
|
||||||
await page.close();
|
await page.close();
|
||||||
} catch {
|
} catch {
|
||||||
/* keep browser for user */
|
/* ignore */
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await closePublishBrowser(platform, accountId);
|
||||||
|
} catch {
|
||||||
|
/* ignore */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user