TikTok - Remove distractions

Remove distractions on TikTok

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension 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.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        TikTok - Remove distractions
// @description Remove distractions on TikTok
// @version  1.2
// @grant    none
// @include	*://tiktok.com/*
// @include	*://*.tiktok.com/*
// @license     GPL v3
// @author      @incognico
// @namespace   https://greasyfork.org/users/931787
// ==/UserScript==

const selectors = [
  '[data-e2e="nav-live"]', // Live tab
  '[data-e2e="nav-foryou"]', // For you tab
  '[data-e2e="nav-explore"]', // Explore tab
  '[class*="-DivDiscoverContainer"]', // Discover section
  '[class*="-DivUserContainer"]:has([data-e2e="suggest-accounts"])', // Suggested accounts in sidebar (needs about:config layout.css.has-selector.enabled for now)
  '[data-e2e="recharge-entrance"]', // Get coins in profile menu
  '[data-e2e="live-studio-entrance"]', // Live Studio in profile menu
  '[class*="-StyledShareIcon"]', // Share icon on profile page
  '[data-e2e="upload-icon"]', // Upload button
];

const hide = (selector) => {
  const node = document.querySelector(selector);
  if (node) {
    node.style.display = 'none';
  }
};

const redirectClickEvent = (event) => {
  event.preventDefault();
  event.stopImmediatePropagation();
  const followingButton = document.querySelector('[data-e2e="nav-following"]');
  if (followingButton) {
    followingButton.click();
  } else {
    window.location.pathname = '/following';
  }
};

const listeners = [];
const redirect = () => {
  // replace home links click to following button
  const links = document.querySelectorAll('[href="/"]');
  links.forEach(link => {
    if (!listeners.includes(link)) {
      listeners.push(link);
      link.addEventListener('click', redirectClickEvent, true);
    }
  });
}

window.setTimeout(
  function check() {
    selectors.forEach(hide);
    redirect();
    window.setTimeout(check, 250);
  }, 250
);

// redirect from home live or home with country code
const { pathname } = window.location;
if (pathname === '/' || pathname === '/live' || pathname === '/explore' || pathname.match(/\/[a-z]{2}$/)) {
  window.location.replace('/following');
}