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