fix: support both json and string cookies for facebook
This commit is contained in:
@@ -297,18 +297,31 @@ export async function ensureFacebookCookies(
|
||||
): Promise<Cookie[]> {
|
||||
// Priority 1: URL parameter (if provided)
|
||||
if (cookiesSource) {
|
||||
// Try JSON array format first
|
||||
try {
|
||||
const cookies = await loadFacebookCookies(cookiesSource);
|
||||
if (cookies.length > 0) {
|
||||
console.log(
|
||||
`Loaded ${cookies.length} Facebook cookies from URL parameter`,
|
||||
`Loaded ${cookies.length} Facebook cookies from URL parameter (JSON format)`,
|
||||
);
|
||||
return cookies;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`Failed to parse cookies from URL parameter: ${e}`);
|
||||
// Continue to next priority
|
||||
} catch {
|
||||
// JSON parse failed, try cookie string format as fallback
|
||||
}
|
||||
|
||||
// Try cookie string format (e.g., "name1=value1; name2=value2")
|
||||
const cookies = parseFacebookCookieString(cookiesSource);
|
||||
if (cookies.length > 0) {
|
||||
console.log(
|
||||
`Loaded ${cookies.length} Facebook cookies from URL parameter (string format)`,
|
||||
);
|
||||
return cookies;
|
||||
}
|
||||
|
||||
console.warn(
|
||||
"URL parameter provided but no valid cookies extracted. Expected JSON array or cookie string.",
|
||||
);
|
||||
}
|
||||
|
||||
// Priority 2: Environment variable
|
||||
@@ -327,12 +340,43 @@ export async function ensureFacebookCookies(
|
||||
|
||||
// Priority 3: Cookie file (fallback)
|
||||
try {
|
||||
const existing = await loadFacebookCookies(undefined, cookiePath);
|
||||
if (existing.length > 0) {
|
||||
console.log(
|
||||
`Loaded ${existing.length} Facebook cookies from ${cookiePath}`,
|
||||
const file = Bun.file(cookiePath);
|
||||
if (await file.exists()) {
|
||||
const content = await file.text();
|
||||
|
||||
// Try JSON array format first
|
||||
try {
|
||||
const parsed = JSON.parse(content);
|
||||
if (Array.isArray(parsed)) {
|
||||
const cookies = parsed.filter(
|
||||
(cookie): cookie is Cookie =>
|
||||
cookie &&
|
||||
typeof cookie.name === "string" &&
|
||||
typeof cookie.value === "string",
|
||||
);
|
||||
if (cookies.length > 0) {
|
||||
console.log(
|
||||
`Loaded ${cookies.length} Facebook cookies from ${cookiePath} (JSON format)`,
|
||||
);
|
||||
return cookies;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// JSON parse failed, try cookie string format
|
||||
}
|
||||
|
||||
// Try cookie string format
|
||||
const cookies = parseFacebookCookieString(content);
|
||||
if (cookies.length > 0) {
|
||||
console.log(
|
||||
`Loaded ${cookies.length} Facebook cookies from ${cookiePath} (string format)`,
|
||||
);
|
||||
return cookies;
|
||||
}
|
||||
|
||||
console.warn(
|
||||
`Cookie file ${cookiePath} exists but no valid cookies extracted`,
|
||||
);
|
||||
return existing;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(`Could not load cookies from ${cookiePath}: ${e}`);
|
||||
|
||||
Reference in New Issue
Block a user