Skip to content

Instantly share code, notes, and snippets.

@pingiun
Created December 11, 2022 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pingiun/ceb065aca92f078e82647396f65aea6a to your computer and use it in GitHub Desktop.
Save pingiun/ceb065aca92f078e82647396f65aea6a to your computer and use it in GitHub Desktop.
Long Tweet Remover userscript
// ==UserScript==
// @name Long Tweet Remover
// @version 0.1
// @description remove tweets with more than 280 characters
// @author Jelle Besseling
// @match https://twitter.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant none
// ==/UserScript==
(async function() {
'use strict';
// Set this to 140 for old school twitter
const removeLongerThan = 280;
const observer = new MutationObserver(removeTweets);
const tweetList = await waitForTweetList();
observer.observe(tweetList, {childList: true});
console.log("Long Tweet Remover installed");
removeTweets();
async function waitForTweetList() {
function getTweetList() {
const node = document.querySelector('[aria-label="Timeline: Your Home Timeline"');
if (node !== null) {
return node.firstChild;
}
return null;
}
return new Promise((resolve) => {
function waitThenTryAgain() {
const tweetList = getTweetList();
if (tweetList !== null) {
return resolve(tweetList);
}
setTimeout(waitThenTryAgain, 500)
}
waitThenTryAgain();
});
}
function countChars(node) {
let count = 0;
for (const child of node.children) {
// Count emoji
if (child.tagName == "IMG") {
count += 1;
} else if (child.tagName == "SPAN") {
count += child.textContent.length;
}
}
return count;
}
function removeTweets() {
for (const tweet of document.querySelectorAll('[data-testid="tweetText"]')) {
if (countChars(tweet) > removeLongerThan) {
// Yea this might break if they modify the twitter app
let parent = tweet.parentElement;
while (parent.parentElement !== tweetList) {
parent = parent.parentElement;
}
// If we just remove the DOM node, the twitter React app gets confused
parent.style = "display: none;"
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment