mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-08-07 17:33:03 +08:00
fix(gemini-web): reuse openPageSession and fix orphaned tab leak
- Replace manual CDP target orchestration with openPageSession, keeping behavior consistent with fetch_google_cookies_via_cdp - Move created-tab cleanup into finally block so tabs are always closed even when Target.attachToTarget or Network.enable throws Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+22
-29
@@ -108,33 +108,30 @@ async function fetch_cookies_from_existing_chrome(
|
|||||||
if (verbose) logger.info(`Found existing Chrome on port ${port}. Extracting cookies...`);
|
if (verbose) logger.info(`Found existing Chrome on port ${port}. Extracting cookies...`);
|
||||||
|
|
||||||
let cdp: CdpConnection | null = null;
|
let cdp: CdpConnection | null = null;
|
||||||
|
let createdTab = false;
|
||||||
|
let targetId: string | null = null;
|
||||||
try {
|
try {
|
||||||
const wsUrl = await waitForChromeDebugPort(port, 10_000, { includeLastError: true });
|
const wsUrl = await waitForChromeDebugPort(port, 10_000, { includeLastError: true });
|
||||||
cdp = await CdpConnection.connect(wsUrl, 15_000);
|
cdp = await CdpConnection.connect(wsUrl, 15_000);
|
||||||
|
|
||||||
const targets = await cdp.send<{ targetInfos: Array<{ targetId: string; url: string; type: string }> }>('Target.getTargets');
|
const targets = await cdp.send<{ targetInfos: Array<{ targetId: string; url: string; type: string }> }>('Target.getTargets');
|
||||||
const geminiTarget = targets.targetInfos.find(
|
const hasGeminiTab = targets.targetInfos.some(
|
||||||
(t) => t.type === 'page' && t.url.includes('gemini.google.com'),
|
(t) => t.type === 'page' && t.url.includes('gemini.google.com'),
|
||||||
);
|
);
|
||||||
|
createdTab = !hasGeminiTab;
|
||||||
|
|
||||||
let targetId: string;
|
if (verbose) logger.debug(hasGeminiTab ? 'Found existing Gemini tab, attaching...' : 'No Gemini tab found, creating new tab...');
|
||||||
let createdTab = false;
|
|
||||||
|
|
||||||
if (geminiTarget) {
|
const page = await openPageSession({
|
||||||
targetId = geminiTarget.targetId;
|
cdp,
|
||||||
if (verbose) logger.debug('Found existing Gemini tab, attaching...');
|
reusing: false,
|
||||||
} else {
|
url: GEMINI_APP_URL,
|
||||||
const created = await cdp.send<{ targetId: string }>('Target.createTarget', { url: GEMINI_APP_URL });
|
matchTarget: (target) => target.type === 'page' && target.url.includes('gemini.google.com'),
|
||||||
targetId = created.targetId;
|
enableNetwork: true,
|
||||||
createdTab = true;
|
activateTarget: false,
|
||||||
if (verbose) logger.debug('No Gemini tab found, created new tab...');
|
});
|
||||||
}
|
const { sessionId } = page;
|
||||||
|
targetId = page.targetId;
|
||||||
const { sessionId } = await cdp.send<{ sessionId: string }>(
|
|
||||||
'Target.attachToTarget',
|
|
||||||
{ targetId, flatten: true },
|
|
||||||
);
|
|
||||||
await cdp.send('Network.enable', {}, { sessionId });
|
|
||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
let last: CookieMap = {};
|
let last: CookieMap = {};
|
||||||
@@ -152,27 +149,23 @@ async function fetch_cookies_from_existing_chrome(
|
|||||||
}
|
}
|
||||||
|
|
||||||
last = cookieMap;
|
last = cookieMap;
|
||||||
if (await is_gemini_session_ready(cookieMap, verbose)) {
|
if (await is_gemini_session_ready(cookieMap, verbose)) return cookieMap;
|
||||||
if (createdTab) {
|
|
||||||
try { await cdp.send('Target.closeTarget', { targetId }, { timeoutMs: 5_000 }); } catch {}
|
|
||||||
}
|
|
||||||
return cookieMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
await sleep(1000);
|
await sleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (createdTab) {
|
|
||||||
try { await cdp.send('Target.closeTarget', { targetId }, { timeoutMs: 5_000 }); } catch {}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (verbose) logger.debug(`Existing Chrome did not yield valid cookies. Last keys: ${Object.keys(last).join(', ')}`);
|
if (verbose) logger.debug(`Existing Chrome did not yield valid cookies. Last keys: ${Object.keys(last).join(', ')}`);
|
||||||
return null;
|
return null;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (verbose) logger.debug(`Failed to connect to existing Chrome: ${e instanceof Error ? e.message : String(e)}`);
|
if (verbose) logger.debug(`Failed to connect to existing Chrome: ${e instanceof Error ? e.message : String(e)}`);
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
if (cdp) cdp.close();
|
if (cdp) {
|
||||||
|
if (createdTab && targetId) {
|
||||||
|
try { await cdp.send('Target.closeTarget', { targetId }, { timeoutMs: 5_000 }); } catch {}
|
||||||
|
}
|
||||||
|
cdp.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user