YouTube Uploads Sorter Button

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

目前为 2022-04-07 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Uploads Sorter Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.13
  5. // @description Adds a button to a YouTube channel's videos page which sorts recent uploads by views
  6. // @author Lex
  7. // @include /^https?:\/\/(www\.)?youtube\.com\/(c|channel|user)\//
  8. // @require https://code.jquery.com/jquery-3.2.1.min.js
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function($) {
  13. 'use strict';
  14.  
  15. function addButton() {
  16. if ($("#sortViewButton").length == 0) {
  17. $("<button>").attr("id", "sortViewButton").html("Sort by Views").click(sortByViews).prependTo("#sort-menu");
  18. }
  19. }
  20. function getViews(e) {
  21. try {
  22. let viewsTitle = $(e).find("a[aria-label]").attr("aria-label");
  23. if (viewsTitle.search(/No views$/) > -1) // video has no views yet
  24. return 0;
  25. else {
  26. const views = parseInt(/([\d,]+) views( - play Short)?$/.exec(viewsTitle)[1].replace(/,/g, ""));
  27. return views;
  28. }
  29. } catch(err) {
  30. return 0;
  31. }
  32. }
  33. function sortByViews() {
  34. let container = $("#page-manager #primary #items:visible");
  35. let items = container.children();
  36. container.append(items.toArray().sort(function(a, b) {
  37. return getViews(b) - getViews(a);
  38. }));
  39. }
  40.  
  41. setTimeout(addButton, 1000);
  42. addButton(); // try to add the button immediately
  43. $("#sub-menu:visible").on("DOMNodeInserted", addButton); // but also add button whenever the #sub-menu is changed
  44. })(window.jQuery);