Binary operations with lists on Twitter web

Binary operations with Twitter lists via web client

For quite a while now, Twitter has lists. Lists are nice - you can include people with them, and then view their posting on a separate page. Or view multiple of these at once if you are using TweetDeck.

The process of managing lists leaves some to be desired, though - if you want to do something like

  • Add all followed users to a list
  • Exclude all followed users from a list
  • Add all followers to a list
  • Add all members from a list to other list
  • Exclude all members of a list from another list

You are apparently expected to do so by using the little context menu on each user, picking "Add or remove from lists...", and then ticking/unticking the checkbox for the according list.

That's no fun. But, of course, this can be helped with a bit of JavaScript.

So the following snippet very literally just clicks on all the context menus on the page, picks the needed menu item, and ticks/unticks the checkbox for the given list. Instructions included.

// 1. Open the target page (e.g. https://twitter.com/following).
// 2. Scroll to the bottom (so that all desired user bios are loaded).
// 3. Open developer tools (F12), pick "console" tab.
// 4. Paste this entire script there.
// 5. Change the list name & target status (two lines below).
// 6. Hit Enter and watch it click the checkboxes.
var targetList = "MyListName"; // insert your list name here
var targetStatus = true; // true -> include, false -> exclude [everyone from the page]
var dropdowns = document.getElementsByClassName("user-dropdown");
var current = null;
var index = 0;
function onNextItem() {
	current = dropdowns[index++];
	console.log(index + "/" + dropdowns.length);
	// click the "user actions" dropdown:
	current.click();
	// await for menu:
	setTimeout(onMenu, 0);
}
function onMenu() {
	// click "Add or remove from lists":
	current.parentElement.getElementsByClassName("list-text")[0].click();
	// await for opening:
	setTimeout(onListWindow, 100);
}
function onListWindow() {
	// wait for modal window to open:
	var listWindow = document.getElementById("list-membership-dialog");
	var listContainer = document.getElementsByClassName("list-membership-container")[0];
	if (listWindow.style.display != "block" || !listContainer) return setTimeout(onListWindow, 100);
	// seek the desired list among the ones available:
	var listElements = listContainer.children, i;
	for (i = 0; i < listElements.length; i++) {
		var listElement = listElements[i];
		var listName = listElement.textContent.trim();
		if (listName == targetList) { // that's it
			var listCb = listElement.getElementsByTagName("input")[0];
			// click the checkbox if the status doesn't match:
			if (listCb.checked != targetStatus) listCb.click();
			// click the close-button on the modal window:
			listWindow.getElementsByClassName("modal-close")[0].click()
			// await for closure:
			onListClose();
			return;
		}
	}
	// list wasn't around
	alert("Couldn't find list " + targetList + "! Do you actually have a list named so?");
}
function onListClose() {
	// wait for modal window to close:
	var listWindow = document.getElementById("list-membership-dialog");
	if (listWindow.style.display == "block") return setTimeout(onListClose, 100);
	// do the next item, if we aren't out of them yet:
	if (index < dropdowns.length) setTimeout(onNextItem, 100);
}
onNextItem();

Obviously, this is a relatively simple trick, but regardless can be very useful, especially since Twitter's web client seems to remain the only method of accessing some of it's data in full extent.

Have fun!

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.