YouTube Tab Title Improver Extreme Deluxe

Adds channel names as prefixes to YouTube video titles and formats playlist titles better too. Adds video length to the end. This makes it easier to understand what each YouTube Sidebery tab is about.

目前為 2024-12-29 提交的版本,檢視 最新版本

// ==UserScript==
// @name         YouTube Tab Title Improver Extreme Deluxe
// @namespace    http://tampermonkey.net/
// @version      1.3
// @license      MIT
// @description  Adds channel names as prefixes to YouTube video titles and formats playlist titles better too. Adds video length to the end. This makes it easier to understand what each YouTube Sidebery tab is about.
// @author       NewsGuyTor
// @match        https://www.youtube.com/*
// @grant        GM_registerMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
    'use strict';

    // Load the channel name map from settings or use default values
    const defaultChannelNameMap = {
        'Digital Foundry': 'DF',
        'Kurzgesagt – In a Nutshell': 'Kurzgesagt'
    };

    const channelNameMap = GM_getValue('channelNameMap', defaultChannelNameMap);

    function saveChannelNameMap(newMap) {
        GM_setValue('channelNameMap', newMap);
    }

    function editChannelNameMap() {
        const keys = Object.keys(channelNameMap);
        const values = Object.values(channelNameMap);

        let menu = 'Edit Channel Name Substitutions:\n';
        keys.forEach((key, index) => {
            menu += `${index + 1}. ${key} -> ${values[index]}\n`;
        });
        menu += '\nEnter the number of the channel to edit, or type "add" to add a new substitution, or "delete" to remove one:';

        const userChoice = prompt(menu);

        if (userChoice === "add") {
            const newKey = prompt("Enter the full channel name:");
            const newValue = prompt("Enter the short name or abbreviation:");

            if (newKey && newValue) {
                channelNameMap[newKey] = newValue;
                saveChannelNameMap(channelNameMap);
                alert("Channel added successfully!");
            } else {
                alert("Invalid input. Please try again.");
            }
        } else if (userChoice === "delete") {
            const deleteKey = prompt("Enter the full channel name to delete:");

            if (deleteKey && channelNameMap[deleteKey]) {
                delete channelNameMap[deleteKey];
                saveChannelNameMap(channelNameMap);
                alert("Channel removed successfully!");
            } else {
                alert("Channel not found. Please try again.");
            }
        } else if (!isNaN(userChoice) && userChoice > 0 && userChoice <= keys.length) {
            const index = parseInt(userChoice) - 1;
            const selectedKey = keys[index];
            const newValue = prompt(`Enter a new short name for "${selectedKey}" (currently "${values[index]}"):`);

            if (newValue) {
                channelNameMap[selectedKey] = newValue;
                saveChannelNameMap(channelNameMap);
                alert("Channel updated successfully!");
            } else {
                alert("Invalid input. Please try again.");
            }
        } else {
            alert("Invalid choice. Please try again.");
        }
    }

    GM_registerMenuCommand('Edit Channel Name Substitutions', editChannelNameMap);

    function modifyVideoTitle() {
        const videoTitleElement = document.querySelector('h1.style-scope.ytd-watch-metadata yt-formatted-string');
        const channelNameElement = document.querySelector('#text-container > yt-formatted-string');
        const videoLengthElement = document.querySelector('.ytp-time-duration');

        if (videoTitleElement && channelNameElement && videoLengthElement) {
            const videoTitle = videoTitleElement.textContent.trim();
            const originalChannelName = channelNameElement.textContent.trim();
            const shortChannelName = channelNameMap[originalChannelName] || originalChannelName;
            const videoLength = videoLengthElement.textContent.trim();

            // Avoid duplication if the video title already starts with the channel name
            const newTitle = videoTitle.startsWith(shortChannelName)
                ? `${videoTitle} (${videoLength})`
                : `${shortChannelName} - ${videoTitle} (${videoLength})`;

            // Remove any notification count prefix like "(5) " from the title
            const currentDocumentTitle = document.title.replace(/^\(\d+\)\s*/, '');

            if (currentDocumentTitle !== newTitle) {
                document.title = newTitle;
            }
        }
    }

    function modifyPlaylistTitle() {
        const playlistTitleElement = document.querySelector('yt-dynamic-text-view-model h1.dynamic-text-view-model-wiz__h1');
        const channelNameElement = document.querySelector('.yt-avatar-stack-view-model-wiz__avatar-stack-text a');

        if (playlistTitleElement && channelNameElement) {
            const playlistTitle = playlistTitleElement.textContent.trim();

            // Extract the channel name while removing prefixes like "av" or "by"
            let channelName = channelNameElement.textContent.trim();
            channelName = channelName.replace(/^[^\w\s]*\s*/, '').trim();

            // Format the title as "PlaylistName - ChannelName"
            const newTitle = `${playlistTitle} - ${channelName}`;

            // Remove any notification count prefix like "(5) " from the title
            const currentDocumentTitle = document.title.replace(/^\(\d+\)\s*/, '');

            if (currentDocumentTitle !== newTitle) {
                document.title = newTitle;
            }
        }
    }

    function detectAndModifyTitle() {
        const url = window.location.href;

        if (url.includes('playlist')) {
            modifyPlaylistTitle();
        } else if (url.includes('watch')) {
            modifyVideoTitle();
        }
    }

    // Observe title and channel changes dynamically
    const observer = new MutationObserver(() => {
        detectAndModifyTitle();
    });

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

    // Initial call
    detectAndModifyTitle();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址