Greasy Fork

Greasy Fork is available in English.

Audio Tab Title

Change the window title on YouTube based on the channel name and video title, and append "- Spotify" to the title on Spotify pages.

目前为 2025-02-02 提交的版本。查看 最新版本

// ==UserScript==
// @name         Audio Tab Title
// @description  Change the window title on YouTube based on the channel name and video title, and append "- Spotify" to the title on Spotify pages.
// @version      0.3.0
// @namespace    itsafeature.org
// @author       Geoffrey De Belie (Smile4ever)
// @license      Unlicense
// @match        https://www.youtube.com/watch?v=*
// @match        https://music.youtube.com/*
// @match        https://open.spotify.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to update the title on YouTube
    function updateYouTubeTitle() {
        const channelElement = document.querySelector('a.yt-simple-endpoint.style-scope.yt-formatted-string');
        const videoTitleElement = document.querySelector('#above-the-fold #title');

        if (channelElement && videoTitleElement) {
            const channelNameClean = channelElement.innerText.split(' - ')[0].split('|')[0].replace(" Official", "");
            console.log("channelNameClean: " + channelNameClean);
            const videoTitle = videoTitleElement.innerText;
            console.log("videoTitle: " + videoTitle);

            if(videoTitle.includes("- ")) return;

            // Set the window title for YouTube
            document.title = `${channelNameClean} - ${videoTitle} - YouTube`;
        }
    }

    // Function to update the title on YouTube Music
    function updateYouTubeMusicTitle() {
        const elements = document.querySelectorAll('.ytmusic-player-bar yt-formatted-string');
        if (elements.length < 2) return;

        const artistElement = elements[1];
        const audioTitleElement = elements[0];

        if (artistElement && audioTitleElement) {
            const artistName = artistElement.childNodes[0].innerText;
            const audioTitle = audioTitleElement.innerText;

            // Set the window title for YouTube Music
            document.title = `${artistName} - ${audioTitle} - YouTube Music`;
        }
    }

    // Function to update the title on Spotify
    function updateSpotifyTitle() {
        const titleElement = document.querySelector('title'); // Get the <title> element
        if (titleElement.innerText.includes(" - Spotify") == false) {
            titleElement.innerText = `${titleElement.innerText} - Spotify`; // Set the title's innerText for Spotify
        }
    }

    // Function to check for the current platform (YouTube or Spotify)
    function updateTitle() {
        if (window.location.href.startsWith('https://www.youtube.com')) {
            updateYouTubeTitle();
         } else if (window.location.hostname == 'music.youtube.com'){
            updateYouTubeMusicTitle();
        } else if (window.location.hostname.includes('spotify.com')) {
            updateSpotifyTitle();
        }
    }

    // Optionally, observe changes in the page if the title might update after load
    const observer = new MutationObserver(updateTitle);
    observer.observe(document.body, { childList: true, subtree: true });

    // Wait for the page content to load before running the updateTitle function
    window.addEventListener('load', updateTitle);

    // Set an interval to check the title every second on Spotify pages only
    setInterval(() => {
        updateTitle(); // Update the title every X seconds (if needed)
    }, 2000);
})();