YouTube Awesome

Get the best YouTube experience

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Awesome
// @namespace    https://github.com/pagyew
// @version      1.0.9
// @description  Get the best YouTube experience
// @author       Vladislav Tsepilov (https://github.com/pagyew)
// @license      MIT
// @homepageURL  https://github.com/pagyew/youtube-awesome
// @supportURL   https://github.com/pagyew/youtube-awesome
// @match        https://www.youtube.com/*
// @match        https://m.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @run-at       document-body
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// ==/UserScript==

(function () {
  'use strict';

  const shortsSelectors = [
    // Menus
    'ytm-pivot-bar-item-renderer:has(.pivot-shorts)',
    'ytd-mini-guide-entry-renderer:has(a#endpoint[title*="shorts" i])',
    'ytd-guide-entry-renderer:has(a#endpoint[title*="shorts" i])',

    // Tabs
    'yt-chip-cloud-chip-renderer:has(yt-formatted-string[title*="shorts" i])',
    'yt-tab-shape[tab-title*="shorts" i]',

    // Sections
    'ytm-reel-shelf-renderer',
    'ytd-reel-shelf-renderer',
    'ytm-item-section-renderer:has(.big-shorts-singleton)',
    'ytd-rich-section-renderer:has(ytd-rich-shelf-renderer)',

    // Videos
    'ytd-video-renderer:has(a#thumbnail[href*="shorts" i])',

    // Ads
    'ytd-rich-item-renderer:has(ytd-ad-slot-renderer)',
  ];

  const mixSelectors = [
    // Tabs
    'yt-chip-cloud-chip-renderer:has(yt-formatted-string[title*=mixes i])',

    // Videos
    'ytd-rich-item-renderer:has(ytd-playlist-thumbnail):has(a[title^="Mix - "])',
    'ytd-rich-item-renderer:has(ytd-playlist-thumbnail):has(a[title="My Mix"])',
    'ytd-rich-item-renderer:has(yt-collections-stack):has([title^="Mix - "])',
    'ytd-rich-item-renderer:has(yt-collections-stack):has([title="My Mix"])',
    'ytd-radio-renderer:has(ytd-playlist-thumbnail):has(span[title^="Mix - "])',
    'ytd-compact-radio-renderer:has(yt-collections-stack):has(span[title^="Mix - "])',
  ];

  const watchedSelectors = [
    //Videos
    'ytd-rich-item-renderer:has(#progress[style="width: 100%;"])',
    'ytd-compact-video-renderer:has(#progress[style="width: 100%;"])',
  ];

  const options = [
    {
      id: 'shorts',
      title: 'Shorts',
      defaultValue: true,
      selectors: shortsSelectors,
    },
    {
      id: 'mix',
      title: 'Mixes',
      defaultValue: true,
      selectors: mixSelectors,
    },
    {
      id: 'watched',
      title: 'Watched',
      defaultValue: true,
      selectors: watchedSelectors,
    },
  ];

  function register(option) {
    const { id, ref, title } = option;
    const command = `--- ${ref.value ? '✅' : '❌'} ${title}`;

    GM_registerMenuCommand(
      command,
      () => {
        ref.value = !ref.value;
        setTimeout(update);
      },
      {
        id,
        autoClose: false,
      },
    );
  };

  function unregister(option) {
    GM_unregisterMenuCommand(option.id);
  };

  function useOption(option) {
    const { id, defaultValue } = option;

    if (typeof GM_getValue === 'undefined') {
      return { value: defaultValue };
    };

    const ref = {
      get value() {
        return GM_getValue(id, defaultValue);
      },
      set value(v) {
        GM_setValue(id, v);
      },
    };

    return { ...option, ref };
  };

  const style = document.createElement('style');
  document.body.appendChild(style);
  
  const usedOptions = options.map(useOption);
  GM_registerMenuCommand('Hide:', () => { }, { autoClose: false });

  function update() {
    usedOptions.forEach(unregister);
    usedOptions.forEach(register);

    const selectors = usedOptions
      .filter(({ ref }) => ref.value)
      .map(({ selectors }) => selectors)
      .flat();

    if (selectors.length > 0) {
      style.innerText = `${selectors.join(',')}{ display: none !important; }`;
    };
  };

  update();
})();