Archive All X.com Posts

Automatically scroll and archive all posts from any user on X.com into a .txt file

  1. // ==UserScript==
  2. // @name Archive All X.com Posts
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Automatically scroll and archive all posts from any user on X.com into a .txt file
  6. // @author wez
  7. // @match https://x.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Create a button to trigger the archiving process
  16. const archiveButton = document.createElement('button');
  17. archiveButton.textContent = 'Archive All Posts';
  18. archiveButton.style.position = 'fixed';
  19. archiveButton.style.top = '60px'; // Move button lower so it doesn't interfere
  20. archiveButton.style.right = '10px';
  21. archiveButton.style.zIndex = '10000';
  22. archiveButton.style.padding = '10px';
  23. archiveButton.style.backgroundColor = '#1DA1F2';
  24. archiveButton.style.color = '#ffffff';
  25. archiveButton.style.border = 'none';
  26. archiveButton.style.borderRadius = '5px';
  27. archiveButton.style.cursor = 'pointer';
  28. document.body.appendChild(archiveButton);
  29.  
  30. let tweets = [];
  31. let isScrolling = false;
  32.  
  33. // Function to collect tweets from the page
  34. const collectTweets = () => {
  35. const tweetElements = document.querySelectorAll('article div[lang]');
  36. tweetElements.forEach((tweet) => {
  37. const tweetText = tweet.innerText;
  38. if (tweetText) {
  39. const tweetTime = tweet.closest('article').querySelector('time').getAttribute('datetime');
  40. const tweetEntry = `${new Date(tweetTime).toLocaleString()}: ${tweetText}`;
  41. if (!tweets.includes(tweetEntry)) { // Avoid duplicates
  42. tweets.push(tweetEntry);
  43. }
  44. }
  45. });
  46. };
  47.  
  48. // Function to scroll the page and load more tweets
  49. const autoScroll = () => {
  50. window.scrollTo(0, document.body.scrollHeight); // Scroll to the bottom
  51. setTimeout(() => {
  52. collectTweets();
  53. if (!isScrolling) {
  54. downloadTweets(); // Download tweets when scrolling stops
  55. } else {
  56. autoScroll(); // Continue scrolling
  57. }
  58. }, 1500); // Adjust this delay if needed
  59. };
  60.  
  61. // Function to download collected tweets as a .txt file
  62. const downloadTweets = () => {
  63. const blob = new Blob([tweets.join('\n\n')], { type: 'text/plain' });
  64. const link = document.createElement('a');
  65. link.href = URL.createObjectURL(blob);
  66. link.download = 'archived_tweets.txt';
  67. document.body.appendChild(link);
  68. link.click();
  69. document.body.removeChild(link);
  70. isScrolling = false; // Reset the scrolling flag
  71. };
  72.  
  73. // Event listener to start archiving
  74. archiveButton.addEventListener('click', () => {
  75. isScrolling = true;
  76. autoScroll();
  77.  
  78. // Stop scrolling after 60 seconds (adjust as necessary)
  79. setTimeout(() => {
  80. isScrolling = false;
  81. }, 60000);
  82. });
  83. })();