Update micBrowserSession.js via upload script - 2026-09-21 00:31:04

This commit is contained in:
mike 2026-09-21 00:31:14 +08:00
parent 20ec6b2106
commit e3fbb1db18
1 changed files with 30 additions and 14 deletions

View File

@ -4,8 +4,15 @@
// uploader_enhanced.py(v33.0),仅去掉了本地 .env 自动登录(改为人工登录)
// 和批量循环(本版本一次只处理一个产品)。
const path = require('path');
const { chromium } = require('playwright');
// 持久化浏览器profile目录——登录态(cookie/localStorage)存在这里,跨会话/跨Node
// 重启都保留。同一时间只能有一个会话用这个目录(Chromium会给profile目录加锁),
// 这对单人操作的工具来说没问题;如果上一个会话没有正常关闭(比如浏览器标签页被
// 直接关掉),这个目录可能残留锁文件导致下次启动失败,报错信息会提示。
const USER_DATA_DIR = path.join(__dirname, 'browser-profile');
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
@ -15,7 +22,6 @@ class MicBrowserSession {
this.id = id;
this.onFrame = onFrame || (() => {});
this.onLog = onLog || (() => {});
this.browser = null;
this.context = null;
this.page = null;
this.cdp = null;
@ -30,18 +36,28 @@ class MicBrowserSession {
// ------------------------------ 生命周期 ------------------------------
async start() {
this.browser = await chromium.launch({
// launchPersistentContext没有独立的browser对象,context本身就代表这个持久化
// 浏览器实例;关闭时调用 context.close() 就够了,登录态留在USER_DATA_DIR里。
try {
this.context = await chromium.launchPersistentContext(USER_DATA_DIR, {
headless: true,
args: ['--disable-blink-features=AutomationControlled'],
});
this.context = await this.browser.newContext({
viewport: this.viewport,
locale: 'en-US',
userAgent:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
args: ['--disable-blink-features=AutomationControlled'],
});
this.page = await this.context.newPage();
} catch (e) {
if (String(e).includes('ProcessSingleton') || String(e).includes('lock')) {
throw new Error(
`浏览器profile目录被锁住了(上一个会话可能没正常关闭): ${e.message}。`
+ `需要人工确认没有残留的chromium进程后删掉 ${USER_DATA_DIR}/SingletonLock 再试。`,
);
}
throw e;
}
this.page = this.context.pages()[0] || (await this.context.newPage());
this.cdp = await this.context.newCDPSession(this.page);
await this.cdp.send('Page.startScreencast', {
@ -66,7 +82,7 @@ class MicBrowserSession {
if (this.closed) return;
this.closed = true;
try { await this.cdp?.send('Page.stopScreencast'); } catch (e) {}
try { await this.browser?.close(); } catch (e) {}
try { await this.context?.close(); } catch (e) {}
}
// -------------------------- 输入转发(登录阶段用) --------------------------