YouTube Improver

Remove ads and improves some stuff

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Improver
// @namespace    -
// @version      2.1
// @description  Remove ads and improves some stuff
// @author       Carolina Reaper
// @match        *://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @run-at       document-start
// @connect      googlevideo.com
// @grant        none
// ==/UserScript==
// Start Injection
let inject1 = Date.now();
console.log("Injecting Script, YouTube Improver.");

// Prevent video loading from a timestamp
if (location.href.includes("&t=")) {
    location.href = location.href.split("&t=")[0];
};

// Set classes to remove
let classes = ["ytd-banner-promo-renderer-background", "style-scope ytd-banner-promo-renderer"];

// Create a new interval
setInterval(function() {
    // Increase transparancy on captions
    let captions = document.getElementsByClassName("caption-window");
    for (let e = 0; e < captions.length; e++) captions[e].style.backgroundColor = "rgba(0, 0, 0, 0)";

    // Remove classes
    classes.forEach(function(currentClass) {
        let elements = document.getElementsByClassName(currentClass);
        for (let e = 0; e < elements.length; e++) elements[e].remove();
    });

    // Remove on main page
    let ads = document.getElementsByTagName("ytd-ad-slot-renderer");
    for (let e = 0; e < ads.length; e++) ads[e].remove();
});

// Skip ads on videos
function closeAd() {
    let css = '.video-ads, .video-ads .ad-container .adDisplay,#player-ads, .ytp-ad-module, .ytp-ad-image-overlay { display: none!important; }';
    let head = document.head || document.getElementsByTagName('head')[0];
    let style = document.createElement('style');

    style.type = 'text/css';
    if (style.styleSheet) style.styleSheet.cssText = css;
    else style.appendChild(document.createTextNode(css));

    head.appendChild(style);
};

let skipInterval, skipAd = function() {
    let skipButton = document.querySelector(".ytp-ad-skip-button.ytp-button") || document.querySelector(".videoAdUiSkipButton ");
    if (skipButton) {
        skipButton = document.querySelector(".ytp-ad-skip-button.ytp-button") || document.querySelector(".videoAdUiSkipButton ");
        skipButton.click();
        if (skipInterval) clearTimeout(skipInterval);
        skipInterval = setTimeout(skipAd, 5e2);
    } else {
        if (skipInterval) clearTimeout(skipInterval);
        skipInterval = setTimeout(skipAd, 5e2);
    };
};

closeAd();
skipAd();

// Complete Injection
let injectionTime = Date.now() - inject1;
console.log("Injection Complete, YouTube Improver.\nInjection Time: " + injectionTime + "ms");