手动登录服务器

This commit is contained in:
ray
2026-04-28 17:52:41 +08:00
parent c7908bf39e
commit fa6e1a0df8
6 changed files with 78 additions and 5 deletions

View File

@@ -33,6 +33,8 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
let _context = null;
let _runtimeController = null;
let _browser = null;
let _isAttachedBrowser = false;
const AUTH_PAGE_KEYWORDS = [
'RAM 用户登录',
@@ -45,6 +47,10 @@ const AUTH_PAGE_KEYWORDS = [
async function closeContextIfNeeded() {
if (!_context) return;
if (_isAttachedBrowser) {
_context = null;
return;
}
await _context.close();
_context = null;
}
@@ -161,6 +167,20 @@ function clearStaleBrowserProfileLocks() {
async function getContext() {
if (_context) return _context;
if (config.browserMode === 'cdp') {
try {
_browser = await chromium.connectOverCDP(config.cdpUrl);
_isAttachedBrowser = true;
const contexts = _browser.contexts();
_context = contexts[0] || await _browser.newContext();
console.log(`[CDP] 已附着到手动浏览器: ${config.cdpUrl}`);
return _context;
} catch (error) {
throw new Error(`无法通过 CDP 连接到手动浏览器(${config.cdpUrl})。请先手动启动 Chrome 并开启远程调试端口。原始错误: ${error.message}`);
}
}
_isAttachedBrowser = false;
clearStaleBrowserProfileLocks();
const launchOptions = {
headless: config.headless,