baoyu-post-to-x
Posts content and articles to X (Twitter). Supports regular posts with images/videos and X Articles (long-form Markdown). In Codex, honor explicit requests for the Codex Chrome plugin/@chrome by using the Chrome Extension workflow; otherwise use Chrome Computer Use when available and fall back to real Chrome CDP scripts only when allowed. Use when user asks to "post to X", "tweet", "publish to Twitter", or "share on X".
1How It Works & Mechanism
Instead of using Playwright/Puppeteer, it drives a real Chrome instance (via CDP connection) so that X perceives a standard browser session; scripts populate content into the real UI, leaving the publish action to the user or an explicit --submit flag.
Uses real Chrome with CDP to bypass anti-automation.
The primary method for attaching images is not CDP synthetic events, but genuine system-level Cmd+V keystrokes: images are first written to the system clipboard, and osascript/xdotool/ydotool sends paste keystrokes to the Chrome window, with CDP synthetic events serving only as a fallback.
Sends real Cmd+V keystroke to Chrome, bypassing CDP's synthetic events that X can detect
Regular posts, video posts, quotes, and long-form articles share the same underlying Chrome layer: resolving or reusing the debugging port, sharing profiles, injecting text via execCommand('insertText') or Input.insertText, and uploading files by directly passing paths via DOM.setFileInputFiles.
const X_COMPOSE_URL = 'https://x.com/compose/post';
Publishing is non-automated by default: scripts stop after populating content and prompt for manual review and submission; clicking tweetButton / publishButton occurs only when --submit is explicitly provided.
Post composed. Please review and click the publish button in the browser.
The credential surface relies on X session cookies: cookies for x.com/twitter.com are read via CDP Network.getCookies, asserting only that auth_token and ct0 exist (values are neither printed nor persisted), with the credential store being the shared Chrome profile.
const REQUIRED_X_SESSION_COOKIES = ['auth_token', 'ct0'] as const;
Reusing an existing running Chrome instance is supported by design: it scans ps aux for processes sharing the same profile and --remote-debugging-port; if found, it connects directly without launching a new browser.
line.includes(options.profileDir) && line.includes("--remote-debugging-port=")
Cross-platform clipboard and keystroke adaptations are built-in: macOS uses inline Swift/AppKit for clipboard writing + osascript for keystrokes; Linux uses wl-copy/xclip + xdotool/ydotool; Windows uses PowerShell STA and SendKeys.
await runCommand('swift', [swiftPath, 'image', imagePath]);
Includes an active pre-check script: verifies Chrome availability, profile isolation, bun, accessibility permissions, clipboard round-trip, paste keystroke capability, and running Chrome process conflicts, exiting with code 1 on failure.
Checks: Chrome, profile isolation, Bun, Accessibility, clipboard, paste keystroke, Chrome conflicts.
2Core Capabilities
3External Dependencies
| Kind | Dependency / Endpoint |
|---|---|
| cli | npx -y bun(跑同目录兄弟脚本与自身) |
| cli | osascript(macOS:激活 Chrome、发送 Cmd+V) |
| cli | swift(macOS:编译内联 AppKit 剪贴板源码) |
| cli | xdotool / ydotool(Linux X11/Wayland 粘贴键) |
| cli | powershell.exe / cmd.exe / wslpath(Windows 与 WSL 路径与键击) |
| cli | ps aux / pgrep / which / where(进程与工具探测) |
| cli | 真实 Chrome/Chromium/Edge 二进制(以 CDP 参数启动) |
| network | X 发帖页与长文编辑器 |
| network | 本地 Chrome DevTools 端点(HTTP 探测 + WebSocket CDP) |
| network | markdown 里引用的任意 http(s) 图片会被下载到本地临时目录(经 baoyu-md) |
| package | baoyu-chrome-cdp / baoyu-md / front-matter / highlight.js / marked / remark-* / unified(scripts/package.json 声明) |
4Recommended Governance & Evidence
- SKILL.md / 脚本: Invocation of undocumented or reverse-engineered private APIs / client fingerprint spoofing
- SKILL.md / 脚本参数: Techniques to bypass anti-automation detection, disable host sandboxes, or ignore SSL certificate verification
- SKILL.md / 会话存储: Reading or reusing browser user authentication states and session cookies
- 本地凭据 / 浏览器配置: Reading user login cookies/sessions/keyrings: CDP Network.getCookies reads session cookies for x.com / twitter.com and asserts auth_token and ct0
- scripts/: Executing local scripts or commands: ${BUN_X} {baseDir}/scripts/x-browser.ts|x-video.ts|x-quote.ts|x-article.ts
- 网络端点: Declared external network access: https://x.com/compose/post and https://x.com/compose/articles (real browser navigation)
- 本地工作目录: File I/O and artifact output: $TMPDIR/x-article-content.html (long-form article HTML) and $TMPDIR/x-article-images (temporary directory for article images)
5Second-Pass Verification
- [ok] 反自动化侦测的绕过(判红主因) — README 'bypass anti-automation' + regular-posts.md 的 Cmd+V/合成事件两段 + paste-from-clipboard.ts 顶部注释互相印证;x-browser.ts 里 pasteFromClipboard('Google Chrome', 5, 500) 为首选,CDP 键事件为 fallback。
- [ok] cookie 面边界:只经 CDP 读、不落盘 — x-utils.ts 经 Network.getCookies 取 x.com/twitter.com cookie 并在内存构建 map,仅断言 auth_token/ct0 存在;全目录 grep 未发现写入 cookie 文件或读取 Cookies SQLite('Cookies' 只出现在单测字面量里)。
- [ok] 接管已运行调试会话 — packages/baoyu-chrome-cdp 通过 ps aux 匹配 profileDir + --remote-debugging-port 找端口并连接;x-utils.ts 也自行 spawnSync('ps', ['aux']) 做占用判定。
- [ok] 发布动作默认关闭 — tweetButton/publishButton 的 click 都包在 --submit 分支内;未提交时脚本打印提示让用户自己在浏览器点发布;SKILL.md 另有『必须当轮明确确认』的硬规则。
- [ok] 进程级动作(pkill) — SKILL.md 第 321 行给出 pkill -f "Chrome.*remote-debugging-port" …; sleep 2 的清理配方;该命令不在任何 TS 脚本内,需由 agent 执行——已写入 security.scripts_executed 与 risks。
- [discrepancy] 文档与实现不一致(--disable-blink-features) — references/regular-posts.md 宣称 CDP 路线会用 --disable-blink-features=AutomationControlled 启动 Chrome,但 x-utils.ts 只传 extraArgs: ['--start-maximized'];该参数仅出现在 packages/baoyu-chrome-cdp/src/mermaid.ts 的无头渲染启动里。故『绕过』的实际手段是真实键击 + 真实浏览器,而非该启动参数。
- [ok] 单测是否触碰网络/浏览器 — x-utils.test.ts 与 md-to-html.test.ts 均为纯函数/临时目录测试(锁文件判定、启动重试判定、cookie map、markdown 解析),无网络、无 Chrome 启动。
- [ok] 档位复核(批4 终裁) — 按 Main 终裁与范式红档『反反爬/绕过』字面命中判 red(撤回先前的 orange);同时保留可减轻情节的说明:无 TLS/沙箱降级、无未转义插值、cookie 不落盘、发布默认关闭。
6Verdict & Scenarios
83fb7a73416e3fe3…