nyaa.si增强

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

Verze ze dne 21. 07. 2022. Zobrazit nejnovější verzi.

// ==UserScript==
// @name         nyaa.si增强
// @namespace    http://nyaa.si/
// @version      0.1
// @description  即将滚动到底后,自动读取下一页内容,附带回顶按钮,表/里站页面结构一样,所以脚本通用
// @author       allence_frede
// @match        https://*.nyaa.si/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=nyaa.si
// @grant        none
// @license      GNU GPLV3
// ==/UserScript==

(function () {
  'use strict';

  let lock = false
  window.addEventListener('scroll', async function () {
    if (lock) {
      return
    }
    if (window.pageYOffset + window.innerHeight >= window.document.querySelector('.table-responsive').scrollHeight) {
      lock = true
      let next_page = $('ul.pagination li:last-child a').attr('href')
      if (!next_page) {
        return
      }
      next_page = window.location.origin + next_page
      let res = await fetch(next_page)
      res = await res.text()
      let data_list = res.match(/<tbody>\n((.*\n)+)\s+<\/tbody>/)
      $('.table-responsive tbody').append(data_list[1])
      let pagination = res.match(/(<ul class="pagination">.*<\/ul>)/)
      $('.pagination').replaceWith(pagination[1])
      lock = false
    }
  });
  window.addEventListener('scroll', function () {
    let have_el = Boolean($('#go-to-top').length)
    if (!have_el && window.pageYOffset >= 200) {
      let code = '<div id="go-to-top">\
        <div class="arrow"></div>\
      </div>\
      <style>\
        #go-to-top {\
          position: fixed;\
          bottom: 20px;\
          right: 20px;\
          width: 60px;\
          height: 60px;\
          border-radius: 50%;\
          background-color: pink;\
          cursor: pointer;\
          overflow: hidden;\
          display: flex;\
          justify-content: center;\
          align-items: center;\
        }\
        #go-to-top .arrow {\
          border-left: 3px solid #fff;\
          border-top: 3px solid #fff;\
          width: 28px;\
          height: 28px;\
          transform: rotate(45deg);\
          margin-top: 13px;\
        }\
      </style>'

      $('body').append(code)

      $('#go-to-top').on('click', function () {
        document.documentElement.scrollTop = document.body.scrollTop = 0
      })
    }

    if (have_el && window.pageYOffset < 200) {
      $('#go-to-top').remove()
    }
  });


})();