Twitter - Remove Promoted Tweets

Remove promoted tweets from Twitter

当前为 2019-01-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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();
})();