nyaa.si增强

即将滚动到底后,自动读取下一页内容,附带回顶按钮,表/里站页面结构一样,所以脚本通用

  1. // ==UserScript==
  2. // @name nyaa.si增强
  3. // @namespace http://nyaa.si/
  4. // @version 0.2.1
  5. // @description 即将滚动到底后,自动读取下一页内容,附带回顶按钮,表/里站页面结构一样,所以脚本通用
  6. // @author allence_frede
  7. // @match https://*.nyaa.si/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=nyaa.si
  9. // @grant none
  10. // @license GNU GPLV3
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. function keyFilter(obj=null) {
  15. let k = $('#showModal textarea').val()
  16. k_arr = k.split('\n')
  17. let a_tag = obj?obj:$('tbody>tr')
  18. a_tag.each(function (){
  19. if (k.length<=0) {
  20. $(this).show()
  21. } else {
  22. $(this).show()
  23. let t = $(this).find('td:nth-child(2)>a:last-child').attr('title').toLowerCase()
  24. let check = false
  25. k_arr.map(ki=>{
  26. if(ki && t.includes(ki.toLowerCase())) check = true
  27. })
  28. if (check) {
  29. $(this).hide()
  30. }
  31. }
  32. })
  33. }
  34.  
  35. let lock = false
  36. window.addEventListener('scroll', async function () {
  37. if (lock) {
  38. return
  39. }
  40. if (window.pageYOffset + window.innerHeight >= window.document.querySelector('.table-responsive').scrollHeight) {
  41. lock = true
  42. let next_page = $('ul.pagination li:last-child a').attr('href')
  43. if (!next_page) {
  44. return
  45. }
  46. next_page = window.location.origin + next_page
  47. let res = await fetch(next_page)
  48. res = await res.text()
  49. let data_list = res.match(/<tbody>\n((.*\n)+)\s+<\/tbody>/)
  50. $('.table-responsive tbody').append(data_list[1])
  51. let pagination = res.match(/(<ul class="pagination">.*<\/ul>)/s)
  52. $('.pagination').replaceWith(pagination[1])
  53. keyFilter()
  54. lock = false
  55. }
  56. });
  57. window.addEventListener('scroll', function () {
  58. let have_el = Boolean($('#go-to-top').length)
  59. if (!have_el && window.pageYOffset >= 200) {
  60. let code = '<div id="go-to-top">\
  61. <div class="arrow"></div>\
  62. </div>\
  63. <style>\
  64. #go-to-top {\
  65. position: fixed;\
  66. bottom: 20px;\
  67. right: 20px;\
  68. width: 60px;\
  69. height: 60px;\
  70. border-radius: 50%;\
  71. background-color: pink;\
  72. cursor: pointer;\
  73. overflow: hidden;\
  74. display: flex;\
  75. justify-content: center;\
  76. align-items: center;\
  77. }\
  78. #go-to-top .arrow {\
  79. border-left: 3px solid #fff;\
  80. border-top: 3px solid #fff;\
  81. width: 28px;\
  82. height: 28px;\
  83. transform: rotate(45deg);\
  84. margin-top: 13px;\
  85. }\
  86. </style>'
  87.  
  88. $('body').append(code)
  89.  
  90. $('#go-to-top').on('click', function () {
  91. document.documentElement.scrollTop = document.body.scrollTop = 0
  92. })
  93. }
  94.  
  95. if (have_el && window.pageYOffset < 200) {
  96. $('#go-to-top').remove()
  97. }
  98. });
  99.  
  100. let modal_switch = true
  101. function showModal() {
  102. if (modal_switch) {
  103. $('#showModal').addClass('show')
  104. $('#showModal textarea').addClass('show')
  105. $('#showModal textarea').removeClass('default')
  106. modal_switch = false
  107. } else {
  108. $('#showModal').removeClass('show')
  109. $('#showModal textarea').addClass('default')
  110. $('#showModal textarea').removeClass('show')
  111. modal_switch = true
  112. }
  113. }
  114. //注入页面拓展元素
  115. let code = '<style>\
  116. #showModal.default {\
  117. position: fixed;\
  118. width: 130px;\
  119. top: 0;\
  120. left: 0;\
  121. z-index: 1001;\
  122. display: flex !important;\
  123. flex-direction: column;\
  124. }\
  125. #showModal.show {\
  126. background-color: #cf2d2d;\
  127. }\
  128. #showModal textarea.default {\
  129. display: none;\
  130. }\
  131. #showModal textarea.show {\
  132. width: 100%;\
  133. resize: none;\
  134. height: 150px;\
  135. }\
  136. </style>\
  137. <div id="showModal" class="default">\
  138. <button style="margin: 10px auto;">\
  139. 排除关键词\
  140. </button>\
  141. <textarea class="default" placeholder="支持多个关键词,一行一个"></textarea>\
  142. </div>'
  143. $('body').append(code)
  144. $('#showModal button').on('click', function () {
  145. showModal()
  146. })
  147. $('#showModal textarea').on('change', function () {
  148. keyFilter()
  149. })
  150. })();
  151.