Twitter Promotion Filter

Hides promotion on twitter

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitter Promotion Filter
// @name:ja      Twitterプロモーションフィルター
// @namespace    https://greasyfork.org/ja/users/175598
// @version      23.03.1
// @description  Hides promotion on twitter
// @description:ja Twitterのプロモーションを隠します
// @author       N.Y.Boyu
// @match        https://twitter.com/*
// @match        https://mobile.twitter.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function(){
    var MutationObserver=window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver;
    if(MutationObserver)console.log("TPF: Twitter Promotion Filter is enabled.");

    var style=document.createElement("style");
    style.textContent='[tabindex="0"][data-testid][tpf-checked="hidden"]{ display:none!important; }';
    document.head.appendChild(style);

    var root=document.getElementById("react-root")||document.body;
    var check=function(){
        var targets=root.querySelectorAll('[tabindex="0"][data-testid]:not([tpf-checked])'),elems,i,j,m,n;
        topfor: for(i=0,m=targets.length;i<m;i++){
            elems=targets[i].getElementsByTagName("path");
            for(j=0,n=elems.length;j<n;j++){
                if(elems[j].getAttribute("d")==="M19.498 3h-15c-1.381 0-2.5 1.12-2.5 2.5v13c0 1.38 1.119 2.5 2.5 2.5h15c1.381 0 2.5-1.12 2.5-2.5v-13c0-1.38-1.119-2.5-2.5-2.5zm-3.502 12h-2v-3.59l-5.293 5.3-1.414-1.42L12.581 10H8.996V8h7v7z"){
                    console.log("TPF: Hit "+targets[i].dataset.testid);
                    targets[i].setAttribute("tpf-checked","hidden");
                    continue topfor;
                }
            }
            /*
            elems=targets[i].querySelectorAll('div:last-child > svg[viewBox="0 0 24 24"]:first-child + :last-child > span:last-child'); //Change here for other language (e.g. '(...) > span:first-child' for English)
            for(j=0,n=elems.length;j<n;j++){
                if(elems[j].textContent.slice(-7)==="プロモーション"){ //Change here for other language (e.g. slice(0,8)==="Promoted" for English)
                    console.log("TPF: Hit");
                    targets[i].parentNode.removeChild(targets[i]);
                    continue topfor;
                }
            }
            */
            targets[i].setAttribute("tpf-checked","");
        }
    };
    new MutationObserver(check).observe(root,{childList:true,subtree:true});
    check();
})();