Twitter X Following JS Exporter using Old Twitter Layout (2024)

Thomas A. Fink™
4 min readNov 9, 2024

--

The old Twitter layout.

This script allows you to export both names and usernames from the following page on Twitter, using the old Twitter layout (2024). It automatically loads more users by clicking the “Load More” button and saves the data as a JSON file.

Requirements

  1. Old Twitter Layout Chrome Extension:
    To use this script, you need to install the Chrome extension that reverts Twitter to the old layout and loads more users: Old Twitter Layout 2024 Extension

How to Use

Install the Chrome Extension:
Make sure you have installed the Old Twitter Layout 2024 extension to revert Twitter to its older design.

Navigate to the Twitter Following Tab:

  • Go to the profile page of the user whose following you want to export.
  • Click on the “Following” tab.

Open the Browser Console:

  • Press F12 or Ctrl + Shift + I to open the Developer Tools.
  • Click on the Console tab.

Paste the Script:

  • Copy the provided script.
  • Paste it into the console.
  • If prompted, type “allow pasting” and paste the script again, then press Enter.
async function exportUsers() {
const users = new Map();

// Function to click "Load more" button and check if new users are loaded
async function loadMoreUsers() {
const loadMoreButton = document.getElementById("following-more");
let previousUserCount = 0;

while (loadMoreButton) {
const currentUserCount = document.querySelectorAll('.user-item').length;

// Click the "Load more" button
loadMoreButton.click();
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait 2 seconds for users to load

// Check if new users have been loaded
const newUserCount = document.querySelectorAll('.user-item').length;
if (newUserCount === currentUserCount) {
console.log("No more users to load.");
break;
}

previousUserCount = newUserCount;
}
}

// Function to extract both names and usernames
function extractUsers() {
const userElements = document.querySelectorAll('.user-item');

userElements.forEach(element => {
const nameElement = element.querySelector('.user-item-name');
const usernameElement = element.querySelector('.tweet-header-handle');

if (nameElement && usernameElement) {
const name = nameElement.innerText.trim();
const username = usernameElement.innerText.trim();

if (name && username && !users.has(username)) {
users.set(username, { name, username });
}
}
});
}

console.log("Loading more users...");
await loadMoreUsers();

console.log("Exporting users...");
extractUsers();

// Convert Map to Array and format it as JSON
const result = Array.from(users.values());
console.log(result);
console.log(`Total unique users exportd: ${result.length}`);

// Download the result as a JSON file
const jsonResult = JSON.stringify(result, null, 2);
const blob = new Blob([jsonResult], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'twitter_users.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
console.log("JSON file downloaded!");
}

exportUsers();

Download the JSON File:
The script will automatically scroll through the page, load more users, and download the data as a twitter_users.json file once complete.

The script inputed in the developer console.

After you finish, disable “allow pasting” again by restoring developer tools. Click the gear settings icon. Then under Preferences scroll to the bottom. Click “Restore defaults and reload”.

Restore defaults and reload

Output Format

The JSON file will have the following format:

[
{ "name": "Bonnie", "username": "@username" },
{ "name": "John Doe", "username": "@username" }
]

Notes

  • This script is specifically designed to work with the old Twitter layout (2024).
  • Make sure you are on the Following tab before running the script.
  • Ensure the “Load More” button is visible on the screen for the script to function correctly.
  • Adjust the script if Twitter’s structure changes in the future.
  • The file will be saved to your default download folder as twitter_users.json.

Troubleshooting

  • If the script stops working, check if Twitter has updated its HTML structure and modify the selectors in the script accordingly.
  • If the script doesn’t download the file, make sure you’ve enabled pop-ups and downloads for the site in your browser settings.
  • If prompted with “allow pasting”, ensure to enable it to paste the script into the console.

--

--

Thomas A. Fink™
Thomas A. Fink™

Written by Thomas A. Fink™

amateur geopol • astro • #design • ios+android software engineering • data science • neural networks • fiat enthusiast • student • 🇺🇸🇪🇺

No responses yet