YouTube Improver

Remove ads and improves some stuff

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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");