Last active
May 9, 2022 10:17
-
-
Save pringshia/3f08e5c7454416e4c94d9f70f0fc6e52 to your computer and use it in GitHub Desktop.
"Enhanced" Twitter script for Tampermonkey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name "Enhanced" Twitter | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Remove tweets shown because of a follower's likes or follow. Dim RTs. | |
// @author You | |
// @match https://twitter.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
/* Twitter - fixed: */ | |
history.pushState = ( f => function pushState(){ | |
var ret = f.apply(this, arguments); | |
window.dispatchEvent(new Event('pushState')); | |
window.dispatchEvent(new Event('locationchange')); | |
return ret; | |
})(history.pushState); | |
history.replaceState = ( f => function replaceState(){ | |
var ret = f.apply(this, arguments); | |
window.dispatchEvent(new Event('replaceState')); | |
window.dispatchEvent(new Event('locationchange')); | |
return ret; | |
})(history.replaceState); | |
window.addEventListener('popstate',()=>{ | |
window.dispatchEvent(new Event('locationchange')) | |
}); | |
/* add global css styles */ | |
var style = document.createElement('style'); | |
var globalStyles = '.hover-show:hover { opacity: 1 !important; }' | |
style.type='text/css'; | |
if (style.styleSheet){ | |
style.styleSheet.cssText = globalStyles; | |
} else { | |
style.appendChild(document.createTextNode(globalStyles)); | |
} | |
document.getElementsByTagName('head')[0].appendChild(style); | |
function searchForText(searchText, tag="span") { | |
var aTags = document.getElementsByTagName(tag); | |
var found = []; | |
for (var i = 0; i < aTags.length; i++) { | |
if (aTags[i].textContent.indexOf(searchText) >= 0) { | |
found.push(aTags[i]); | |
// break; | |
} | |
} | |
return found; | |
} | |
function collectionHas(a, b) { //helper function (see below) | |
for(var i = 0, len = a.length; i < len; i ++) { | |
if(a[i] == b) return true; | |
} | |
return false; | |
} | |
function findParentBySelector(elm, selector) { | |
var all = document.querySelectorAll(selector); | |
var cur = elm.parentNode; | |
while(cur && !collectionHas(all, cur)) { //keep going up until you find a match | |
cur = cur.parentNode; //go up | |
} | |
return cur; //will return null if not found | |
} | |
var resizeObserver = new ResizeObserver(entries => { | |
console.log("resized") | |
for (let entry of entries) { | |
[{ | |
searchTerm: " Retweeted", | |
getParent: (el) => findParentBySelector(el, "article").parentElement.parentElement, | |
action: (tweet) => { | |
tweet.style.opacity = 0.3; | |
tweet.style.transition = "opacity 0.2s"; | |
tweet.classList.add("hover-show"); | |
} | |
}, | |
{ | |
searchTerm: " liked", | |
getParent: (el) => findParentBySelector(el, "article").parentElement.parentElement, | |
action: (tweet) => { | |
tweet.style.opacity = 0.05; | |
tweet.style.transition = "opacity 0.1s"; | |
tweet.classList.add("hover-show"); | |
} | |
}, | |
{ | |
searchTerm: "Promoted", | |
getParent: (el) => { | |
try { | |
return findParentBySelector(el, "article").parentElement.parentElement; | |
} catch (err) { | |
return null; | |
} | |
}, | |
action: (tweet) => { | |
tweet.style.opacity = 0; | |
tweet.style.transition = "opacity 0.1s"; | |
/* tweet.style.visibility="hidden"; */ | |
} | |
} | |
].forEach(item => { | |
var offendingElements = searchForText(item.searchTerm); | |
if (offendingElements.length === 0) return; | |
var tweets = offendingElements.map(item.getParent).filter(el => !!el); | |
tweets.forEach(tweet => { | |
console.log("tweet removed") | |
item.action(tweet) | |
}) | |
}); | |
} | |
}); | |
setTimeout(enhanceTimeline, 1500) | |
window.addEventListener('locationchange', () => { | |
enhanceTimeline(); | |
}) | |
function enhanceTimeline() { | |
var el = document.querySelector("[aria-label='Timeline: Your Home Timeline']"); | |
if (!el) { | |
setTimeout(enhanceTimeline, 1000); | |
return; | |
} | |
console.log(el) | |
resizeObserver.observe(el); | |
setTimeout(() => { | |
document.querySelector('[data-testid="sidebarColumn"]').style.visibility="hidden"; | |
}, 100) | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script is not recognized as compatible with Tampermonkey 4.16.6160 (6 April 2022), Mozilla Firefox 100.0 (64-bit).