Youtube: Remove Overlays

10/30/2021, 12:35:27 AM

目前為 2021-10-30 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Youtube: Remove Overlays
// @namespace   https://greasyfork.org/en/users/221281-klaufir
// @match       https://www.youtube.com/embed/*
// @match       https://www.youtube.com/watch*
// @grant       none
// @version     1.7
// @author      -
// @description 10/30/2021, 12:35:27 AM
// ==/UserScript==

function idle_waiter(wait_for_idle_ms, on_idle_callback) {
  // source: https://stackoverflow.com/a/47406751
  var action = function(o) {
      o.disconnect();
      on_idle_callback();
  }
    
  var resetTimer = function(changes, observer) {
      clearTimeout(timer);
      timer = setTimeout(action, wait_for_idle_ms, observer);
  }
  
  var observer = new MutationObserver(resetTimer);
  var timer = setTimeout(action, wait_for_idle_ms, observer); // wait for the page to stay still for 3 seconds
  observer.observe(document, {childList: true, subtree: true});
}

function removeAllByClass(classes) {
  classes.forEach(cls => {
    Array.from(document.getElementsByClassName(cls)).map(e => e.remove());
  });
}

function cleanupOverlays() {
  const classes = [
        'ytp-paid-content-overlay',  // paid promotion notification overlay in the bottom left corner
        'ytp-pause-overlay',         // "More Videos" overlay on paused embeds
        'ytp-ce-element',            // covering overlays at the end of the video
        'iv-branding',               // branding overlay in the bottom right corner
        'ytp-cards-teaser',          // info cards in the top right corner
        'ytp-cards-button-icon',     // info cards in the top right corner
        'ytp-cards-button-title',    // info cards in the top right corner
        'ytp-endscreen-content'      // endscreen recommended videos
      ];
  removeAllByClass(classes);
}

// overkill but works
idle_waiter(100, cleanupOverlays);
idle_waiter(500, cleanupOverlays);
idle_waiter(1000, cleanupOverlays);
setTimeout(cleanupOverlays, 2000);