rarbg-visual-ordering-opinionated

Colors cells to classify them

  1. // ==UserScript==
  2. // @name rarbg-visual-ordering-opinionated
  3. // @namespace https://gitlab.com/aspootarya
  4. // @version 2022.04.22
  5. // @description Colors cells to classify them
  6. // @author aspootarya
  7. // @contributor darkred
  8. // @license MIT
  9. // @include /^(https?:)?\/\/(www\.)?(rarbg|rbg(\.(bypassed|unblockall|unblocked))?|rarbgaccess(ed)?|rarbgget|rarbgmirror(ed)?|rarbgproxy|rarbgproxied|rarbgprx|rarbgs|rarbgto|rarbgunblock|proxyrarbg|unblocktorrent)\.(to|com|org|is|xyz|lol|vc|link)\/(rarbg-proxy-unblock\/)?(torrents\.php.*|catalog\/.*|tv\/.*|top10)$/
  10. // @grant none
  11. // @require https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
  12. // @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js
  13. // @require https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.6/moment-timezone-with-data-2010-2020.js
  14. // @require https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js
  15. // @run-at document-idle
  16. // ==/UserScript==
  17.  
  18. /* global jstz, moment */
  19. function addAndModifyColumns() {
  20.  
  21. var titleCells = document.querySelectorAll('.lista2t > tbody > tr[class="lista2"] > td:nth-child(2)'); // the column 'Title'
  22. var sizeCells = document.querySelectorAll('.lista2t > tbody > tr[class="lista2"] > td:nth-child(4)'); // the column 'Size'
  23. var seedCells = document.querySelectorAll('.lista2t > tbody > tr[class="lista2"] > td:nth-child(5)'); // the column 'Seeds'
  24.  
  25. for (let i = 0; i < sizeCells.length; i++) {
  26. sizeCells[i].setAttribute("style", getColorByFileSize(getFileSize(sizeCells[i].innerText)));
  27. seedCells[i].innerHTML = `${seedCells[i].textContent}`;
  28. seedCells[i].setAttribute("style", getColorBySeeds(Number(seedCells[i].innerText)));
  29. if (titleCells[i].innerText.match(/(?:\.SD|480p|\sSD)/i)) {
  30. titleCells[i].setAttribute("style", getColorByRarity('Legendary'));
  31. }
  32. }
  33. }
  34.  
  35. // Customize the strings in the locale to display "1 minute ago" instead of "a minute ago" (https://github.com/moment/moment/issues/3764#issuecomment-279928245)
  36. moment.updateLocale('en', {
  37. relativeTime: {
  38. future: 'in %s',
  39. past: '%s ago',
  40. s: 'seconds',
  41. m: '1 minute',
  42. mm: '%d minutes',
  43. h: '1 hour',
  44. hh: '%d hours',
  45. d: '1 day',
  46. dd: '%d days',
  47. M: '1 month',
  48. MM: '%d months',
  49. y: '1 year',
  50. yy: '%d years'
  51. }
  52. });
  53.  
  54. function convertToLocalTimezone(timestamps) {
  55. const localTimezone = jstz.determine().name();
  56. const serverTimezone = 'Europe/Berlin'; // GMT+1
  57. for (let i = 0; i < timestamps.length; i++) {
  58. const initialTimestamp = timestamps[i].textContent;
  59. if (moment(initialTimestamp, 'YYYY-MM-DD HH:mm:ss', true).isValid()) { // As of moment.js v2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly, including delimeters.
  60. const convertedToLocalTimezone = moment.tz(initialTimestamp, serverTimezone).tz(localTimezone);
  61. timestamps[i].textContent = convertedToLocalTimezone.fromNow();
  62. const format = 'YYYY-MM-DD HH:mm:ss';
  63. timestamps[i].title = convertedToLocalTimezone.format(format);
  64. const diffInDays = moment().diff(convertedToLocalTimezone, 'days');
  65. timestamps[i].setAttribute("style", getColorByRarity(getRarityByDateDiff(diffInDays)));
  66. }
  67. }
  68.  
  69. // recalculate the relative times every 10 sec
  70. (function () {
  71. for (let i = 0; i < timestamps.length; i++) {
  72. timestamps[i].textContent = moment(timestamps[i].title).fromNow();
  73. }
  74. setTimeout(arguments.callee, 1 * 60 * 1000);
  75. })();
  76.  
  77. }
  78.  
  79. function getRarityByDateDiff(diffInDays) {
  80. if (diffInDays < 7) {
  81. return 'Legendary';
  82. }
  83. if (diffInDays < 30) {
  84. return 'Epic';
  85. }
  86. if (diffInDays < 90) {
  87. return 'Rare';
  88. }
  89. if (diffInDays < 180) {
  90. return 'Uncommon';
  91. }
  92. return 'Common';
  93. }
  94.  
  95. function getColorBySeeds(seeds) {
  96. if (seeds > 50) {
  97. return getColorByRarity('Legendary');
  98. }
  99. if (seeds > 20) {
  100. return getColorByRarity('Epic');
  101. }
  102. if (seeds > 10) {
  103. return getColorByRarity('Rare');
  104. }
  105. if (seeds > 3) {
  106. return getColorByRarity('Uncommon');
  107. }
  108. return getColorByRarity('Common');
  109. }
  110.  
  111. function getColorByFileSize(fileSize) {
  112. const MB = 1000000;
  113. if (fileSize > 120 * MB && fileSize < 1000 * MB) {
  114. return getColorByRarity('Legendary');
  115. }
  116. if (fileSize > 100 * MB && fileSize < 2000 * MB) {
  117. return getColorByRarity('Epic');
  118. }
  119. if (fileSize > 80 * MB && fileSize < 4000 * MB) {
  120. return getColorByRarity('Rare');
  121. }
  122. if (fileSize > 60 * MB && fileSize < 6000 * MB) {
  123. return getColorByRarity('Uncommon');
  124. }
  125. return getColorByRarity('Common');
  126. }
  127.  
  128. function getColorByRarity(rarity) {
  129. if (rarity === 'Legendary') {
  130. return "background-color: #ffc107;"; // material amber 500
  131. }
  132. if (rarity === 'Epic') {
  133. return "color: white; background-color: #9c27b0;"; // material purple 500
  134. }
  135. if (rarity === 'Rare') {
  136. return "color: white; background-color: #3f51b5;"; // material indigo 500
  137. }
  138. if (rarity === 'Uncommon') {
  139. return "background-color: #4caf50;"; // material green 500
  140. }
  141. if (rarity === 'Common') {
  142. return "color: #546e7a; background-color: #eeeeee;"
  143. }
  144. return "";
  145. }
  146.  
  147. function getFileSize(text) {
  148. let fileSize = text.replace(" ", "");
  149. if (fileSize.indexOf("MB") != -1) {
  150. fileSize = fileSize.replace("MB", "");
  151. fileSize = Number(fileSize);
  152. fileSize *= 1000000;
  153. } else if (fileSize.indexOf("GB") != -1) {
  154. fileSize = fileSize.replace("GB", "");
  155. fileSize = Number(fileSize);
  156. fileSize *= 1000000000;
  157. } else {
  158. fileSize = 0;
  159. }
  160. return fileSize;
  161. }
  162.  
  163. const timestamps = document.querySelectorAll('td[width="150px"]');
  164. convertToLocalTimezone(timestamps);
  165. addAndModifyColumns();