Files
wehub-resource-sync 9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

59 lines
2.6 KiB
JavaScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import { AuthRequiredError, EmptyResultError } from '@jackwener/opencli/errors';
// ── CLI definition ────────────────────────────────────────────────────
//
// X (Twitter) removed the post-count caption from each trend cell on the
// /explore/tabs/trending page in 2024-2025. The DOM now only carries:
// divs[0] = rank + category (e.g. "1 · Trending in United States")
// divs[1] = topic
// divs[2..] = caret menu button (no post-count text)
// We previously surfaced a `tweets` column whose value was permanently
// "N/A" on every row — that's silent-wrong data, drop it.
cli({
site: 'twitter',
name: 'trending',
access: 'read',
description: 'Twitter/X trending topics',
domain: 'x.com',
strategy: Strategy.COOKIE,
browser: true,
args: [
{ name: 'limit', type: 'int', default: 20, help: 'Number of trends to show' },
],
columns: ['rank', 'topic', 'category'],
func: async (page, kwargs) => {
const limit = kwargs.limit || 20;
// Navigate to trending page
await page.goto('https://x.com/explore/tabs/trending');
await page.wait(3);
// Verify login via CSRF cookie (read directly from cookie store via CDP)
const cookies = await page.getCookies({ url: 'https://x.com' });
const ct0 = cookies.find((c) => c.name === 'ct0')?.value || null;
if (!ct0)
throw new AuthRequiredError('x.com', 'Not logged into x.com (no ct0 cookie)');
await page.wait(2);
const trends = await page.evaluate(`(() => {
const items = [];
const cells = document.querySelectorAll('[data-testid="trend"]');
cells.forEach((cell) => {
const text = cell.textContent || '';
if (text.includes('Promoted')) return;
const container = cell.querySelector(':scope > div');
if (!container) return;
const divs = container.children;
if (divs.length < 2) return;
const topic = divs[1].textContent.trim();
if (!topic) return;
const catText = divs[0].textContent.trim();
const category = catText.replace(/^\\d+\\s*/, '').replace(/^\\xB7\\s*/, '').trim();
items.push({ rank: items.length + 1, topic, category });
});
return items;
})()`);
if (!Array.isArray(trends) || trends.length === 0) {
throw new EmptyResultError('twitter trending', 'No trends found. The page structure may have changed.');
}
return trends.slice(0, limit);
},
});