Remove promoted tweets from Twitter
目前為
// ==UserScript==
// @name Twitter - Remove Promoted Tweets
// @description Remove promoted tweets from Twitter
// @author Nick Stakenburg
// @namespace https://gist.githubusercontent.com/staaky
// @license MIT; https://opensource.org/licenses/MIT
// @include http://twitter.com/*
// @include https://twitter.com/*
// @include http://*.twitter.com/*
// @include https://*.twitter.com/*
// @icon https://twitter.com/favicon.ico
// @version 0.1.4
// ==/UserScript==
(() => {
// Regex to match against when looking for promoted tweets
// Add yours based on your html[lang] attribute, test it and message @staaky to contribute
const regexes = {
'en': /^Promoted(.*)?$/,
'jp': /^プロモーション(.*)?$/,
};
const language = document.documentElement.getAttribute('lang');
const regex = regexes[language] || regexes.en;
function removeAdTweets() {
let articles = document.querySelectorAll('article:not([adschecked])');
for (let article of articles) {
article.setAttribute('adschecked', 'adschecked');
let divs = article.getElementsByTagName('div'),
height = article.getBoundingClientRect().height;
for (let div of divs) {
if (regex.test(div.textContent)) {
let parent = div.parentNode,
remove = div;
while (parent && (parent.getBoundingClientRect().height <= height + 1)) {
remove = parent;
parent = parent.parentNode;
}
// can use this for debugging (blanks out the tweets instead of removing them)
//remove.setAttribute('style', 'opacity: 0');
remove.remove();
}
}
}
}
let mutationObserver = new MutationObserver(removeAdTweets);
mutationObserver.observe(document.body, { childList: true, subtree: true });
removeAdTweets();
})();