/** * Read X (x.com) through the browser's own signed-in session: search posts, * read one post with its thread and replies, read a profile, list trends. * * X serves all of this only to a signed-in session and renders it from * JavaScript, so the page is the only interface there is: open the page in a * background tab, wait for it to render, read it, scroll for more, close the * tab. Free, unlike xAI's x_search ($5 per 1,000 posts returned). * * A search query is X's own search syntax, passed through untouched: * from:handle, to:handle, since:2026-11-01, until:..., min_faves:100, * filter:media, -filter:replies, lang:en, "exact phrase", OR. * * Every read shares one set of limits (x-limits.ts) and, signed out, signs the * browser back in once (x-login.ts). Every failure throws, as for the engine * searches (errors.ts): signed out, an X error page, or nothing rendering are * reported as such, never as an empty answer. X's own "No results" or "doesn't * exist" page is reported as an error naming what was asked for. */ import type { PageSession } from "./cdp.ts"; import { BrowserUnavailableError, NoResultsError } from "./errors.ts"; import { withPage } from "./search.ts"; import { XLimiter } from "./x-limits.ts"; import { loadCredentials, X_LOGIN_TIMEOUT_MS, xLogin } from "./x-login.ts"; /** Shared by every X read in this process: see x-limits.ts. */ const limiter = new XLimiter(); /** * Signed out, a read signs the browser back in (x-login.ts) when * CCS_X_LOGIN_FILE names the credentials, then reads again, once. At most one * sign-in attempt per LOGIN_COOLDOWN_MS: a sign-in that keeps failing is what * gets an account locked, so until the cooldown has passed every read fails * with the reason the last attempt gave. */ export const LOGIN_COOLDOWN_MS = 30 * 60_000; let lastLogin: { at: number; error?: string } | undefined; class SignedOut extends Error { readonly at: string; constructor(at: string) { super(`signed out at ${at}`); this.at = at; } } export type XSort = "latest" | "top"; export type XMedia = { type: "photo" | "video"; url?: string; alt?: string }; export type XPost = { url: string; author: string; handle: string; /** ISO 8601, from the post's