☰

YouTube Avatars Fix

Fixes the display of avatars on YouTube

スクリプトをインストールするには、Tampermonkey, Greasemonkey や Violentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、Tampermonkey や Violentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、Tampermonkey や Userscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら、通報はこちらへお寄せください
// ==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);
})();