Reposition Recently Uploaded Chip

Repositions the "Recently Uploaded" chip to the front

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Reposition Recently Uploaded Chip
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Repositions the "Recently Uploaded" chip to the front
// @author       RadicalDowntownUrbanite
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?domain=youtube.com
// @grant        none
// @license      MIT
// ==/UserScript==

/**
 * Originally I wanted to hook unto the pushState of the site to check for the
 * position of the Recently Uploded chip, however YT uses some secret sauce I
 * am not familiar with to change page state which makes it difficult to hook
 * into it without just polling the url. Instead I use a mutation observer to
 * trigger whenever the DOM of the site changes and then update the position
 * of the Recently Uploaded chip if it's present on the page.
 */
(function() {
    'use strict';

    function debounce(func) {
        let timer;

        return (...args) => {
            clearTimeout(timer);
            timer = setTimeout(() => func.apply(this, args), 300);
        };
    }

    /**
     * Debounce the mutation observer callback because the MO can really thrash while the page is loading.
     * This will result in a slight delay before the RU chip is moved but won't affect page load performance either.
     */
    const updateRecentlyUploaded = debounce((mutations, me) => {
        if (!window.location.pathname === '/') {
            //console.debug(`=========== Not on main url ${window.location.href}`)
            return ;
        }

        const chips = [...document.querySelectorAll('iron-selector.ytd-feed-filter-chip-bar-renderer yt-chip-cloud-chip-renderer')];

        if (chips.length === 0) {
            //console.debug('=========== No chips');
            return;
        }

        const index = chips.findIndex(chip => chip.innerText === 'Recently uploaded')

        if (index > 1) {
            //console.debug('=========== Recently uploaded found and not first index');
            const chip = chips[index];
            const parent = chip.parentNode;
            parent.removeChild(chip);
            parent.prepend(chip);
         //} else if (index === 0) {
            //console.debug('=========== Recently uploaded already at first position');
         //} else {
            //console.debug('=========== Recently uploaded chip not found');
        }
    });

    // set up the mutation observer
    const observer = new MutationObserver(updateRecentlyUploaded);

    // start observing
    observer.observe(document, {
        childList: true,
        subtree: true
    });

})();