Custom Image for Youtube Background

Replace the old boring flat background with whatever picture you like

目前為 2024-06-16 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Custom Image for Youtube Background
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Replace the old boring flat background with whatever picture you like
// @author       Hoover
// @match        *://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';
    // Fill the array up with URLs to pictures
    const bgSources = [
        "https://i.kym-cdn.com/photos/images/original/000/581/296/c09.jpg",
        "https://i.kym-cdn.com/photos/images/original/000/581/296/c09.jpg"
    ]

    function getRandomImageUrl(images) {
        const randomIndex = Math.floor(Math.random() * images.length);
        return images[randomIndex];
    }

    // Select only one image from the array
    let bgImage = getRandomImageUrl(bgSources);

    function applyBackground() {
        var primaryElement = document.getElementById('content');
        if (primaryElement) {
            primaryElement.style.backgroundImage = `url(${bgImage})`;
            primaryElement.style.backgroundAttachment = 'fixed';
            primaryElement.style.backgroundSize = 'cover';
            primaryElement.style.backgroundRepeat = 'no-repeat';
            primaryElement.style.backgroundPosition = 'center';
        }
    }
    
    // Function to add transparency to some divs
    function applyTransparency(divName) {
        var divElem = document.getElementById(divName);
        if (divElem) {
            divElem.style.opacity = 0.9;
        }
    }

    // Repackaged for quick calling
    function applyTransparentElems() {
        applyTransparency('guide');
        applyTransparency('chips-wrapper');
    }

    // Function to remove elements by class name
    function removeElementsByID(id) {
        var element = document.getElementById(id);
        if (element) {
            element.parentNode.removeChild(element);
        }
    }


    // Must remove cinematic container (the glowing effect when video plays)
    removeElementsByID('cinematics-container');

    applyTransparentElems();

    // Check for the element every 500ms until it is found
    var checkExist = setInterval(function () {
        if (document.getElementById('content')) {
            applyBackground();
            clearInterval(checkExist);
            removeElementsByID('cinematics-container');
        }
    }, 500);

    // Additionally, listen for page changes and reapply the background
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            if (mutation.addedNodes.length) {
                applyBackground();
                applyTransparentElems()
                removeElementsByID('cinematics-container');
            }
        });
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();