Twitter Cleaner

This script keeps Twitter clean.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        Twitter Cleaner
// @namespace   https://github.com/mosaicer
// @author      mosaicer
// @description This script keeps Twitter clean.
// @version     1.0.11
// @match       https://twitter.com/*
// @run-at      document-idle
// @license     MIT
// ==/UserScript==
(() => {
  'use strict';

  const STRING_RESOURCES = {
    ja: {
      timelineList: 'タイムライン: リスト'
    },
    en: {
      timelineList: 'Timeline: List'
    }
  };
  const STRINGS = STRING_RESOURCES[navigator.language === 'ja' ? 'ja' : 'en'];

  const muteIfNeed = function tryToHideTweetNode(tweetNode) {
    if (isPromotion(tweetNode)) {
      tweetNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display = 'none';
    }
  };

  const extractTweets = function extractTweetNoodesFrom(node) {
    return node.querySelectorAll('[data-testid="tweet"]');
  };

  const isPromotion = function checkIfTweetNodeIsPromotion(tweetNode) {
    return tweetNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
      .getAttribute('data-testid') === 'placementTracking';
  };

  const isElement = function checkIfValueIsInstanceOfElement(value) {
    return value instanceof Element;
  };

  const hideListHeader =
    function tryToHideListHeaderAndDoNothingIfNotExists(node) {
      try {
        const target = node.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;

        if (target.getAttribute('aria-label') === STRINGS.timelineList) {
          node.parentNode.parentNode.parentNode.style.display = 'none';
        }
      } catch(e) {}
    };

  const rootNode = document.getElementById('react-root');
  new MutationObserver(mutations =>
    mutations.forEach(mutation =>
      mutation.addedNodes.forEach(node => {
        if (!isElement(node)) return;

        const tweets = extractTweets(node);
        tweets.forEach(n => muteIfNeed(n));

        hideListHeader(node);
      })
    )
  ).observe(rootNode, { childList: true, subtree: true });
})();