From 00f0ac9133d77ba626d9177cd02ffe4d340d5325 Mon Sep 17 00:00:00 2001 From: jackwener Date: Sun, 15 Mar 2026 19:58:46 +0800 Subject: [PATCH] fix(boss): prioritize fresh browser cookie extraction over boss-cli cache --- src/clis/boss/search.ts | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/clis/boss/search.ts b/src/clis/boss/search.ts index 75e54ad..fbfaca4 100644 --- a/src/clis/boss/search.ts +++ b/src/clis/boss/search.ts @@ -125,9 +125,23 @@ function extractCookiesViaPython(): Record | null { function loadCookieHeader(): string { if (_cachedCookieHeader) return _cachedCookieHeader; - // Priority 1: boss-cli credential file - const credPath = path.join(os.homedir(), '.config', 'boss-cli', 'credential.json'); + // Priority 1: extract fresh from Chrome via browser-cookie3 + const extracted = extractCookiesViaPython(); + if (extracted && Object.keys(extracted).length > 0) { + _cachedCookieHeader = Object.entries(extracted).map(([k, v]) => `${k}=${v}`).join('; '); + // Save for future use + try { + const configDir = path.join(os.homedir(), '.config', 'boss-cli'); + const credPath = path.join(configDir, 'credential.json'); + fs.mkdirSync(configDir, { recursive: true }); + fs.writeFileSync(credPath, JSON.stringify({ cookies: extracted, saved_at: Date.now() / 1000 }, null, 2)); + } catch { /* ignore save errors */ } + return _cachedCookieHeader; + } + + // Priority 2: boss-cli credential file (fallback) try { + const credPath = path.join(os.homedir(), '.config', 'boss-cli', 'credential.json'); const data = JSON.parse(fs.readFileSync(credPath, 'utf-8')); const cookies: Record = data.cookies || {}; if (Object.keys(cookies).length > 0) { @@ -136,19 +150,6 @@ function loadCookieHeader(): string { } } catch { /* not available */ } - // Priority 2: extract from Chrome via browser-cookie3 - const extracted = extractCookiesViaPython(); - if (extracted && Object.keys(extracted).length > 0) { - _cachedCookieHeader = Object.entries(extracted).map(([k, v]) => `${k}=${v}`).join('; '); - // Save for future use - try { - const configDir = path.join(os.homedir(), '.config', 'boss-cli'); - fs.mkdirSync(configDir, { recursive: true }); - fs.writeFileSync(credPath, JSON.stringify({ cookies: extracted, saved_at: Date.now() / 1000 }, null, 2)); - } catch { /* ignore save errors */ } - return _cachedCookieHeader; - } - throw new Error( 'BOSS 直聘需要 Cookie 登录态。请确保:\n' + ' 1. 你已在 Chrome 中登录 www.zhipin.com\n' +