FixAnnoyingTwitterCrap

Remove annoying crap from Twitter

当前为 2020-02-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         FixAnnoyingTwitterCrap
// @version      0.1.4
// @description  Remove annoying crap from Twitter
// @author       Jeremy Bornstein <[email protected]>
// @match        https://twitter.com/*
// @grant        none
// @namespace https://gf.qytechs.cn/users/414927
// ==/UserScript==

(function() {
    'use strict';

    const debug = false;

    function removeMatchingNodes(parent, selector, predicate, findParent, debugColor) {
        const nodes = parent.querySelectorAll(selector);
        for (let i = 0; i < nodes.length; i++) {
            const node = nodes[i];
            if (predicate(node)) {
                const offendingNode = findParent(node);
                if (debug) {
                    offendingNode.style.backgroundColor = debugColor;
                    console.debug("TARGET", offendingNode);
                } else {
                    //offendingNode.parentNode.removeChild(offendingNode);
                    offendingNode.hidden = true;
                    offendingNode.style.display = "none";
                }
            }
        }
    }

    function removeAds(parent) {
        function isTargetNode(n) {
            const nodeText = n.innerText;
            return nodeText == "Sponsorisé"
                || nodeText == "Sponsored"
                || nodeText == "Promoted"
        }
        removeMatchingNodes(parent, 'span', isTargetNode, n => n.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode, 'black');
    }

    function removeTrendAds(parent) {
        function isTargetNode(n) {
            const nodeText = n.innerText;
            return nodeText.startsWith("Promoted by ")
                || nodeText.startsWith("Sponsored by ")
                || nodeText.startsWith("Sponsorisé par ")
        }

        removeMatchingNodes(parent, 'span', isTargetNode, n => n.parentNode.parentNode.parentNode.parentNode.parentNode, 'pink');
    }


    function removeFollowedBy(parent) {
        function isTargetNode(n) {
            const nodeText = n.innerText;
            return nodeText.endsWith(" suivent")
            || nodeText.endsWith(" suit")
            || nodeText.endsWith(" follow");
        }

        removeMatchingNodes(parent, 'a', isTargetNode, n => n.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode, 'blue');
    }

    function removeLikedBy(parent) {
        function isTargetNode(n) {
            const nodeText = n.innerText;
            return nodeText.endsWith(" a aimé") 
                || nodeText.endsWith(" ont aimé")
                || nodeText.endsWith(" liked");
        }

        removeMatchingNodes(parent, 'a', isTargetNode, n => n.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode, 'yellow');
    }

    removeLikedBy(document);
    removeFollowedBy(document);
    removeAds(document);
    removeTrendAds(document);

    const observer = new MutationObserver(function(mutationsList, observer) {
        for(let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                const addedNodes = mutation.addedNodes;
                for (let node of addedNodes) {
                    removeLikedBy(node);
                    removeFollowedBy(node);
                    removeAds(node);
                    removeTrendAds(node);  // trends are only loaded once (and always dynamically) so theoretically we could disable this after it finds any once.
                }
            }
        }
    });
    observer.observe(document.body, { childList: true, subtree: true });

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址