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 Weibo (微博) via Chrome browser automation.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/weibo-post.ts
1import fs from 'node:fs';2import { mkdir } from 'node:fs/promises';3import path from 'node:path';4import process from 'node:process';5import {6CdpConnection,7findChromeExecutable,8findExistingChromeDebugPort,9getDefaultProfileDir,10killChromeByProfile,11launchChrome as launchWeiboChrome,12sleep,13waitForChromeDebugPort,14} from './weibo-utils.js';1516const WEIBO_HOME_URL = 'https://weibo.com/';1718const MAX_FILES = 18;1920interface WeiboPostOptions {21text?: string;22images?: string[];23videos?: string[];24timeoutMs?: number;25profileDir?: string;26chromePath?: string;27}2829export async function postToWeibo(options: WeiboPostOptions): Promise<void> {30const { text, images = [], videos = [], timeoutMs = 120_000, profileDir = getDefaultProfileDir() } = options;3132const allFiles = [...images, ...videos];33if (allFiles.length > MAX_FILES) {34throw new Error(`Too many files: ${allFiles.length} (max ${MAX_FILES})`);35}3637await mkdir(profileDir, { recursive: true });3839const chromePath = findChromeExecutable(options.chromePath);40if (!chromePath) throw new Error('Chrome not found. Set WEIBO_BROWSER_CHROME_PATH env var.');4142let port: number;43const existingPort = await findExistingChromeDebugPort(profileDir);4445if (existingPort) {46console.log(`[weibo-post] Found existing Chrome on port ${existingPort}, checking health...`);47try {48const wsUrl = await waitForChromeDebugPort(existingPort, 5_000);49const testCdp = await CdpConnection.connect(wsUrl, 5_000, { defaultTimeoutMs: 5_000 });50await testCdp.send('Target.getTargets');51testCdp.close();52console.log('[weibo-post] Existing Chrome is responsive, reusing.');53port = existingPort;54} catch {55console.log('[weibo-post] Existing Chrome unresponsive, restarting...');56killChromeByProfile(profileDir);57await sleep(2000);58port = await launchWeiboChrome(WEIBO_HOME_URL, profileDir, chromePath);59}60} else {61port = await launchWeiboChrome(WEIBO_HOME_URL, profileDir, chromePath);62}6364let cdp: CdpConnection | null = null;6566try {67const wsUrl = await waitForChromeDebugPort(port, 30_000);68cdp = await CdpConnection.connect(wsUrl, 30_000, { defaultTimeoutMs: 15_000 });6970const targets = await cdp.send<{ targetInfos: Array<{ targetId: string; url: string; type: string }> }>('Target.getTargets');71let pageTarget = targets.targetInfos.find((t) => t.type === 'page' && t.url.includes('weibo.com'));7273if (!pageTarget) {74const { targetId } = await cdp.send<{ targetId: string }>('Target.createTarget', { url: WEIBO_HOME_URL });75pageTarget = { targetId, url: WEIBO_HOME_URL, type: 'page' };76}7778const { sessionId } = await cdp.send<{ sessionId: string }>('Target.attachToTarget', { targetId: pageTarget.targetId, flatten: true });7980await cdp.send('Target.activateTarget', { targetId: pageTarget.targetId });8182await cdp.send('Page.enable', {}, { sessionId });83await cdp.send('Runtime.enable', {}, { sessionId });84await cdp.send('Input.setIgnoreInputEvents', { ignore: false }, { sessionId });8586const currentUrl = await cdp.send<{ result: { value: string } }>('Runtime.evaluate', {87expression: `window.location.href`,88returnByValue: true,89}, { sessionId });9091if (!currentUrl.result.value.includes('weibo.com/') || currentUrl.result.value.includes('card.weibo.com')) {92console.log('[weibo-post] Navigating to Weibo home...');93await cdp.send('Page.navigate', { url: WEIBO_HOME_URL }, { sessionId });94await sleep(3000);95}9697console.log('[weibo-post] Waiting for Weibo editor...');98await sleep(3000);99100const waitForEditor = async (): Promise<boolean> => {101const start = Date.now();102while (Date.now() - start < timeoutMs) {103const result = await cdp!.send<{ result: { value: boolean } }>('Runtime.evaluate', {104expression: `!!document.querySelector('#homeWrap textarea')`,105returnByValue: true,106}, { sessionId });107if (result.result.value) return true;108await sleep(1000);109}110return false;111};112113const editorFound = await waitForEditor();114if (!editorFound) {115console.log('[weibo-post] Editor not found. Please log in to Weibo in the browser window.');116console.log('[weibo-post] Waiting for login...');117const loggedIn = await waitForEditor();118if (!loggedIn) throw new Error('Timed out waiting for Weibo editor. Please log in first.');119}120121if (text) {122console.log('[weibo-post] Typing text...');123124// Focus and use Input.insertText via CDP125await cdp.send('Runtime.evaluate', {126expression: `(() => {127const editor = document.querySelector('#homeWrap textarea');128if (editor) { editor.focus(); editor.value = ''; }129})()`,130}, { sessionId });131await sleep(200);132133await cdp.send('Input.insertText', { text }, { sessionId });134await sleep(500);135136// Verify text was entered137const textCheck = await cdp.send<{ result: { value: string } }>('Runtime.evaluate', {138expression: `document.querySelector('#homeWrap textarea')?.value || ''`,139returnByValue: true,140}, { sessionId });141142if (textCheck.result.value.length > 0) {143console.log(`[weibo-post] Text verified (${textCheck.result.value.length} chars)`);144} else {145console.warn('[weibo-post] Text input appears empty, trying execCommand fallback...');146await cdp.send('Runtime.evaluate', {147expression: `(() => {148const editor = document.querySelector('#homeWrap textarea');149if (editor) { editor.focus(); document.execCommand('insertText', false, ${JSON.stringify(text)}); }150})()`,151}, { sessionId });152await sleep(300);153154const textRecheck = await cdp.send<{ result: { value: string } }>('Runtime.evaluate', {155expression: `document.querySelector('#homeWrap textarea')?.value || ''`,156returnByValue: true,157}, { sessionId });158console.log(`[weibo-post] Text after fallback: ${textRecheck.result.value.length} chars`);159}160}161162if (allFiles.length > 0) {163const missing = allFiles.filter((f) => !fs.existsSync(f));164if (missing.length > 0) {165throw new Error(`Files not found: ${missing.join(', ')}`);166}167168const absolutePaths = allFiles.map((f) => path.resolve(f));169console.log(`[weibo-post] Uploading ${absolutePaths.length} file(s) via file input...`);170171await cdp.send('DOM.enable', {}, { sessionId });172173const { root } = await cdp.send<{ root: { nodeId: number } }>('DOM.getDocument', {}, { sessionId });174175const { nodeId } = await cdp.send<{ nodeId: number }>('DOM.querySelector', {176nodeId: root.nodeId,177selector: '#homeWrap input[type="file"]',178}, { sessionId });179180if (!nodeId || nodeId === 0) {181throw new Error('File input not found. Make sure the Weibo compose area is visible.');182}183184await cdp.send('DOM.setFileInputFiles', {185nodeId,186files: absolutePaths,187}, { sessionId });188189console.log('[weibo-post] Files set on input. Waiting for upload...');190await sleep(2000);191192const uploadCheck = await cdp.send<{ result: { value: number } }>('Runtime.evaluate', {193expression: `document.querySelectorAll('#homeWrap img[src^="blob:"], #homeWrap img[src^="data:"], #homeWrap video').length`,194returnByValue: true,195}, { sessionId });196197if (uploadCheck.result.value > 0) {198console.log(`[weibo-post] Upload verified (${uploadCheck.result.value} media item(s) detected)`);199} else {200console.warn('[weibo-post] Upload may still be in progress. Please verify in browser.');201}202}203204console.log('[weibo-post] Post composed. Please review and click the publish button in the browser.');205console.log('[weibo-post] Browser remains open for manual review.');206207} finally {208if (cdp) {209cdp.close();210}211}212}213214function printUsage(): never {215console.log(`Post to Weibo using real Chrome browser216217Usage:218npx -y bun weibo-post.ts [options] [text]219220Options:221--image <path> Add image (can be repeated)222--video <path> Add video (can be repeated)223--profile <dir> Chrome profile directory224--help Show this help225226Max ${MAX_FILES} files total (images + videos combined).227228Examples:229npx -y bun weibo-post.ts "Hello from CLI!"230npx -y bun weibo-post.ts "Check this out" --image ./screenshot.png231npx -y bun weibo-post.ts "Post it!" --image a.png --image b.png232npx -y bun weibo-post.ts "Watch this" --video ./clip.mp4233`);234process.exit(0);235}236237async function main(): Promise<void> {238const args = process.argv.slice(2);239if (args.includes('--help') || args.includes('-h')) printUsage();240241const images: string[] = [];242const videos: string[] = [];243let profileDir: string | undefined;244const textParts: string[] = [];245246for (let i = 0; i < args.length; i++) {247const arg = args[i]!;248if (arg === '--image' && args[i + 1]) {249images.push(args[++i]!);250} else if (arg === '--video' && args[i + 1]) {251videos.push(args[++i]!);252} else if (arg === '--profile' && args[i + 1]) {253profileDir = args[++i];254} else if (!arg.startsWith('-')) {255textParts.push(arg);256}257}258259const text = textParts.join(' ').trim() || undefined;260261if (!text && images.length === 0 && videos.length === 0) {262console.error('Error: Provide text or at least one image/video.');263process.exit(1);264}265266await postToWeibo({ text, images, videos, profileDir });267}268269await main().catch((err) => {270console.error(`Error: ${err instanceof Error ? err.message : String(err)}`);271process.exit(1);272});273