Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Post text, images, videos, and long-form articles to X (Twitter) via real Chrome browser automation.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/x-utils.ts
1import { execSync, spawnSync } from 'node:child_process';2import fs from 'node:fs';3import path from 'node:path';4import process from 'node:process';5import { fileURLToPath } from 'node:url';67import {8CdpConnection,9findChromeExecutable as findChromeExecutableBase,10findExistingChromeDebugPort as findExistingChromeDebugPortBase,11getFreePort as getFreePortBase,12gracefulKillChrome,13killChrome,14launchChrome as launchChromeBase,15openPageSession,16resolveSharedChromeProfileDir,17sleep,18waitForChromeDebugPort,19type PlatformCandidates,20} from 'baoyu-chrome-cdp';2122export { CdpConnection, gracefulKillChrome, killChrome, openPageSession, sleep, waitForChromeDebugPort };23export type { PlatformCandidates } from 'baoyu-chrome-cdp';2425const X_SESSION_URLS = ['https://x.com/', 'https://twitter.com/'] as const;26const REQUIRED_X_SESSION_COOKIES = ['auth_token', 'ct0'] as const;2728interface CookieLike {29name?: string;30value?: string | null;31}3233interface NetworkGetCookiesResult {34cookies?: CookieLike[];35}3637export const CHROME_CANDIDATES_BASIC: PlatformCandidates = {38darwin: [39'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',40'/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',41'/Applications/Chromium.app/Contents/MacOS/Chromium',42],43win32: [44'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',45'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',46],47default: [48'/usr/bin/google-chrome',49'/usr/bin/chromium',50'/usr/bin/chromium-browser',51],52};5354export const CHROME_CANDIDATES_FULL: PlatformCandidates = {55darwin: [56'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',57'/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',58'/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta',59'/Applications/Chromium.app/Contents/MacOS/Chromium',60'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',61],62win32: [63'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',64'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',65'C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe',66'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe',67],68default: [69'/usr/bin/google-chrome',70'/usr/bin/google-chrome-stable',71'/usr/bin/chromium',72'/usr/bin/chromium-browser',73'/snap/bin/chromium',74'/usr/bin/microsoft-edge',75],76};7778export function findChromeExecutable(candidates: PlatformCandidates): string | undefined {79return findChromeExecutableBase({80candidates,81envNames: ['X_BROWSER_CHROME_PATH'],82});83}8485let _wslHome: string | null | undefined;86function getWslWindowsHome(): string | null {87if (_wslHome !== undefined) return _wslHome;88if (!process.env.WSL_DISTRO_NAME) { _wslHome = null; return null; }89try {90const raw = execSync('cmd.exe /C "echo %USERPROFILE%"', { encoding: 'utf-8', timeout: 5000 }).trim().replace(/\r/g, '');91_wslHome = execSync(`wslpath -u "${raw}"`, { encoding: 'utf-8', timeout: 5000 }).trim() || null;92} catch { _wslHome = null; }93return _wslHome;94}9596export function getDefaultProfileDir(): string {97return resolveSharedChromeProfileDir({98envNames: ['BAOYU_CHROME_PROFILE_DIR', 'X_BROWSER_PROFILE_DIR'],99wslWindowsHome: getWslWindowsHome(),100});101}102103export async function getFreePort(): Promise<number> {104return await getFreePortBase('X_BROWSER_DEBUG_PORT');105}106107export async function findExistingChromeDebugPort(profileDir: string): Promise<number | null> {108return await findExistingChromeDebugPortBase({ profileDir });109}110111const CHROME_LOCK_FILES = ['SingletonLock', 'SingletonSocket', 'SingletonCookie', 'chrome.pid'] as const;112113export function hasChromeLockArtifacts(entries: readonly string[]): boolean {114return CHROME_LOCK_FILES.some((name) => entries.includes(name));115}116117export function shouldRetryChromeLaunch(options: {118lockArtifactsPresent: boolean;119hasLiveOwner: boolean;120}): boolean {121return options.lockArtifactsPresent && !options.hasLiveOwner;122}123124export function buildXSessionCookieMap(cookies: readonly CookieLike[]): Record<string, string> {125const cookieMap: Record<string, string> = {};126for (const cookie of cookies) {127const name = cookie.name?.trim();128const value = cookie.value?.trim();129if (!name || !value) {130continue;131}132cookieMap[name] = value;133}134return cookieMap;135}136137export function hasRequiredXSessionCookies(cookieMap: Record<string, string>): boolean {138return REQUIRED_X_SESSION_COOKIES.every((name) => Boolean(cookieMap[name]));139}140141export async function readXSessionCookieMap(142cdp: CdpConnection,143sessionId?: string,144): Promise<Record<string, string>> {145const { cookies } = await cdp.send<NetworkGetCookiesResult>(146'Network.getCookies',147{ urls: [...X_SESSION_URLS] },148{149sessionId,150timeoutMs: 5_000,151},152);153return buildXSessionCookieMap(cookies ?? []);154}155156export async function waitForXSessionPersistence(options: {157cdp: CdpConnection;158sessionId?: string;159timeoutMs?: number;160pollIntervalMs?: number;161}): Promise<boolean> {162const timeoutMs = options.timeoutMs ?? 15_000;163const pollIntervalMs = options.pollIntervalMs ?? 1_000;164const start = Date.now();165166while (Date.now() - start < timeoutMs) {167const cookieMap = await readXSessionCookieMap(options.cdp, options.sessionId).catch(() => ({}));168if (hasRequiredXSessionCookies(cookieMap)) {169return true;170}171await sleep(pollIntervalMs);172}173174return false;175}176177function cleanStaleLockFiles(profileDir: string): void {178for (const name of CHROME_LOCK_FILES) {179try { fs.unlinkSync(path.join(profileDir, name)); } catch {}180}181}182183function hasLiveChromeOwner(profileDir: string): boolean {184if (process.platform === 'win32') return false;185try {186const result = spawnSync('ps', ['aux'], {187encoding: 'utf8',188timeout: 5000,189});190if (result.status !== 0 || !result.stdout) return false;191return result.stdout.split('\n').some((line) => line.includes(`--user-data-dir=${profileDir}`));192} catch {193return false;194}195}196197async function listProfileDirEntries(profileDir: string): Promise<string[]> {198try {199return await fs.promises.readdir(profileDir);200} catch {201return [];202}203}204205async function launchChromeOnce(206url: string,207profileDir: string,208chromePath: string,209): Promise<{ chrome: Awaited<ReturnType<typeof launchChromeBase>>; port: number }> {210const port = await getFreePort();211const chrome = await launchChromeBase({212chromePath,213profileDir,214port,215url,216extraArgs: ['--start-maximized'],217});218219try {220await waitForChromeDebugPort(port, 30_000, { includeLastError: true });221return { chrome, port };222} catch (error) {223killChrome(chrome);224throw error;225}226}227228export async function launchChrome(229url: string,230profileDir: string,231candidates: PlatformCandidates,232chromePathOverride?: string,233): Promise<{ chrome: Awaited<ReturnType<typeof launchChromeBase>>; port: number }> {234const chromePath = chromePathOverride?.trim() || findChromeExecutable(candidates);235if (!chromePath) throw new Error('Chrome not found. Set X_BROWSER_CHROME_PATH env var.');236237try {238return await launchChromeOnce(url, profileDir, chromePath);239} catch (error) {240const entries = await listProfileDirEntries(profileDir);241const shouldRetry = shouldRetryChromeLaunch({242lockArtifactsPresent: hasChromeLockArtifacts(entries),243hasLiveOwner: hasLiveChromeOwner(profileDir),244});245if (!shouldRetry) throw error;246247cleanStaleLockFiles(profileDir);248return await launchChromeOnce(url, profileDir, chromePath);249}250}251252export function getScriptDir(): string {253return path.dirname(fileURLToPath(import.meta.url));254}255256function runBunScript(scriptPath: string, args: string[]): boolean {257const result = spawnSync('npx', ['-y', 'bun', scriptPath, ...args], { stdio: 'inherit' });258return result.status === 0;259}260261export function copyImageToClipboard(imagePath: string): boolean {262const copyScript = path.join(getScriptDir(), 'copy-to-clipboard.ts');263return runBunScript(copyScript, ['image', imagePath]);264}265266export function copyHtmlToClipboard(htmlPath: string): boolean {267const copyScript = path.join(getScriptDir(), 'copy-to-clipboard.ts');268return runBunScript(copyScript, ['html', '--file', htmlPath]);269}270271export function pasteFromClipboard(targetApp?: string, retries = 3, delayMs = 500): boolean {272const pasteScript = path.join(getScriptDir(), 'paste-from-clipboard.ts');273const args = ['--retries', String(retries), '--delay', String(delayMs)];274if (targetApp) args.push('--app', targetApp);275return runBunScript(pasteScript, args);276}277