YouTube Branding Image Remover

Removes branding images (elements with class 'branding-img') from YouTube pages to provide a cleaner interface.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Branding Image Remover
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Removes branding images (elements with class 'branding-img') from YouTube pages to provide a cleaner interface.
// @author       Your Name/AI
// @match        *://*.youtube.com/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    function removeBrandingImages() {
        const images = document.querySelectorAll('img.branding-img');
        let imagesRemovedThisRun = 0;

        images.forEach(img => {
            if (document.body.contains(img)) {
                img.remove();
                imagesRemovedThisRun++;
            }
        });

        if (imagesRemovedThisRun > 0) {
            // console.log(`[YouTube Branding Image Remover] Removed ${imagesRemovedThisRun} branding image(s).`);
        }
    }

    // Initial run
    removeBrandingImages();

    // Observe DOM changes for dynamically loaded content
    const observer = new MutationObserver(function(mutationsList, observerInstance) {
        let potentiallyNewBrandingImages = false;
        for(const mutation of mutationsList) {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                for (const node of mutation.addedNodes) {
                    if (node.nodeType === 1) { 
                        if (node.matches && node.matches('img.branding-img')) {
                            potentiallyNewBrandingImages = true;
                            break;
                        }
                        if (node.querySelector && node.querySelector('img.branding-img')) {
                            potentiallyNewBrandingImages = true;
                            break;
                        }
                    }
                }
            }
            if (potentiallyNewBrandingImages) break;
        }

        if (potentiallyNewBrandingImages) {
            removeBrandingImages();
        }
    });

    function startObserver() {
        if (document.body) {
            observer.observe(document.body, { childList: true, subtree: true });
        } else {
            document.addEventListener('DOMContentLoaded', function() {
                observer.observe(document.body, { childList: true, subtree: true });
            });
        }
    }

    startObserver();

})();