Monday, May 13, 2024

Twitter: collect all links to posts on a page

The following snippet executed in the Developer Console will help you collect all the links to individual posts on a profile. Once the script has been executed, all you'll need to do is to scroll down (or navigate from post to post using the "J" key).

window.links = new Set();

window._updateLinks = () => {
    Array.from(document.querySelectorAll('a > time'))
        .map(node => node.parentNode.href)
        .forEach(link => window.links.add(link));
}

window._handleScroll = () => {
    _updateLinks();
    console.log("Links collected:", window.links.size);
}

window.addEventListener('scroll', _handleScroll);

Once you're ready to collect the data, you can copy it to the clipboard using:

copy(Array.from(window.links).join("\n"));

This will copy the links to all the posts you've scrolled through, one link per line.
Useful e.g. when wanting to archive the content from a profile.