Archive All X.com Posts

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

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         Archive All X.com Posts
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Automatically scroll and archive all posts from any user on X.com into a .txt file
// @author       wez
// @match        https://x.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create a button to trigger the archiving process
    const archiveButton = document.createElement('button');
    archiveButton.textContent = 'Archive All Posts';
    archiveButton.style.position = 'fixed';
    archiveButton.style.top = '60px'; // Move button lower so it doesn't interfere
    archiveButton.style.right = '10px';
    archiveButton.style.zIndex = '10000';
    archiveButton.style.padding = '10px';
    archiveButton.style.backgroundColor = '#1DA1F2';
    archiveButton.style.color = '#ffffff';
    archiveButton.style.border = 'none';
    archiveButton.style.borderRadius = '5px';
    archiveButton.style.cursor = 'pointer';
    document.body.appendChild(archiveButton);

    let tweets = [];
    let isScrolling = false;

    // Function to collect tweets from the page
    const collectTweets = () => {
        const tweetElements = document.querySelectorAll('article div[lang]');
        tweetElements.forEach((tweet) => {
            const tweetText = tweet.innerText;
            if (tweetText) {
                const tweetTime = tweet.closest('article').querySelector('time').getAttribute('datetime');
                const tweetEntry = `${new Date(tweetTime).toLocaleString()}: ${tweetText}`;
                if (!tweets.includes(tweetEntry)) { // Avoid duplicates
                    tweets.push(tweetEntry);
                }
            }
        });
    };

    // Function to scroll the page and load more tweets
    const autoScroll = () => {
        window.scrollTo(0, document.body.scrollHeight); // Scroll to the bottom
        setTimeout(() => {
            collectTweets();
            if (!isScrolling) {
                downloadTweets(); // Download tweets when scrolling stops
            } else {
                autoScroll(); // Continue scrolling
            }
        }, 1500); // Adjust this delay if needed
    };

    // Function to download collected tweets as a .txt file
    const downloadTweets = () => {
        const blob = new Blob([tweets.join('\n\n')], { type: 'text/plain' });
        const link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = 'archived_tweets.txt';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        isScrolling = false; // Reset the scrolling flag
    };

    // Event listener to start archiving
    archiveButton.addEventListener('click', () => {
        isScrolling = true;
        autoScroll();

        // Stop scrolling after 60 seconds (adjust as necessary)
        setTimeout(() => {
            isScrolling = false;
        }, 60000);
    });
})();