YouTube Channel Name Prefixer for Tab Titles

Adds channel names as prefixes to YouTube video titles and formats playlist titles to include the playlist name followed by the channel name. 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 Channel Name Prefixer for Tab Titles
// @namespace    http://tampermonkey.net/
// @version      1.1
// @license      MIT
// @description  Adds channel names as prefixes to YouTube video titles and formats playlist titles to include the playlist name followed by the channel name. 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        none
// ==/UserScript==

(function() {
    'use strict';

    // List of channel name substitutions
    const channelNameMap = {
        'Digital Foundry': 'DF',
        'Kurzgesagt – In a Nutshell': 'Kurzgesagt'
        // Add more substitutions here
    };

    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或关注我们的公众号极客氢云获取最新地址