fix: 修复自动登录时可能会出现的问题

This commit is contained in:
foxhui
2025-12-11 19:20:35 +08:00
Unverified
parent 7d7a9ace79
commit 13d2deef92
2 changed files with 37 additions and 4 deletions
+18 -1
View File
@@ -104,12 +104,29 @@ async function waitForInputWithAccountChooser(page, options = {}) {
// 轮询等待输入框,同时处理账户选择
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
// 如果正在处理跳转,暂停检测输入框
if (isHandlingAuth) {
await sleep(500, 1000);
continue;
}
if (await handleAccountChooser(page)) {
// 处理了账户选择,重置等待
continue;
}
const inputHandle = await page.$(INPUT_SELECTOR);
let inputHandle = null;
try {
inputHandle = await page.$(INPUT_SELECTOR);
} catch (e) {
// 忽略执行上下文销毁错误
if (e.message.includes('Execution context was destroyed')) {
inputHandle = null;
} else {
throw e;
}
}
if (inputHandle) break;
await sleep(1000, 1500);
+19 -3
View File
@@ -21,14 +21,13 @@ const INPUT_SELECTOR = '.tiptap.ProseMirror';
// 入口 URL
const TARGET_URL = 'https://zai.is/';
// 防止重复处理登录的锁
let isHandlingAuth = false;
/**
* 处理 Discord OAuth2 登录流程
* @param {import('playwright-core').Page} page
* @returns {Promise<boolean>} 是否处理了登录
*/
let isHandlingAuth = false;
async function handleDiscordAuth(page) {
// 防止重复处理
if (isHandlingAuth) return false;
@@ -126,11 +125,28 @@ async function waitForInputWithAuth(page, options = {}) {
// 轮询等待输入框,同时处理登录
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
// 如果正在处理登录,暂停检测输入框,避免冲突
if (isHandlingAuth) {
await sleep(500, 1000);
continue;
}
if (await handleDiscordAuth(page)) {
continue;
}
const inputHandle = await page.$(INPUT_SELECTOR);
let inputHandle = null;
try {
inputHandle = await page.$(INPUT_SELECTOR);
} catch (e) {
// 忽略执行上下文销毁错误 (通常发生在页面刷新/跳转时)
if (e.message.includes('Execution context was destroyed')) {
inputHandle = null;
} else {
throw e;
}
}
if (inputHandle) break;
await sleep(1000, 1500);