Plex Dynamic Title

Change the title of Plex pages to reflect the current content being viewed

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Plex Dynamic Title
// @namespace    http://tampermonkey.net/
// @version      1.9
// @description  Change the title of Plex pages to reflect the current content being viewed
// @author       azizLIGHT
// @match        https://app.plex.tv/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function updateTitle() {
        const url = window.location.href;
        let newTitle = 'Plex';

        // Determine the title based on the current URL
        if (url.includes('/desktop/#!/')) {
            if (url === 'https://app.plex.tv/desktop/#!/') {
                newTitle = 'Plex: Home';
            } else if (url.includes('source=watchlist')) {
                newTitle = 'Plex: Watchlist';
            } else if (url.includes('source=home')) {
                newTitle = 'Plex: Discover';
            } else if (url.includes('/search?')) {
                const searchQueryMatch = url.match(/query=([^&]+)/);
                if (searchQueryMatch) {
                    const searchQuery = decodeURIComponent(searchQueryMatch[1]);
                    newTitle = `Plex: Top Results for "${searchQuery}"`;
                }
            } else if (url.includes('/settings/')) {
                // Handle settings pages
                const settingsHeader = document.querySelector('h2.SettingsPageHeader-header-Ra2WXw');
                if (settingsHeader) {
                    newTitle = `Plex: ${settingsHeader.innerText}`;
                }
            } else if (url.includes('/media/')) {
                // Handle library pages
                const libraryTitleElement = document.querySelector('div.PageHeaderTitle-title-W0yjas');
                if (libraryTitleElement) {
                    const libraryName = libraryTitleElement.innerText;
                    newTitle = `Plex: ${libraryName}`;
                }
            }

            // Check for active tab titles on non-library pages
            const selectedTab = document.querySelector('.TabButton-selected-wnNjlr');
            if (selectedTab) {
                const tabTitle = selectedTab.innerText;
                if (url.includes('source=home')) {
                    newTitle = `Plex: Home - ${tabTitle}`;
                } else if (url.includes('source=watchlist')) {
                    newTitle = `Plex: Watchlist - ${tabTitle}`;
                } else {
                    newTitle = `Plex: ${tabTitle}`;
                }
            }
        }

        // Check for movie/show titles
        const titleElement = document.querySelector('h1[data-testid="metadata-title"]');
        const libraryTitleElement = document.querySelector('div.PageHeaderTitle-title-W0yjas');

        if (titleElement && libraryTitleElement) {
            const movieName = titleElement.innerText;
            const libraryName = libraryTitleElement.innerText;

            // Check for subtitle
            const subtitleElement = document.querySelector('h2[data-testid="metadata-subtitle"]');
            if (subtitleElement) {
                const subtitleText = subtitleElement.innerText;

                // Check if the subtitle contains "Directed by"
                if (subtitleText.startsWith("Directed by")) {
                    // It's a movie, so exclude the director info
                    newTitle = `Plex: ${libraryName} - ${movieName}`;
                } else {
                    // It's likely a TV show with season info
                    newTitle = `Plex: ${libraryName} - ${movieName}: ${subtitleText}`;
                }
            } else {
                // No subtitle, just set the title
                newTitle = `Plex: ${libraryName} - ${movieName}`;
            }
        } else if (libraryTitleElement) {
            const libraryName = libraryTitleElement.innerText;
            if (url.includes('pivot=recommended')) {
                newTitle = `Plex: ${libraryName} - Recommended`;
            } else if (url.includes('pivot=library')) {
                newTitle = `Plex: ${libraryName} - Library`;
            } else if (url.includes('pivot=collections')) {
                newTitle = `Plex: ${libraryName} - Collections`;
            } else if (url.includes('pivot=playlists')) {
                newTitle = `Plex: ${libraryName} - Playlists`;
            } else if (url.includes('pivot=categories')) {
                newTitle = `Plex: ${libraryName} - Categories`;
            } else {
                newTitle = `Plex: ${libraryName}`;
            }
        }

        document.title = newTitle;
    }

    // Run the function on page load
    updateTitle();

    // Observe changes in the DOM to update the title dynamically
    const observer = new MutationObserver(updateTitle);
    observer.observe(document.body, { childList: true, subtree: true });
})();