MZITU Infinite Scroll

Free your hand from mouse/keyboard

  1. // ==UserScript==
  2. // @name MZITU Infinite Scroll
  3. // @namespace http://www.mzitu.com/
  4. // @version 0.1
  5. // @description Free your hand from mouse/keyboard
  6. // @author You
  7. // @match https://www.mzitu.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. const displayArea = document.querySelector('.main-image p');
  14. let page = document;
  15. let lock = false;
  16. let lastPage = null;
  17. document.addEventListener('scroll', checkAndLoad, {passive: true});
  18. checkAndLoad();
  19.  
  20. async function checkAndLoad() {
  21. if (document.body.clientHeight - window.scrollY < window.innerHeight * 4 && !lock) {
  22. lock = true;
  23. page = await loadPage(page);
  24. lock = false;
  25. }
  26. }
  27. function loadPage(dom) /* Promise<HTMLDocument>: the document of next page */ {
  28. const nextPageA = dom.querySelector('.pagenavi a:last-child');
  29. if (nextPageA.innerText !== '下一页»' || lastPage === nextPageA.href) return;
  30.  
  31. lastPage = nextPageA.href;
  32. return fetch(nextPageA.href)
  33. .then(e => e.text())
  34. .then(rawHtml => {
  35. const parser = new DOMParser();
  36. const newDom = parser.parseFromString(rawHtml, 'text/html');
  37. displayArea.appendChild(newDom.querySelector('.main-image img'));
  38. return newDom;
  39. });
  40. }
  41. })();