YouTube Avatars Fix

Fixes the display of avatars on YouTube

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Advertisement:

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

Advertisement:

// ==UserScript==
// @name         YouTube Avatars Fix
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Fixes the display of avatars on YouTube
// @author       frz
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @match        https://*.youtube.com/*
// @match        https://studio.youtube.com/*
// @match        https://yt3.googleusercontent.com/*
// @match        https://yt4.ggpht.com/*
// @match        https://yt4.googleusercontent.com/*
// @match        https://vidiq.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    function fixAvatarUrl(url) {
        if (!url) return url;

        return url.replace(/yt3\.ggpht\.com/g, 'yt4.ggpht.com');
    }


    function fixImage(img) {
        if (!img || !img.src) return;
        const originalSrc = img.src;

        if (originalSrc.includes('yt3.ggpht.com')) {
            const newSrc = fixAvatarUrl(originalSrc);
            if (newSrc !== originalSrc) {
                img.src = newSrc;

            }
        }
    }


    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {

            if (mutation.addedNodes.length) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE) {

                        if (node.tagName === 'IMG') {
                            fixImage(node);
                        }

                        if (node.querySelectorAll) {
                            node.querySelectorAll('img[src*="yt3.ggpht.com"]').forEach(fixImage);
                        }
                    }
                });
            }

            if (mutation.type === 'attributes' && mutation.attributeName === 'src') {
                const target = mutation.target;
                if (target.tagName === 'IMG') {
                    fixImage(target);
                }
            }
        });
    });


    observer.observe(document.documentElement, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['src']
    });


    document.querySelectorAll('img[src*="yt3.ggpht.com"]').forEach(fixImage);
})();