Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/clis/twitter/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,41 @@ cli({
],
columns: ['title', 'author', 'content', 'url'],
func: async (page, kwargs) => {
// Extract tweet ID from URL if needed
// Extract tweet ID from URL if needed.
// Article URLs (x.com/i/article/{articleId}) use a different ID than
// tweet status URLs — the GraphQL endpoint needs the parent tweet ID.
let tweetId = kwargs['tweet-id'];
const isArticleUrl = /\/article\/\d+/.test(tweetId);
const urlMatch = tweetId.match(/\/(?:status|article)\/(\d+)/);
if (urlMatch) tweetId = urlMatch[1];

if (isArticleUrl) {
// Navigate to the article page and resolve the parent tweet ID from DOM
await page.goto(`https://x.com/i/article/${tweetId}`);
await page.wait(3);
const resolvedId = await page.evaluate(`
(function() {
var links = document.querySelectorAll('a[href*="/status/"]');
for (var i = 0; i < links.length; i++) {
var m = links[i].href.match(/\\/status\\/(\\d+)/);
if (m) return m[1];
}
var og = document.querySelector('meta[property="og:url"]');
if (og && og.content) {
var m2 = og.content.match(/\\/status\\/(\\d+)/);
if (m2) return m2[1];
}
return null;
})()
`);
if (!resolvedId || typeof resolvedId !== 'string') {
throw new CommandExecutionError(
`Could not resolve article ${tweetId} to a tweet ID. The article page may not contain a linked tweet.`,
);
}
tweetId = resolvedId;
}

// Navigate to the tweet page for cookie context
await page.goto(`https://x.com/i/status/${tweetId}`);
await page.wait(3);
Expand Down