Nyaa Filter

为nyaa上添加更多过滤条件

  1. // ==UserScript==
  2. // @name Nyaa Filter
  3. // @version 1.0
  4. // @description 为nyaa上添加更多过滤条件
  5. // @icon https://sukebei.nyaa.si/static/favicon.png
  6. // @author Amamiya
  7. // @match https://sukebei.nyaa.si/*
  8. // @match https://nyaa.si/*
  9. // @match https://nyaa.land/*
  10. // @exclude https://sukebei.nyaa.si/*/*
  11. // @exclude https://nyaa.si/*/*
  12. // @exclude https://nyaa.land/*/*
  13. // @grant GM_addStyle
  14. // @require https://code.jquery.com/jquery-3.6.0.min.js
  15. // @license Apache-2.0 license
  16. // @namespace https://greasyfork.org/users/801480
  17. // ==/UserScript==
  18.  
  19. (function () {
  20. 'use strict';
  21.  
  22. // 条件框
  23. var nodeHtml = '<div id="tool-card" class="centered-style"><div class="card-body"><label for="earliestDate">最早日期 <input type="date" style="width:75%" id="earliestDate"></label><label for="latestDate">最晚日期 <input type="date" id="latestDate" style="width:75%"></label><label for="downloadComplete">下载完成 <input type="number" style="width:75%" id="downloadComplete"></label></div><div class="card-footer"><button id="RESET_BUTTON">重置</button><button id="FILTER_BUTTON">确定</button></div></div>';
  24. var tempNode = document.createElement('div');
  25. tempNode.innerHTML = nodeHtml;
  26. var toolCard = tempNode.firstChild;
  27. document.body.appendChild(toolCard);
  28.  
  29. // 重置
  30. $("#RESET_BUTTON").on("click", function () {
  31. $("#earliestDate").val("");
  32. $("#latestDate").val("");
  33. $("#downloadComplete").val("");
  34. $("tr:hidden").show();
  35. });
  36.  
  37. // 确定
  38. $("#FILTER_BUTTON").on("click", function () {
  39. applyFilter();
  40. });
  41. watchNewTr();
  42. dargCard();
  43. // 样式
  44. GM_addStyle(`
  45. #tool-card {
  46. z-index: 998;
  47. position: fixed;
  48. left: 10px;
  49. top: 400px;
  50. background-color: #0f0e17;
  51. color: #a7a9be;
  52. display: flex;
  53. flex-direction: column;
  54. padding: 0.75rem 1rem 0.4rem;
  55. font-size: 1.2rem;
  56. font-weight: 600;
  57. border-radius: 6px;
  58. border: 2px solid #337ab7;
  59. min-width: 160px;
  60. }
  61. .card-body {
  62. display: flex;
  63. flex-direction: column;
  64. }
  65. .card-footer {
  66. display: flex;
  67. justify-content: space-around;
  68. margin-top: 0.3rem;
  69. }
  70. #tool-card button {
  71. margin-bottom: 0.2rem;
  72. background-color: #337ab7;
  73. color: #fffffe;
  74. padding: 0.3rem 0.5rem;
  75. border: none;
  76. border-radius: 3px;
  77. font-size: 1.2rem;
  78. font-weight: 600;
  79. cursor: pointer;
  80. }
  81. #tool-card label {
  82. margin-bottom: 0.5rem;
  83. }
  84. #tool-card input[type="date"],
  85. #tool-card input[type="number"] {
  86. padding: 0.5rem;
  87. border-radius: 4px;
  88. border: 1px solid #ccc;
  89. }
  90. .centered-style {
  91. position: fixed;
  92. top: 50%;
  93. transform: translateY(-50%);
  94. }
  95. `);
  96.  
  97.  
  98.  
  99. })();
  100.  
  101.  
  102.  
  103.  
  104. function watchNewTr() {
  105. var observer = new MutationObserver(function (mutations) {
  106.  
  107. mutations.forEach(function (mutation) {
  108. var filterValue = parseInt($("#downloadComplete").val(), 10);
  109. var earliestDate = $("#earliestDate").val();
  110. var latestDate = $("#latestDate").val();
  111. mutation.addedNodes.forEach(function (node) {
  112. if (node.tagName && node.tagName.toLowerCase() === 'tr') {
  113. //applyFilter();
  114. var timeTd = $(node).find("td:nth-child(5)");
  115. var timeValue = Date.parse(timeTd.text());
  116. var downloadTd = $(node).find("td:last-child");
  117. var downloadValue = parseInt(downloadTd.text(), 10);
  118. if (downloadValue < filterValue) {
  119. $(node).hide();
  120. }
  121. if (earliestDate) {
  122. if (timeValue < Date.parse(earliestDate)) {
  123. $(node).hide();
  124. }
  125. }
  126. if (latestDate) {
  127. if (timeValue > Date.parse(latestDate)) {
  128. $(node).hide();
  129. }
  130. }
  131. }
  132. });
  133. });
  134. });
  135. var config = { childList: true, subtree: true };
  136. observer.observe(document.body, config);
  137. }
  138.  
  139. function applyFilter() {
  140. $("tr").show();
  141. var filterValue = parseInt($("#downloadComplete").val(), 10);
  142. var earliestDate = $("#earliestDate").val();
  143. var latestDate = $("#latestDate").val();
  144. $("tr").each(function () {
  145. var timeTd = $(this).find("td:nth-child(5)");
  146. var timeValue = Date.parse(timeTd.text());
  147. var downloadTd = $(this).find("td:last-child");
  148. var downloadValue = parseInt(downloadTd.text(), 10);
  149. if (downloadValue < filterValue) {
  150. $(this).hide();
  151. }
  152. if (earliestDate) {
  153. if (timeValue < Date.parse(earliestDate)) {
  154. $(this).hide();
  155. }
  156. }
  157. if (latestDate) {
  158. if (timeValue > Date.parse(latestDate)) {
  159. $(this).hide();
  160. }
  161. }
  162. });
  163. }
  164.  
  165. function tryCatch(callBack) {
  166. try {
  167. callBack && callBack()
  168. } catch (e) {
  169. console.log(e)
  170. }
  171. }
  172.  
  173. function dargCard() {
  174. tryCatch(() => {
  175. $("#tool-card").mousedown(function () {
  176. $("#tool-card").on({
  177. mousedown: function (e) {
  178. var el = $(this);
  179. var os = el.offset();
  180. var dx = e.pageX - os.left
  181. var dy = e.pageY - os.top;
  182. $(document).on('mousemove.drag', function (e) {
  183. el.offset({
  184. top: e.pageY - dy,
  185. left: e.pageX - dx
  186. });
  187. });
  188. },
  189. mouseup: function (e) {
  190. $(document).off('mousemove.drag');
  191. }
  192. })
  193. })
  194. })
  195. }