YouTube Subscriptions Tab Filter - Advanced (with Members Only)

Advanced filter for YouTube Subscriptions. Tabs for Uploads, Live, Upcoming, Shorts, Members Only, and All. Includes toggles, persistence, and auto-refreshing on scroll.

// ==UserScript==
// @name         YouTube Subscriptions Tab Filter - Advanced (with Members Only)
// @namespace    http://tampermonkey.net/
// @version      2.1
// @description  Advanced filter for YouTube Subscriptions. Tabs for Uploads, Live, Upcoming, Shorts, Members Only, and All. Includes toggles, persistence, and auto-refreshing on scroll.
// @author       ChatGPT
// @license      MIT
// @match        https://www.youtube.com/feed/subscriptions
// @grant        none
// ==/UserScript==

/*
MIT License

Copyright (c) 2025 ChatGPT + Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

(function () {
    'use strict';

    const ALL_TABS = ['Uploads', 'Live', 'Upcoming', 'Shorts', 'Members Only', 'All'];
    let enabledTabs = JSON.parse(localStorage.getItem('yt_sub_filter_tabs_enabled') || JSON.stringify(ALL_TABS));
    let lastTab = localStorage.getItem('yt_sub_filter_last_tab') || 'Uploads';

    const waitForGrid = setInterval(() => {
        const grid = document.querySelector('ytd-rich-grid-renderer');
        if (grid && document.querySelectorAll('ytd-rich-item-renderer').length > 0) {
            clearInterval(waitForGrid);
            createConfigBar();
            createTabBar();
            filterVideos(lastTab);
            observeGridChanges();
        }
    }, 500);

    function createTabBar() {
        const container = document.querySelector('ytd-rich-grid-renderer');

        const tabBar = document.createElement('div');
        tabBar.id = 'yt-tab-filter-bar';
        tabBar.style.display = 'flex';
        tabBar.style.gap = '10px';
        tabBar.style.margin = '10px 10px 0 10px';
        tabBar.style.flexWrap = 'wrap';

        enabledTabs.forEach(tabName => {
            const btn = document.createElement('button');
            btn.textContent = tabName;
            btn.style.padding = '5px 10px';
            btn.style.cursor = 'pointer';
            btn.style.border = '1px solid #aaa';
            btn.style.background = '#eee';
            btn.style.borderRadius = '4px';
            btn.style.fontWeight = tabName === lastTab ? 'bold' : 'normal';

            btn.onclick = () => {
                document.querySelectorAll('#yt-tab-filter-bar button').forEach(b => b.style.fontWeight = 'normal');
                btn.style.fontWeight = 'bold';
                lastTab = tabName;
                localStorage.setItem('yt_sub_filter_last_tab', tabName);
                filterVideos(tabName);
            };

            tabBar.appendChild(btn);
        });

        container.parentElement.insertBefore(tabBar, container);
    }

    function createConfigBar() {
        const container = document.querySelector('ytd-rich-grid-renderer');
        const configBar = document.createElement('div');
        configBar.style.margin = '10px';
        configBar.style.display = 'flex';
        configBar.style.flexWrap = 'wrap';
        configBar.style.gap = '8px';

        const label = document.createElement('span');
        label.textContent = 'Enable Tabs: ';
        configBar.appendChild(label);

        ALL_TABS.forEach(tab => {
            const toggle = document.createElement('input');
            toggle.type = 'checkbox';
            toggle.checked = enabledTabs.includes(tab);
            toggle.id = `toggle-${tab}`;
            toggle.onchange = () => {
                if (toggle.checked) {
                    if (!enabledTabs.includes(tab)) enabledTabs.push(tab);
                } else {
                    enabledTabs = enabledTabs.filter(t => t !== tab);
                    if (lastTab === tab) lastTab = 'Uploads';
                }
                localStorage.setItem('yt_sub_filter_tabs_enabled', JSON.stringify(enabledTabs));
                localStorage.setItem('yt_sub_filter_last_tab', lastTab);
                document.querySelector('#yt-tab-filter-bar')?.remove();
                createTabBar();
                filterVideos(lastTab);
            };

            const lbl = document.createElement('label');
            lbl.textContent = tab;
            lbl.style.marginRight = '10px';
            lbl.style.userSelect = 'none';
            lbl.htmlFor = toggle.id;

            configBar.appendChild(toggle);
            configBar.appendChild(lbl);
        });

        container.parentElement.insertBefore(configBar, container);
    }

    function filterVideos(filter) {
        const items = document.querySelectorAll('ytd-rich-item-renderer');

        items.forEach(item => {
            const text = item.textContent.toLowerCase();
            const hasNotify = item.querySelector('button[aria-label="Notify me"]');
            const isLive = text.includes('watching') || text.includes('live now');
            const isUpcoming = !!hasNotify;
            const isShort = text.includes('#shorts') || isLikelyShort(item);
            const isMembers = text.includes('members only');

            let show = false;

            if (filter === 'All') {
                show = true;
            } else if (filter === 'Uploads') {
                show = !isLive && !isUpcoming && !isShort && !isMembers;
            } else if (filter === 'Live') {
                show = isLive && !isUpcoming && !isShort && !isMembers;
            } else if (filter === 'Upcoming') {
                show = isUpcoming && !isLive && !isShort && !isMembers;
            } else if (filter === 'Shorts') {
                show = isShort;
            } else if (filter === 'Members Only') {
                show = isMembers;
            }

            item.style.display = show ? '' : 'none';
        });
    }

    function isLikelyShort(item) {
        const durationLabel = item.querySelector('span.ytd-thumbnail-overlay-time-status-renderer');
        if (durationLabel) {
            const duration = durationLabel.textContent.trim();
            const parts = duration.split(':').map(Number);
            let seconds = 0;
            if (parts.length === 2) seconds = parts[0] * 60 + parts[1];
            else if (parts.length === 3) seconds = parts[0] * 3600 + parts[1] * 60 + parts[2];
            return seconds > 0 && seconds < 60;
        }
        return false;
    }

    function observeGridChanges() {
        const grid = document.querySelector('ytd-rich-grid-renderer #contents');
        if (!grid) return;

        const observer = new MutationObserver(() => {
            filterVideos(lastTab);
        });

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