Advanced YouTube Ad Blocker

Block YouTube ads and enhance your viewing experience

目前為 2025-02-14 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Advanced YouTube Ad Blocker
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Block YouTube ads and enhance your viewing experience
// @author       Patrick
// @match        https://*.youtube.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // Function to remove ads from the DOM
    function removeAds() {
        // Remove overlay ads
        const overlayAds = document.querySelectorAll('.ytp-ad-overlay');
        overlayAds.forEach(ad => ad.remove());

        // Remove pre-roll and mid-roll ads
        const videoAds = document.querySelectorAll('.videoAdUi');
        videoAds.forEach(ad => ad.remove());

        // Remove banner ads
        const bannerAds = document.querySelectorAll('.ytp-ad-text');
        bannerAds.forEach(ad => ad.remove());
    }

    // Function to skip ads automatically
    function skipAds() {
        const skipButton = document.querySelector('.ytp-ad-skip-button');
        if (skipButton) {
            skipButton.click();
        }
    }

    // Function to mute and speed up playback during ads
    function enhancePlayback() {
        const videoPlayer = document.querySelector('video');
        if (videoPlayer) {
            videoPlayer.muted = true;
            videoPlayer.playbackRate = 2;
        }
    }

    // Function to inject custom CSS to hide elements
    function injectCSS() {
        const style = document.createElement('style');
        style.innerHTML = `
            .ytp-ad-overlay, .videoAdUi, .ytp-ad-text {
                display: none !important;
            }
        `;
        document.head.appendChild(style);
    }

    // Function to handle ad removal and playback enhancement
    function handleAdRemoval() {
        removeAds();
        skipAds();
        enhancePlayback();
    }

    // Inject custom CSS on page load
    injectCSS();

    // Set interval to periodically remove ads and enhance playback
    setInterval(handleAdRemoval, 500);

    // Override fetch to intercept ad-related requests
    const originalFetch = window.fetch;
    window.fetch = function(url, ...args) {
        if (url.includes('/youtubei/v1/player')) {
            return originalFetch(url, ...args).then(response => {
                return response.clone().json().then(data => {
                    data.adPlacements = [];
                    return new Response(JSON.stringify(data), {
                        status: response.status,
                        statusText: response.statusText,
                        headers: response.headers
                    });
                });
            });
        }
        return originalFetch(url, ...args);
    };

    // Override ytInitialPlayerResponse to remove ad placements
    const originalYtInitialPlayerResponse = window.ytInitialPlayerResponse;
    window.ytInitialPlayerResponse = {
        ...originalYtInitialPlayerResponse,
        adPlacements: []
    };

    // Override Object.defineProperty to ensure ad placements are removed
    const originalDefineProperty = Object.defineProperty;
    Object.defineProperty = function(obj, prop, descriptor) {
        if (prop === 'ytInitialPlayerResponse') {
            descriptor.get = function() {
                return { ...originalYtInitialPlayerResponse, adPlacements: [] };
            };
        }
        return originalDefineProperty(obj, prop, descriptor);
    };
})();