YouTube Uploads Sorter Button

Adds a button to a YouTube channel's videos page which sorts recent uploads by views

目前为 2023-01-28 提交的版本。查看 最新版本

// ==UserScript==
// @name         YouTube Uploads Sorter Button
// @namespace    http://tampermonkey.net/
// @version      0.14.3
// @description  Adds a button to a YouTube channel's videos page which sorts recent uploads by views
// @author       Lex
// @match        *://*.youtube.com/@*
// @match        *://*.youtube.com/*/featured
// @match        *://*.youtube.com/*/videos
// @exclude-match *://*.youtube.com/watch
// @require      https://code.jquery.com/jquery-3.2.1.min.js
// @grant        none
// ==/UserScript==

(function($) {
    'use strict';

    function addButton() {
        if ($("#sortViewButton").length == 0) {
            const chip = $("<yt-chip-cloud-chip-renderer>").attr("chip-style", "STYLE_DEFAULT").attr("id", "sortViewButton").click(sortByViews);
            const container = $("#chips-wrapper iron-selector");
            chip.appendTo(container);
            chip.text('Sort by Views'); // have to set the text here, after the append
        }
    }
    function getViews(e) {
        try {
            //console.log(e);
            const viewsTitle = $(e).find('a')[2].getAttribute("aria-label");
            //console.log(`Found title: ${viewsTitle}`);
            if (viewsTitle.search(/No views$/) > -1) // video has no views yet
                return 0;
            else {
                const views = parseInt(/([\d,]+) views( - play Short)?$/.exec(viewsTitle)[1].replace(/,/g, ""));
                return views;
            }
        } catch(err) {
            //console.log(err);
            return 0;
        }
    }

    function sortByViews() {
        console.log("Sorting...");
        const items = $("ytd-rich-item-renderer");
        console.log(`Found ${items.length} videos on the page.`);
        //console.log(items);
        //console.log(getViews(items[0]));

        // Array of each parent for a given index.
        // e.g. if there are 4 videos in the first row container, the first 4 indexes are that first row
        const parents = [...items].map(e => e.parentNode);
        //console.log(parents);
        const sorted = items.toArray().sort(function(a, b) {
            return getViews(b) - getViews(a);
        });
        for (let item of sorted) {
          // Remove item from its parent and append it to the ordered parent
          const parent = parents.shift();
          //console.log("Parent: ", parent);
          //console.log("Removing", item, "from its parent");
          item.parentNode.removeChild(item);
          parent.append(item);
        }
    }

    function waitForLoad(query, callback) {
        if (document.querySelector(query)) {
            callback();
        } else {
            setTimeout(waitForLoad.bind(null, query, callback), 100);
        }
    }

    waitForLoad("#chips-wrapper", addButton);
})(window.jQuery);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址