Skip to content

Instantly share code, notes, and snippets.

@tizee
Last active August 9, 2023 13:17

Revisions

  1. tizee revised this gist Aug 9, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.js
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@
    if (
    [...spans]
    .map((span) => span.innerHTML)
    .some((txt) => txt == "Promoted")
    .some((txt) => txt == "Promoted" || txt == "Ad")
    ) {
    tweet.style.display = "none";
    console.debug("block promoted tweet", tweet);
  2. tizee revised this gist Jul 15, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.js
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@
    });
    }

    const regex = /^https:\/\/twitter.com\/home$/;
    const regex = /^https:\/\/twitter.com\/.*$/;

    const observer = new MutationObserver(function (mutationList, observer) {
    if (!regex.test(window.location.href)) {
  3. tizee revised this gist Jul 15, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    // @version 0.1
    // @description Block twitter promoted tweets at home timeline
    // @author tizee
    // @match https://twitter.com/home
    // @match https://twitter.com/*
    // @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
    // @grant none
    // ==/UserScript==
  4. tizee created this gist Jul 15, 2023.
    59 changes: 59 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    // ==UserScript==
    // @name Twitter promoted blocker
    // @namespace http://tampermonkey.net/
    // @version 0.1
    // @description Block twitter promoted tweets at home timeline
    // @author tizee
    // @match https://twitter.com/home
    // @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
    // @grant none
    // ==/UserScript==

    (function () {
    "use strict";

    const hiddenTweets = new WeakSet();

    function watchTweetNodes(mutationList) {
    mutationList.forEach(function (mutationRecord) {
    mutationRecord.addedNodes.forEach(function (node) {
    if (node && typeof node.querySelector == "function") {
    const tweet = node.querySelector(
    `div[data-testid="cellInnerDiv"] article[data-testid="tweet"]`
    );
    if (tweet) {
    if (hiddenTweets.has(tweet)) {
    return;
    }

    const spans = tweet.querySelectorAll("span");
    if (
    [...spans]
    .map((span) => span.innerHTML)
    .some((txt) => txt == "Promoted")
    ) {
    tweet.style.display = "none";
    console.debug("block promoted tweet", tweet);
    hiddenTweets.add(tweet);
    }
    }
    }
    });
    });
    }

    const regex = /^https:\/\/twitter.com\/home$/;

    const observer = new MutationObserver(function (mutationList, observer) {
    if (!regex.test(window.location.href)) {
    return;
    }

    watchTweetNodes(mutationList);
    });

    observer.observe(document.documentElement, {
    childList: true,
    subtree: true,
    });
    })();