MeiziTuAutoLoad

MeiziTuAutoLoad!

  1. // ==UserScript==
  2. // @name MeiziTuAutoLoad
  3. // @namespace //tampermonkey.net/
  4. // @version 0.2.0
  5. // @description MeiziTuAutoLoad!
  6. // @author cuzfinal
  7. // @match http*://www.mzitu.com/*
  8. // @grant none
  9. // ==/UserScript==
  10. let loadInterval
  11. const config = {
  12. hostName: location.hostname,
  13. interval: 200, // 发起一次请求后200ms内不再发起
  14. isInterval: 0,
  15. overSet: document.documentElement.clientHeight * 0.2 // 分页导航距离屏幕底部大于屏幕高度20%时触发加载
  16. }
  17. const defaultParams = {
  18. method: 'GET',
  19. }
  20.  
  21. const hostRegular = {
  22. 'www.mzitu.com': {
  23. container: '.main-image',
  24. pageUrl() { return `/${this.articleId}/${++this.page}` },
  25. page: location.href.match(/\/\d(\d)?$/) && location.href.match(/\/(\d(\d)?)$/)[1] || 1,
  26. articleId: location.href.match(/com\/(\d+)/)[1],
  27. hideNodes: null,
  28. nav: '.pagenavi',
  29. isOver(next) {
  30. return next.nav.lastElementChild.textContent === "下一组»"
  31. },
  32. extra: () => { }
  33. }
  34. }
  35.  
  36. const current = hostRegular[config.hostName]
  37. const box = document.querySelector(current.container)
  38. let pageNav = getNav()
  39. const io = new IntersectionObserver(loadNext)
  40.  
  41. function getNav() {
  42. return document.querySelector(current.nav)
  43. }
  44.  
  45. function getTitle(node) {
  46. if (node.tagName === 'TITLE') {
  47. return node.outerText
  48. }
  49. }
  50.  
  51. function parseNextPage(dom) {
  52. let content, nav, title
  53. content = current.next ? dom.querySelectorAll(current.next) : [...dom.querySelector(current.container).children]
  54. nav = dom.querySelector(current.nav)
  55. const result = { content, nav, title }
  56. current.isOver && current.isOver(result, dom)
  57. return result
  58. }
  59.  
  60. async function loadNext(entries) {
  61. if(!entries[0].isIntersecting) return
  62. io.unobserve(pageNav)
  63.  
  64. const nextUrl = current.pageUrl()
  65. try {
  66. const resp = await fetch(nextUrl, current.params || defaultParams)
  67. const parser = new DOMParser()
  68. const dom = parser.parseFromString(await resp.text(), 'text/html')
  69. const next = parseNextPage(dom)
  70. if (resp.status === 404) {
  71. loadOver()
  72. } else {
  73. next.content.forEach(el => box.appendChild(el))
  74. if (next.nav) {
  75. pageNav.parentNode.replaceChild(next.nav, pageNav)
  76. pageNav = getNav()
  77. io.observe(pageNav)
  78. }
  79. if (next.title) {
  80. document.title = next.title
  81. }
  82. }
  83. current.isOver(next, dom) && (loadOver())
  84. } catch (e) {
  85. loadOver()
  86. console.log(e)
  87. }
  88. }
  89.  
  90. function hideURL() {
  91. [...document.querySelectorAll(current.hideNodes)].forEach(el => el.style.display = "none")
  92. }
  93.  
  94. function loadOver() {
  95. io.disconnect()
  96. hideURL()
  97. current.extra && current.extra()
  98. console.log('Preload loaded!')
  99. }
  100.  
  101. (function () {
  102. // Your code here...
  103. current.beforeLoad && current.beforeLoad()
  104. window.addEventListener("load", () => io.observe(pageNav))
  105. console.log('Preload loading...')
  106. })();