Skip to content

Instantly share code, notes, and snippets.

@winkler1
Last active December 25, 2022 20:34
Show Gist options
  • Save winkler1/9347598b94f0eb20d7178c8a90333e7a to your computer and use it in GitHub Desktop.
Save winkler1/9347598b94f0eb20d7178c8a90333e7a to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Cleanup Twitter.
// @include https://twitter.com/*
// ==/UserScript==
// v12: Hover over username / avatar for 100% opacity.
// v13: Add link to demo. https://www.youtube.com/watch?v=07uYCbxDcqU
// v14: refactor rm() and addClass() functions. Remove Messages popup at lower-right. Opacity more subtle.
// Use with TamperMonkey: https://www.tampermonkey.net/
function rm(sel) { document.querySelectorAll(sel).forEach((e) => e.remove()); }
function addClass(sel,clz) { document.querySelectorAll(sel).forEach((e) => e.classList.add(clz)); }
function cleanup() {
const start = performance.now();
["Twitter", "Verified account","Trending", "Twitter Blue", "Bookmarks", "Share Tweet"].forEach(function(str) {
rm(`[aria-label="${str}"]`);
});
["Home", "Search and explore", "Notifications", "Direct Messages", "Profile","Account menu"].forEach(function(str) {
addClass(`[aria-label="${str}"]`,'uxButton');
});
rm(`[aria-label*="View Tweet analytics"]`);
for (const span of document.querySelectorAll('span')) {
if (span.textContent === 'Show this thread') {
span.remove();
}
}
addClass('[data-testid="User-Names"]', 'tweakedUsername');
rm('[data-testid="DMDrawer"]');
["More", ". Reply", ". Retweet", ". Like"].forEach(function(str) {
addClass(`[aria-label*="${str}"]`,'uxButton');
});
addClass('div[data-testid="Tweet-User-Avatar"]','tweakedAvatar');
addClass('a[data-testid="SideNav_NewTweet_Button"]','tweakedTweet');
const end = performance.now();
//console.log(`Code took ${end - start} milliseconds to run.`);
}
var styleElement = document.createElement('style', {type: 'text/css'});
styleElement.innerHTML = ".tweakedUsername{opacity:0.1}.tweakedUsername:hover{opacity:1}"+
".tweakedTweet{opacity:0.4}.tweakedAvatar{opacity:0.65}.tweakedAvatar:hover{opacity:1}"+
".uxButton {opacity: 0.45;}.uxButton:hover {opacity: 1;}";
document.head.appendChild(styleElement);
function debounce(fn, delay) {
let timer = null;
return function() {
clearTimeout(timer);
timer = setTimeout(function() {
fn.apply(this, arguments);
}, delay);
};
}
var debouncedCleanup = debounce(cleanup, 150);
cleanup();
var observer = new MutationObserver(function(mutations) {
for (var mutation of mutations) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
debouncedCleanup();
return;
}
}
});
observer.observe(document.body, {
childList: true, subtree: true
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment