YT Feed Sorter

Sorts the YouTube feed so that all scheduled streams come before archived videos.

目前為 2022-11-01 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YT Feed Sorter
// @namespace    YTFeedSorter
// @version      0.4
// @description  Sorts the YouTube feed so that all scheduled streams come before archived videos.
// @match        *://*.youtube.com/*
// @author       KFP
// ==/UserScript==

(function() {
    'use strict';

    let feedSorted = false;
    const feedSelector = 'ytd-browse[page-subtype="subscriptions"][role="main"] #contents.ytd-section-list-renderer';
    const blockSelector = '#items.ytd-grid-renderer';
    const liveSelector = '.badge-style-type-live-now-alternate';
    const soonSelector = 'ytd-toggle-button-renderer';
    const sortedClass = 'ytfeedsorter-sorted';

    const sortBlock = block => {
        const wasSorted = block.classList.contains(sortedClass);
        if (!wasSorted) block.classList.add(sortedClass);
        setTimeout(() => {
            [...block.children].sort((a, b) => {
                const ai = a.querySelector(liveSelector) ? 2 : a.querySelector(soonSelector) ? 1 : 0;
                const bi = b.querySelector(liveSelector) ? 2 : b.querySelector(soonSelector) ? 1 : 0;
                return (ai > bi) ? -1 : (ai < bi) ? 1 : 0;
            }).forEach(item => block.appendChild(item));
        }, wasSorted ? 200 : 0);
    };

    const observer = new MutationObserver(mutations => {
        for (const mut of mutations) {
            for (const added of mut.addedNodes) {
                const block = added.querySelector(blockSelector);
                if (block) sortBlock(block);
            }
        }
    });

    setInterval(() => {
        const feed = document.querySelector(feedSelector);
        if (feed) {
            if (feedSorted) return;
            feed.querySelectorAll(blockSelector).forEach(sortBlock);
            observer.observe(feed, {childList: true});
            feedSorted = true;
        } else if (feedSorted) {
            observer.disconnect();
            feedSorted = false;
        }
    }, 100);
})();