Twitch Sort by Newest/Oldest

Sorts Twitch clips by newest or oldest on the 24-hour clips page

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name Twitch Sort by Newest/Oldest
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Sorts Twitch clips by newest or oldest on the 24-hour clips page
// @author MahdeenSky
// @match https://www.twitch.tv/*/clips?filter=clips&range=24hr
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @license      GNU GPLv3
// ==/UserScript==

(function() {
    'use strict';

    let sortByNewest = true;
    
    function sortClips() {
        console.log(`Sorting clips by ${sortByNewest ? 'newest' : 'oldest'}...`);
    
        const clipTowers = document.querySelectorAll('.ScTower-sc-1sjzzes-0');
        const clipTower = clipTowers[1];
    
        if (clipTower) {
            console.log("Found clip tower element:", clipTower);
        } else {
            console.error("Clip tower element not found!");
            return; // Exit if the main container is not found
        }
    
        const clipCards = Array.from(clipTower.querySelectorAll('.Layout-sc-1xcs6mc-0[data-a-target^="clips-card-"]'));
        console.log("Found", clipCards.length, "clip cards.");
    
        clipCards.sort((a, b) => {
            const elementA = a.querySelectorAll('.ScPositionCorner-sc-1shjvnv-1');
            const elementB = b.querySelectorAll('.ScPositionCorner-sc-1shjvnv-1');
            const timeA = elementA[elementA.length - 1].textContent.trim();
            const timeB = elementB[elementB.length - 1].textContent.trim();
    
            // edge case, if there is 'Yesterday' in the time, it should be considered as the oldest or newest
            if (timeA.includes('Yesterday')) {
                return sortByNewest ? -999 : 999;
            } else if (timeB.includes('Yesterday')) {
                return sortByNewest ? 999 : -999;
            }
    
            // handle hours ago and minutes ago cases
            let timeAValue = parseInt(timeA);
            let timeBValue = parseInt(timeB);
    
            if (timeA.includes('hour')) {
                timeAValue *= 60;
            }
            if (timeB.includes('hour')) {
                timeBValue *= 60;
            }
    
            return sortByNewest ? timeBValue - timeAValue : timeAValue - timeBValue;
        });
    
        clipTower.innerHTML = '';
        clipCards.forEach(card => clipTower.appendChild(card));
    
        console.log("Clips sorted successfully.");
    }
    
    // Create sort button
    const sortButton = document.createElement('button');
    sortButton.textContent = 'Sort by Newest';
    sortButton.style.backgroundColor = '#6441A5';
    sortButton.style.color = 'white';
    sortButton.style.padding = '10px 20px';
    sortButton.style.border = 'none';
    sortButton.style.cursor = 'pointer';
    sortButton.style.borderRadius = '5px';
    sortButton.style.marginTop = '10px';
    
    sortButton.addEventListener('click', () => {
        sortByNewest = !sortByNewest;
        sortButton.textContent = `Sort by ${sortByNewest ? 'Newest' : 'Oldest'}`;
        sortClips();
    });
    
    function addSortButton() {
        const sortContainer = document.querySelector('div[data-test-selector="sort"]');
        if (sortContainer) {
            console.log("Found sort container element:", sortContainer);
            sortContainer.appendChild(sortButton);
            console.log("Sort button added.");
        } else {
            console.log("Sort container element not found. Setting up observer...");
            const observer = new MutationObserver(mutations => {
                const sortContainer = document.querySelector('div[data-test-selector="sort"]');
                if (sortContainer) {
                    console.log("Sort container element found by observer:", sortContainer);
                    sortContainer.appendChild(sortButton);
                    console.log("Sort button added.");
                    observer.disconnect();
                }
            });
            observer.observe(document.body, { childList: true, subtree: true });
        }
    }
    
    addSortButton();
    })();