fix(boss): prioritize fresh browser cookie extraction over boss-cli cache

This commit is contained in:
jackwener committed 2026-03-15 19:58:46 +08:00
1 parent 64d655ae0d
commit 00f0ac9133
1 file changed
+16 -15
+16 -15
View File
@@ -125,9 +125,23 @@ function extractCookiesViaPython(): Record<string, string> | 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<string, string> = 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' +