Skip to content

Instantly share code, notes, and snippets.

@sorrycc
Created February 22, 2026 11:36
Show Gist options
  • Select an option

  • Save sorrycc/62bbf471898f49a3466ec8bb67552083 to your computer and use it in GitHub Desktop.

Select an option

Save sorrycc/62bbf471898f49a3466ec8bb67552083 to your computer and use it in GitHub Desktop.
import { getCookies } from "@steipete/sweet-cookie";
const TWITTER_URL = "https://x.com/";
const TWITTER_ORIGINS = ["https://x.com/", "https://twitter.com/"];
const COOKIE_NAMES = ["auth_token", "ct0"];
function pickCookieValue(
cookies: Array<{ name?: string; value?: string; domain?: string }>,
name: string
): string | null {
const matches = cookies.filter(
(c) => c?.name === name && typeof c.value === "string"
);
if (matches.length === 0) return null;
const preferred = matches.find((c) => (c.domain ?? "").endsWith("x.com"));
if (preferred?.value) return preferred.value;
const twitter = matches.find((c) =>
(c.domain ?? "").endsWith("twitter.com")
);
if (twitter?.value) return twitter.value;
return matches[0]?.value ?? null;
}
async function main() {
console.log("[1] Requesting cookies from Chrome via sweet-cookie...");
console.log(` url: ${TWITTER_URL}`);
console.log(` origins: ${TWITTER_ORIGINS.join(", ")}`);
console.log(` names: ${COOKIE_NAMES.join(", ")}`);
const start = performance.now();
const { cookies, warnings } = await getCookies({
url: TWITTER_URL,
origins: TWITTER_ORIGINS,
names: [...COOKIE_NAMES],
browsers: ["chrome"],
mode: "merge",
timeoutMs: 300_000,
});
const elapsed = (performance.now() - start).toFixed(0);
console.log(`[2] getCookies returned in ${elapsed}ms`);
if (warnings.length > 0) {
console.log(`[!] Warnings:`);
for (const w of warnings) console.log(` - ${w}`);
}
console.log(`[3] Raw cookies returned: ${cookies.length}`);
for (const c of cookies) {
const val = c.value ?? "";
const masked = val.length > 8 ? val.slice(0, 4) + "..." + val.slice(-4) : "****";
console.log(` name=${c.name} domain=${c.domain} value=${masked}`);
}
const authToken = pickCookieValue(cookies, "auth_token");
const ct0 = pickCookieValue(cookies, "ct0");
console.log("[4] Extracted values:");
if (authToken) {
console.log(
` auth_token: ${authToken.slice(0, 4)}...${authToken.slice(-4)} (${authToken.length} chars)`
);
} else {
console.log(" auth_token: NOT FOUND");
}
if (ct0) {
console.log(
` ct0: ${ct0.slice(0, 4)}...${ct0.slice(-4)} (${ct0.length} chars)`
);
} else {
console.log(" ct0: NOT FOUND");
}
if (authToken && ct0) {
console.log("[5] Cookie header:");
console.log(` auth_token=${authToken}; ct0=${ct0}`);
} else {
console.error(
"[5] FAILED: Missing cookies. Make sure you are logged into x.com in Chrome."
);
process.exit(1);
}
}
main().catch((err) => {
console.error("[FATAL]", err);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment