Last active
March 29, 2020 09:10
-
-
Save cocoabox/afd0d123b6be50c1b316ec09344c3f43 to your computer and use it in GitHub Desktop.
cocoa-twitter-hide-annoying-tweets.js
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 cocoa hide twitter annoying "Liked by XX" and "Promoted tweets" | |
// @namespace cocoabox.twitter.hide_promoted | |
// @description cocoa hide twitter annoying "Liked by XX" and "Promoted tweets | |
// @include https://twitter.com/* | |
// @version 1.0.0 | |
// @run-at document-start | |
// @grant none | |
// ==/UserScript== | |
(function(){ | |
const SELECTORS = { | |
LABELS : '.css-1dbjc4n.r-1habvwh.r-1iusvr4.r-16y2uox.r-5f2r5o , .css-901oao.css-16my406.r-gwet1z.r-ad9z0x.r-bcqeeo.r-qvutc0', | |
TWEET_BOX : '.css-1dbjc4n.r-1j3t67a', | |
}; | |
const FILTERS = [/[Ll]iked/, /[Pp]romoted/, /[Ff]ollow/, /がフォロー/, /いいねしました/, /プロモーション/]; | |
const DATA_ROOT = '__cocoa_hide_twitter_annoying_tweets__'; | |
/** | |
* finds the nearest Parent of elem that matches selector | |
* @param {HTMLElement} elem | |
* @param {String} selector | |
* @returns {HTMLElement|undefined} | |
*/ | |
function nearest_parent(elem, selector) { | |
let e = elem.parentElement; | |
while (e) { | |
if (e.querySelector(selector)) { | |
return e; | |
} | |
e = e.parentElement; | |
} | |
// returns undefined | |
} | |
/** | |
* hide an element | |
*/ | |
function hide(elem) { | |
elem.style.display = 'none'; | |
} | |
function removeElems() { | |
let count = 0; | |
let labels = document.querySelectorAll(SELECTORS.LABELS); | |
labels = Array.prototype.slice.call(labels); | |
labels = labels.filter( d => { | |
let text = d.textContent; | |
for (let filter of FILTERS) { | |
if (filter instanceof RegExp && text.match(filter)) return true; | |
if (typeof filter === 'string' && text.indexOf(filter) >= 0) return true; | |
} | |
return false; | |
}); | |
return labels.map(l => nearest_parent(l, SELECTORS.TWEET_BOX)).forEach(hide); | |
} | |
let handleScroll = function(evt) { | |
if (window[DATA_ROOT].attempts) { | |
return | |
} | |
window[DATA_ROOT].attempts = 2; | |
window[DATA_ROOT].timer = window.setInterval(function(){ | |
let elems = removeElems(); | |
if (elems.length >=0 ) { | |
console.log(`** got ${elems.length} target elements **`); | |
} | |
window[DATA_ROOT].attempts--; | |
if (window[DATA_ROOT].attempts <= 0) { | |
window.clearInterval( window[DATA_ROOT].timer ); | |
window[DATA_ROOT].timer = false; | |
} | |
}, 1000); | |
}; | |
// main | |
if (window[DATA_ROOT]) { | |
window.removeEventListener('scroll', handleScroll, true); | |
window[DATA_ROOT] = null; | |
} | |
else { | |
window.addEventListener('scroll', handleScroll); | |
window[DATA_ROOT] = {attempts: 0, timer: null}; | |
} | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment