Skip to content

Instantly share code, notes, and snippets.

@jalbam
Last active February 26, 2024 16:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jalbam/1546b944f5074efd20e5bc838ee138f5 to your computer and use it in GitHub Desktop.
Save jalbam/1546b944f5074efd20e5bc838ee138f5 to your computer and use it in GitHub Desktop.
Code to show usernames of people you are following on Twitter, being able to choose certain filters to skip some users (and even skip followers or non-followers)
/*
Show user names of people you are following on Twitter, being able to choose certain filters to skip some users (and even skip followers or non-followers).
This will work for new Twitter web site code structure (it was changed from July 2019).
Instructions:
1) The code may need to be modified depending on the language of your Twitter web site:
* For English language web site, no modification needed.
* For Spanish language web site, remember to set the 'LANGUAGE' variable to "ES".
* For another language, remember to set the 'LANGUAGE' variable to that language and modify the 'WORDS' object to add the words in that language.
2) If you do not want to print information about whether the user is following you or not, set the 'PRINT_FOLLOW_INFORMATION' variable to false.
3) Optionally, you can edit the 'SKIP_USERS' array to insert those users whose names you do not want to print.
4) To skip (do not print) users who are following you, set the 'SKIP_FOLLOWERS' variable to true. Both 'SKIP_FOLLOWERS' and 'SKIP_NON_FOLLOWERS' variables cannot be set to true at the same time.
5) To skip (do not print) users who are not following you, set the 'SKIP_NON_FOLLOWERS' variable to true. Both 'SKIP_FOLLOWERS' and 'SKIP_NON_FOLLOWERS' variables cannot be set to true at the same time.
6) You can set the milliseconds per cycle (each call to the 'printUserNames' function) by modifying the 'MS_PER_CYCLE' variable.
7) When the code is fine, on Twitter web site, go to the section where it shows all the people you are following (https://twitter.com/YOUR_USERNAME_HERE/following).
8) Once there, open the JavaScript console (F12 key, normally), paste all the code there and press enter.
9) Wait until you see it has finished. If something goes wrong or some users were not printed, reload the page and repeat from the step 8 again.
* Gist by Joan Alba Maldonado: https://gist.github.com/jalbam/1546b944f5074efd20e5bc838ee138f5
*/
var LANGUAGE = "EN"; //NOTE: change it to use your language!
var WORDS =
{
//English language:
EN:
{
followsYouText: "Follows you" //Text that informs that follows you.
},
//Spanish language:
ES:
{
followsYouText: "Te sigue" //Text that informs that follows you.
}
//NOTE: if needed, add your language here...
}
var MS_PER_CYCLE = 10; //Milliseconds per cycle (each call to 'printUserNames').
var PRINT_FOLLOW_INFORMATION = true; //Sets whether to also print information about whether the user follows you or not.
var SKIP_FOLLOWERS = false; //Defines whether to avoid printing user names of followers.
var SKIP_NON_FOLLOWERS = false; //Defines whether to avoid printing user names of non-followers.
var SKIP_USERS = //Users whose name we do not want to print:
[
//Place the user names that you want to skip here (they will not be printed):
"user_name_to_skip_example_1",
"user_name_to_skip_example_2",
"user_name_to_skip_example_3"
];
SKIP_USERS.forEach(function(value, index) { SKIP_USERS[index] = value.toLowerCase(); }); //Transforms all the user names to lower case as it will be case insensitive.
//Function that names of the users that you are following on Twitter:
var USERS_PRINTED = {}; //Object that will keep the user names printed already and information about whether they are following you or not.
var printUserNames = function(followsYouText, skipFollowers, skipNonFollowers, printFollowInformation)
{
followsYouText = followsYouText || WORDS.EN.followsYouText; //Text that informs that follows you.
skipFollowers = (skipFollowers === true || skipFollowers === false) ? skipFollowers : SKIP_FOLLOWERS;
skipNonFollowers = (skipNonFollowers === true || skipNonFollowers === false) ? skipNonFollowers : SKIP_NON_FOLLOWERS;
printFollowInformation = (printFollowInformation === true || printFollowInformation === false) ? printFollowInformation : PRINT_FOLLOW_INFORMATION;
if (skipFollowers && skipNonFollowers) { console.log("You cannot skip printing everyone!"); return; }
//Looks through all the containers of each user:
var userContainers = document.querySelectorAll('[data-testid=UserCell]');
Array.prototype.filter.call
(
userContainers,
function(userContainer)
{
//Checks whether the user is following you:
var followsYou = false;
Array.from(userContainer.querySelectorAll("*")).find
(
function(element)
{
if (element.textContent === followsYouText) { followsYou = true; }
}
);
if (followsYou && skipFollowers) { return; }
else if (!followsYou && skipNonFollowers) { return; }
//Finds the user name and checks whether we want to skip this user or not:
var skipUser = false;
var userName = "";
Array.from(userContainer.querySelectorAll("[href^='/']")).find
(
function (element)
{
if (skipUser) { return; }
if (element.href.indexOf("search?q=") !== -1 || element.href.indexOf("/") === -1) { return; }
userName = element.href.substring(element.href.lastIndexOf("/") + 1).toLowerCase();
Array.from(element.querySelectorAll("*")).find
(
function (subElement)
{
if (subElement.textContent.toLowerCase() === "@" + userName)
{
if (SKIP_USERS.indexOf(userName) !== -1)
{
skipUser = true;
}
}
}
);
}
);
//If we do not want to skip the user:
if (!skipUser)
{
//Prints the username:
if (!USERS_PRINTED[userName])
{
console.log("* Username: " + userName + (PRINT_FOLLOW_INFORMATION ? (followsYou ? " follows you" : " does NOT follow you") : ""));
USERS_PRINTED[userName] = { userName: userName, followsYou: followsYou };
}
}
}
);
}
//Scrolls and prints usernames, constantly:
var scrollAndShowUsernames = function()
{
if (SKIP_FOLLOWERS && SKIP_NON_FOLLOWERS) { console.log("You cannot skip printing everyone!"); return; }
window.scrollTo(0, document.body.scrollHeight);
printUserNames(WORDS[LANGUAGE].followsYouText, SKIP_FOLLOWERS, SKIP_NON_FOLLOWERS, PRINT_FOLLOW_INFORMATION); //For English, you can try to call it without parameters.
setTimeout(scrollAndShowUsernames, MS_PER_CYCLE);
};
scrollAndShowUsernames();
@jalbam
Copy link
Author

jalbam commented Aug 10, 2019

Show user names of people you are following on Twitter, being able to choose certain filters to skip some users (and even skip followers or non-followers).

This will work for new Twitter web site code structure (it was changed from July 2019).

Instructions:

  1. The code may need to be modified depending on the language of your Twitter web site:
    • For English language web site, no modification needed.
    • For Spanish language web site, remember to set the 'LANGUAGE' variable to "ES".
    • For another language, remember to set the 'LANGUAGE' variable to that language and modify the 'WORDS' object to add the words in that language.
  2. If you do not want to print information about whether the user is following you or not, set the 'PRINT_FOLLOW_INFORMATION' variable to false.
  3. Optionally, you can edit the 'SKIP_USERS' array to insert those users whose names you do not want to print.
  4. To skip (do not print) users who are following you, set the 'SKIP_FOLLOWERS' variable to true. Both 'SKIP_FOLLOWERS' and 'SKIP_NON_FOLLOWERS' variables cannot be set to true at the same time.
  5. To skip (do not print) users who are not following you, set the 'SKIP_NON_FOLLOWERS' variable to true. Both 'SKIP_FOLLOWERS' and 'SKIP_NON_FOLLOWERS' variables cannot be set to true at the same time.
  6. You can set the milliseconds per cycle (each call to the 'printUserNames' function) by modifying the 'MS_PER_CYCLE' variable.
  7. When the code is fine, on Twitter web site, go to the section where it shows all the people you are following (https://twitter.com/YOUR_USERNAME_HERE/following).
  8. Once there, open the JavaScript console (F12 key, normally), paste all the code there and press enter.
  9. Wait until you see it has finished. If something goes wrong or some users were not printed, reload the page and repeat from the step 8 again.

Gist by Joan Alba Maldonado: https://gist.github.com/jalbam/1546b944f5074efd20e5bc838ee138f5

Thank you very much. Any comments are welcome.

@roshanmirajkar
Copy link

This branched off gist from Main is amazing.

I have some ideas on expanding this project out. Would like to move it into a Repo. What do you think?

I could help maintain. Would be cool to use GPT to analyze followers daily, client-side. Open-source.
Could also interface with Twitter Open-Source algorithm.

gist:
https://gist.github.com/roshanmirajkar/afbefda0bd127e0c3df42ac80d6eae6f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment