Skip to content

Instantly share code, notes, and snippets.

@random-robbie
Created April 7, 2026 13:25
Show Gist options
  • Select an option

  • Save random-robbie/cc0915961f4061f7efb2681ccdba04f1 to your computer and use it in GitHub Desktop.

Select an option

Save random-robbie/cc0915961f4061f7efb2681ccdba04f1 to your computer and use it in GitHub Desktop.
extra a users tweets visit their profile and copy it in to console
(async () => {
const delay = ms => new Promise(r => setTimeout(r, ms));
const tweets = new Map();
const extract = () => {
document.querySelectorAll('article[data-testid="tweet"]').forEach(el => {
const textEl = el.querySelector('[data-testid="tweetText"]');
const timeEl = el.querySelector('time');
const likeEl = el.querySelector('[data-testid="like"] span[data-testid="app-text-transition-container"]');
const retweetEl = el.querySelector('[data-testid="retweet"] span[data-testid="app-text-transition-container"]');
const replyEl = el.querySelector('[data-testid="reply"] span[data-testid="app-text-transition-container"]');
const linkEl = el.querySelector('a[href*="/status/"]');
if (!textEl || !timeEl) return;
const text = textEl.innerText.trim();
const datetime = timeEl.getAttribute('datetime');
const url = linkEl ? 'https://twitter.com' + linkEl.getAttribute('href') : '';
const id = url.match(/status\/(\d+)/)?.[1] || datetime;
tweets.set(id, {
datetime,
text: text.replace(/\n/g, ' ').replace(/"/g, '""'),
likes: likeEl?.innerText || '0',
retweets: retweetEl?.innerText || '0',
replies: replyEl?.innerText || '0',
url
});
});
};
const scrollsWithoutNew = 3;
let noNewCount = 0;
let prevSize = 0;
console.log('Collecting tweets... Scroll will auto-run. Press stop to cancel early.');
while (noNewCount < scrollsWithoutNew) {
extract();
const currentSize = tweets.size;
console.log(`Tweets collected: ${currentSize}`);
if (currentSize === prevSize) noNewCount++;
else noNewCount = 0;
prevSize = currentSize;
window.scrollBy(0, 2000);
await delay(1800);
}
extract(); // final pass
const header = ['datetime', 'text', 'likes', 'retweets', 'replies', 'url'];
const rows = [...tweets.values()].map(t =>
header.map(k => `"${t[k]}"`).join(',')
);
const csv = [header.join(','), ...rows].join('\n');
const blob = new Blob([csv], { type: 'text/csv' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `tweets_${Date.now()}.csv`;
a.click();
console.log(`Done. Downloaded ${tweets.size} tweets.`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment