Prevent Autoplay on YouTube Channels

This script prevents autoplayed videos on YouTube channel profile pages.

目前為 2025-07-31 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Prevent Autoplay on YouTube Channels
// @description  This script prevents autoplayed videos on YouTube channel profile pages.
// @namespace    http://tampermonkey.net/
// @icon         https://cdn-icons-png.flaticon.com/64/2504/2504965.png
// @version      0.0.4
// @author       rxm
// @match        https://www.youtube.com/@*/featured
// @match        https://www.youtube.com/@*
// @match        https://www.youtube.com/*
// @exclude      https://www.youtube.com/watch?v=*
// @license      MIT
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_notification
// @grant        GM_info
// @grant        window.onurlchange
// ==/UserScript==

// Custom urlchange event (used to monitor URL changes)
function addUrlChangeEvent() {
    history.pushState = (f => function pushState() {
        const ret = f.apply(this, arguments);
        window.dispatchEvent(new Event('pushstate'));
        window.dispatchEvent(new Event('urlchange'));
        return ret;
    })(history.pushState);

    history.replaceState = (f => function replaceState() {
        const ret = f.apply(this, arguments);
        window.dispatchEvent(new Event('replacestate'));
        window.dispatchEvent(new Event('urlchange'));
        return ret;
    })(history.replaceState);

    window.addEventListener('popstate', () => {
        window.dispatchEvent(new Event('urlchange'));
    });
}

// === Configurable Menu ===
const menu_ALL = [
    ['menu_isEnableHighlightChannelSubscriberNumber', 'Highlight Channel Subscriber Count', 'Highlight Channel Subscriber Count', false]
];
const menu_ID = [];

// Initialize menu values
for (let i = 0; i < menu_ALL.length; i++) {
    if (GM_getValue(menu_ALL[i][0]) == null) {
        GM_setValue(menu_ALL[i][0], menu_ALL[i][3]);
    }
}

// Register script menu
function registerMenuCommand() {
    if (menu_ID.length >= menu_ALL.length) {
        for (let i = 0; i < menu_ID.length; i++) {
            GM_unregisterMenuCommand(menu_ID[i]);
        }
    }

    for (let i = 0; i < menu_ALL.length; i++) {
        menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
        menu_ID[i] = GM_registerMenuCommand(
            `${menu_ALL[i][3] ? '✅' : '❌'} ${menu_ALL[i][1]}`,
            function () {
                menu_switch(menu_ALL[i][3], menu_ALL[i][0], menu_ALL[i][2]);
            }
        );
    }
}

// Menu toggle
function menu_switch(menu_status, Name, Tips) {
    const isEnabled = (menu_status === 'true');
    GM_setValue(Name, !isEnabled);

    GM_notification({
        text: `${isEnabled ? 'Disabled' : 'Enabled'} [${Tips}] feature\n(Click to refresh for changes)`,
        timeout: 3500,
        onclick: () => location.reload()
    });

    registerMenuCommand();
}

registerMenuCommand();
if (window.onurlchange === undefined) {
    addUrlChangeEvent();
}

// === Main Logic ===
(function () {
    'use strict';

    function isChannelPage() {
        return location.pathname.startsWith('/@') || location.pathname.startsWith('/channel/');
    }

    function pauseVideo() {
        if (!isChannelPage()) return;

        const video = document.querySelector('video');
        if (video && !video.paused) {
            video.pause();
        }
    }

    function highlightElement() {
        if (!GM_getValue('menu_isEnableHighlightChannelSubscriberNumber', false)) return;

        const element = document.querySelector('.page-header-view-model-wiz__page-header-content-metadata');
        if (element) {
            element.style.backgroundColor = 'yellow';

            Array.from(element.children).forEach(child => {
                child.style.fontWeight = 'bold';
                child.style.color = 'red';
                child.style.textDecoration = 'none';
            });

            const spans = element.querySelectorAll('span');
            spans.forEach(span => {
                span.style.fontWeight = 'bold';
                span.style.color = 'red';
                span.style.textDecoration = 'none';
            });
        }
    }

    // MutationObserver to monitor changes
    const observer = new MutationObserver(() => {
        pauseVideo();
        highlightElement();
    });

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

    // Initial run
    pauseVideo();
    highlightElement();

    // Fallback interval for early loads
    let executionCount = 0;
    const intervalId = setInterval(() => {
        highlightElement();
        executionCount++;
        if (executionCount >= 2) {
            clearInterval(intervalId);
        }
    }, 1000);
})();