YouTube Uploads Sorter Button

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

目前為 2022-07-13 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name YouTube Uploads Sorter Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.13.1
  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.$['video-title'].getAttribute("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. //console.log(err);
  31. return 0;
  32. }
  33. }
  34. function sortByViews() {
  35. let container = $("#contents #items");
  36. let items = container.children();
  37. //console.log(items);
  38. //console.log(getViews(items[0]));
  39. container.append(items.toArray().sort(function(a, b) {
  40. return getViews(b) - getViews(a);
  41. }));
  42. }
  43.  
  44. setTimeout(addButton, 1000);
  45. addButton(); // try to add the button immediately
  46. $("#sub-menu:visible").on("DOMNodeInserted", addButton); // but also add button whenever the #sub-menu is changed
  47. })(window.jQuery);