|
| 1 | +--- |
| 2 | +name: post-to-x |
| 3 | +description: 发布内容和文章到 X (Twitter)。支持带图片的普通推文和 X Articles(长文)。使用真实的 Chrome 和 CDP 来绕过自动化检测。 |
| 4 | +--- |
| 5 | + |
| 6 | +# 发布到 X (Twitter) |
| 7 | + |
| 8 | +使用真实的 Chrome 浏览器发布内容、图片和长篇文章到 X(绕过反机器人检测)。 |
| 9 | + |
| 10 | +## 功能 |
| 11 | + |
| 12 | +- **普通推文**: 文本 + 最多 4 张图片 |
| 13 | +- **X Articles**: 发布带有丰富格式和图片的 Markdown 文件(需要 X Premium) |
| 14 | + |
| 15 | +## 用法 |
| 16 | + |
| 17 | +```bash |
| 18 | +# 仅发布文本 |
| 19 | +/post-to-x "Your post content here" |
| 20 | + |
| 21 | +# 发布带图片的推文 |
| 22 | +/post-to-x "Your post content" --image /path/to/image.png |
| 23 | + |
| 24 | +# 发布带多张图片的推文 (最多 4 张) |
| 25 | +/post-to-x "Your post content" --image img1.png --image img2.png |
| 26 | + |
| 27 | +# 实际提交发布 |
| 28 | +/post-to-x "Your post content" --submit |
| 29 | +``` |
| 30 | + |
| 31 | +## 先决条件 |
| 32 | + |
| 33 | +- 已安装 Google Chrome 或 Chromium |
| 34 | +- 已安装 `bun` (用于运行脚本) |
| 35 | +- 首次运行:在打开的浏览器窗口中登录 X |
| 36 | + |
| 37 | +## 快速开始 (推荐) |
| 38 | + |
| 39 | +直接使用 `x-browser.ts` 脚本: |
| 40 | + |
| 41 | +```bash |
| 42 | +# 预览模式 (不发布) |
| 43 | +npx -y bun ./scripts/x-browser.ts "Hello from Claude!" --image ./screenshot.png |
| 44 | + |
| 45 | +# 实际发布 |
| 46 | +npx -y bun ./scripts/x-browser.ts "Hello!" --image ./photo.png --submit |
| 47 | +``` |
| 48 | + |
| 49 | +脚本流程: |
| 50 | +1. 启动禁用了反检测功能的真实 Chrome |
| 51 | +2. 使用持久化配置文件(只需登录一次) |
| 52 | +3. 通过 CDP 输入文本并粘贴图片 |
| 53 | +4. 等待 30 秒进行预览(或使用 `--submit` 立即发布) |
| 54 | + |
| 55 | +## 手动工作流 |
| 56 | + |
| 57 | +如果您更喜欢通过分步控制: |
| 58 | + |
| 59 | +### 步骤 1: 复制图片到剪贴板 |
| 60 | + |
| 61 | +```bash |
| 62 | +npx -y bun ./scripts/copy-to-clipboard.ts image /path/to/image.png |
| 63 | +``` |
| 64 | + |
| 65 | +### 步骤 2: 使用 Playwright MCP (如果 Chrome 会话可用) |
| 66 | + |
| 67 | +```bash |
| 68 | +# 导航 |
| 69 | +mcp__playwright__browser_navigate url="https://x.com/compose/post" |
| 70 | + |
| 71 | +# 获取元素引用 |
| 72 | +mcp__playwright__browser_snapshot |
| 73 | + |
| 74 | +# 输入文本 |
| 75 | +mcp__playwright__browser_click element="editor" ref="<ref>" |
| 76 | +mcp__playwright__browser_type element="editor" ref="<ref>" text="Your content" |
| 77 | + |
| 78 | +# 粘贴图片 (复制到剪贴板后) |
| 79 | +mcp__playwright__browser_press_key key="Meta+v" # macOS |
| 80 | +# 或 |
| 81 | +mcp__playwright__browser_press_key key="Control+v" # Windows/Linux |
| 82 | + |
| 83 | +# 截图以验证 |
| 84 | +mcp__playwright__browser_take_screenshot filename="preview.png" |
| 85 | +``` |
| 86 | + |
| 87 | +## 参数 |
| 88 | + |
| 89 | +| 参数 | 描述 | |
| 90 | +|-----------|-------------| |
| 91 | +| `<text>` | 发布内容 (位置参数) | |
| 92 | +| `--image <path>` | 图片文件路径 (可重复,最多 4 张) | |
| 93 | +| `--submit` | 实际发布 (默认: 仅预览) | |
| 94 | +| `--profile <dir>` | 自定义 Chrome 配置文件目录 | |
| 95 | + |
| 96 | +## 图片支持 |
| 97 | + |
| 98 | +- 格式: PNG, JPEG, GIF, WebP |
| 99 | +- 每条推文最多 4 张图片 |
| 100 | +- 图片被复制到系统剪贴板,然后通过快捷键粘贴 |
| 101 | + |
| 102 | +## 示例会话 |
| 103 | + |
| 104 | +``` |
| 105 | +User: /post-to-x "Hello from Claude!" --image ./screenshot.png |
| 106 | +
|
| 107 | +Claude: |
| 108 | +1. Runs: npx -y bun ./scripts/x-browser.ts "Hello from Claude!" --image ./screenshot.png |
| 109 | +2. Chrome opens with X compose page |
| 110 | +3. Text is typed into editor |
| 111 | +4. Image is copied to clipboard and pasted |
| 112 | +5. Browser stays open 30s for preview |
| 113 | +6. Reports: "Post composed. Use --submit to post." |
| 114 | +``` |
| 115 | + |
| 116 | +## 故障排除 |
| 117 | + |
| 118 | +- **Chrome not found**: 设置 `X_BROWSER_CHROME_PATH` 环境变量 |
| 119 | +- **Not logged in**: 首次运行会打开 Chrome - 手动登录,Cookies 会被保存 |
| 120 | +- **Image paste fails**: 验证剪贴板脚本: `npx -y bun ./scripts/copy-to-clipboard.ts image <path>` |
| 121 | +- **Rate limited**: 等待几分钟后重试 |
| 122 | + |
| 123 | +## 工作原理 |
| 124 | + |
| 125 | +`x-browser.ts` 脚本使用 Chrome DevTools Protocol (CDP) 来: |
| 126 | +1. 启动真实的 Chrome (非 Playwright),带参数 `--disable-blink-features=AutomationControlled` |
| 127 | +2. 使用持久化配置文件目录来保存登录会话 |
| 128 | +3. 通过 CDP 命令与 X 交互 (Runtime.evaluate, Input.dispatchKeyEvent) |
| 129 | +4. 从系统剪贴板粘贴图片 |
| 130 | + |
| 131 | +这种方法绕过了 X 阻止 Playwright/Puppeteer 的反自动化检测。 |
| 132 | + |
| 133 | +## 注意事项 |
| 134 | + |
| 135 | +- 首次运行需要手动登录 (会话会被保存) |
| 136 | +- 使用 `--submit` 前请务必预览 |
| 137 | +- 操作完成后浏览器会自动关闭 |
| 138 | +- 支持 macOS, Linux, 和 Windows |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +# X Articles (长文发布) |
| 143 | + |
| 144 | +发布 Markdown 文章到 X Articles 编辑器,支持富文本格式和图片。 |
| 145 | + |
| 146 | +## X Article 用法 |
| 147 | + |
| 148 | +```bash |
| 149 | +# 发布 Markdown 文章 (预览模式) |
| 150 | +/post-to-x article /path/to/article.md |
| 151 | + |
| 152 | +# 带自定义封面图片 |
| 153 | +/post-to-x article article.md --cover ./hero.png |
| 154 | + |
| 155 | +# 带自定义标题 |
| 156 | +/post-to-x article article.md --title "My Custom Title" |
| 157 | + |
| 158 | +# 实际发布 (不仅仅是草稿) |
| 159 | +/post-to-x article article.md --submit |
| 160 | +``` |
| 161 | + |
| 162 | +## Articles 先决条件 |
| 163 | + |
| 164 | +- X Premium 订阅 (Articles 功能必需) |
| 165 | +- 已安装 Google Chrome |
| 166 | +- 已安装 `bun` |
| 167 | + |
| 168 | +## Article 脚本 |
| 169 | + |
| 170 | +直接使用 `x-article.ts`: |
| 171 | + |
| 172 | +```bash |
| 173 | +npx -y bun ./scripts/x-article.ts article.md |
| 174 | +npx -y bun ./scripts/x-article.ts article.md --cover ./cover.jpg |
| 175 | +npx -y bun ./scripts/x-article.ts article.md --submit |
| 176 | +``` |
| 177 | + |
| 178 | +## Markdown 格式 |
| 179 | + |
| 180 | +```markdown |
| 181 | +--- |
| 182 | +title: My Article Title |
| 183 | +cover_image: /path/to/cover.jpg |
| 184 | +--- |
| 185 | + |
| 186 | +# Title (becomes article title) |
| 187 | + |
| 188 | +Regular paragraph text with **bold** and *italic*. |
| 189 | + |
| 190 | +## Section Header |
| 191 | + |
| 192 | +More content here. |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +- List item 1 |
| 197 | +- List item 2 |
| 198 | + |
| 199 | +1. Numbered item |
| 200 | +2. Another item |
| 201 | + |
| 202 | +> Blockquote text |
| 203 | +
|
| 204 | +[Link text](https://example.com) |
| 205 | + |
| 206 | +\`\`\` |
| 207 | +Code blocks become blockquotes (X doesn't support code) |
| 208 | +\`\`\` |
| 209 | +``` |
| 210 | + |
| 211 | +## Frontmatter 字段 |
| 212 | + |
| 213 | +| 字段 | 描述 | |
| 214 | +|-------|-------------| |
| 215 | +| `title` | 文章标题 (或使用第一个 H1) | |
| 216 | +| `cover_image` | 封面图片路径或 URL | |
| 217 | +| `cover` | cover_image 的别名 | |
| 218 | +| `image` | cover_image 的别名 | |
| 219 | + |
| 220 | +## 图片处理 |
| 221 | + |
| 222 | +1. **封面图片**: 第一张图片或 Frontmatter 中的 `cover_image` |
| 223 | +2. **远程图片**: 自动下载到临时目录 |
| 224 | +3. **占位符**: 内容中的图片使用 `[[IMAGE_PLACEHOLDER_N]]` 格式 |
| 225 | +4. **插入**: 找到占位符,选中,并将其替换为实际图片 |
| 226 | + |
| 227 | +## Markdown 转 HTML 脚本 |
| 228 | + |
| 229 | +转换 Markdown 并检查结构: |
| 230 | + |
| 231 | +```bash |
| 232 | +# 获取包含所有元数据的 JSON |
| 233 | +npx -y bun ./scripts/md-to-html.ts article.md |
| 234 | + |
| 235 | +# 仅输出 HTML |
| 236 | +npx -y bun ./scripts/md-to-html.ts article.md --html-only |
| 237 | + |
| 238 | +# 保存 HTML 到文件 |
| 239 | +npx -y bun ./scripts/md-to-html.ts article.md --save-html /tmp/article.html |
| 240 | +``` |
| 241 | + |
| 242 | +JSON 输出: |
| 243 | +```json |
| 244 | +{ |
| 245 | + "title": "Article Title", |
| 246 | + "coverImage": "/path/to/cover.jpg", |
| 247 | + "contentImages": [ |
| 248 | + { |
| 249 | + "placeholder": "[[IMAGE_PLACEHOLDER_1]]", |
| 250 | + "localPath": "/tmp/x-article-images/img.png", |
| 251 | + "blockIndex": 5 |
| 252 | + } |
| 253 | + ], |
| 254 | + "html": "<p>Content...</p>", |
| 255 | + "totalBlocks": 20 |
| 256 | +} |
| 257 | +``` |
| 258 | + |
| 259 | +## 支持的格式 |
| 260 | + |
| 261 | +| Markdown | HTML 输出 | |
| 262 | +|----------|-------------| |
| 263 | +| `# H1` | 仅标题 (不在正文中) | |
| 264 | +| `## H2` - `###### H6` | `<h2>` | |
| 265 | +| `**bold**` | `<strong>` | |
| 266 | +| `*italic*` | `<em>` | |
| 267 | +| `[text](url)` | `<a href>` | |
| 268 | +| `> quote` | `<a>` | |
| 269 | +| `` `code` `` | `<code>` | |
| 270 | +| ```` ``` ```` | `<blockquote>` (X 限制) | |
| 271 | +| `- item` | `<ul><li>` | |
| 272 | +| `1. item` | `<ol><li>` | |
| 273 | +| `` | Image placeholder | |
| 274 | + |
| 275 | +## Article 工作流 |
| 276 | + |
| 277 | +1. **解析 Markdown**: 提取标题、封面、内容图片,生成 HTML |
| 278 | +2. **启动 Chrome**: 带有 CDP 的真实浏览器,持久化登录 |
| 279 | +3. **导航**: 打开 `x.com/compose/articles` |
| 280 | +4. **创建文章**: 如果在列表页,点击创建按钮 |
| 281 | +5. **上传封面**: 使用文件输入上传封面图片 |
| 282 | +6. **填写标题**: 将标题输入到标题字段 |
| 283 | +7. **粘贴内容**: 将 HTML 复制到剪贴板,粘贴到编辑器 |
| 284 | +8. **插入图片**: 对于每个占位符 (倒序): |
| 285 | + - 在编辑器中找到占位符文本 |
| 286 | + - 选中占位符 |
| 287 | + - 将图片复制到剪贴板 |
| 288 | + - 粘贴以替换选中内容 |
| 289 | +9. **预览**: 浏览器保持打开 60 秒以供预览 |
| 290 | +10. **发布**: 仅在使用 `--submit` 标志时发布 |
| 291 | + |
| 292 | +## Article 示例会话 |
| 293 | + |
| 294 | +``` |
| 295 | +User: /post-to-x article ./blog/my-post.md --cover ./thumbnail.png |
| 296 | +
|
| 297 | +Claude: |
| 298 | +1. Parses markdown: title="My Post", 3 content images |
| 299 | +2. Launches Chrome with CDP |
| 300 | +3. Navigates to x.com/compose/articles |
| 301 | +4. Clicks create button |
| 302 | +5. Uploads thumbnail.png as cover |
| 303 | +6. Fills title "My Post" |
| 304 | +7. Pastes HTML content |
| 305 | +8. Inserts 3 images at placeholder positions |
| 306 | +9. Reports: "Article composed. Review and use --submit to publish." |
| 307 | +``` |
| 308 | + |
| 309 | +## Article 故障排除 |
| 310 | + |
| 311 | +- **No create button**: 确保 X Premium 订阅处于活动状态 |
| 312 | +- **Cover upload fails**: 检查文件路径和格式 (PNG, JPEG) |
| 313 | +- **Images not inserting**: 验证粘贴的内容中是否存在占位符 |
| 314 | +- **Content not pasting**: 检查 HTML 剪贴板: `npx -y bun ./scripts/copy-to-clipboard.ts html --file /tmp/test.html` |
| 315 | + |
| 316 | +## Article 发布工作原理 |
| 317 | + |
| 318 | +1. `md-to-html.ts` 将 Markdown 转换为 HTML: |
| 319 | + - 提取 Frontmatter (标题, 封面) |
| 320 | + - 将 Markdown 转换为 HTML |
| 321 | + - 用唯一的占位符替换图片 |
| 322 | + - 将远程图片下载到本地 |
| 323 | + - 返回结构化 JSON |
| 324 | + |
| 325 | +2. `x-article.ts` 通过 CDP 发布: |
| 326 | + - 启动真实 Chrome (绕过检测) |
| 327 | + - 使用持久化配置文件 (已保存的登录) |
| 328 | + - 通过 DOM 操作导航和填充编辑器 |
| 329 | + - 从系统剪贴板粘贴 HTML |
| 330 | + - 查找/选择/替换每个图片占位符 |
| 331 | + |
| 332 | +## 脚本参考 |
| 333 | + |
| 334 | +| 脚本 | 用途 | |
| 335 | +|--------|---------| |
| 336 | +| `x-browser.ts` | 普通推文 (文本 + 图片) | |
| 337 | +| `x-article.ts` | Article 发布 (Markdown) | |
| 338 | +| `md-to-html.ts` | Markdown → HTML 转换 | |
| 339 | +| `copy-to-clipboard.ts` | 复制图片/HTML 到剪贴板 | |
0 commit comments