PubMed自动加载下一页

滚动到PubMed页面底部时自动加载更多结果。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         PubMed Auto Load More Results
// @name:zh-CN   PubMed自动加载下一页
// @name:zh-TW   PubMed自動加載下一頁
// @namespace    Zhang
// @version      1.1
// @description  Automatically load more results when scrolling to the bottom of PubMed pages. 
// @description:zh-CN  滚动到PubMed页面底部时自动加载更多结果。
// @description:zh-TW  滾動到PubMed頁面底部時自動載入更多結果。
// @author       Zhang
// @match        https://*pubmed.ncbi.nlm.nih.gov/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
  'use strict';

  function isBottom() {
    let scrollHeight = document.documentElement.scrollHeight;
    let scrollTop = document.documentElement.scrollTop;
    let clientHeight = document.documentElement.clientHeight;
    return (scrollHeight - scrollTop - clientHeight) < 10;
  }

  function clickButton() {
    let button = document.querySelector("button.load-button");
    if (button && button.style.display !== "none") {
      button.click();
      observer.observe(document.body, {childList: true, subtree: true});
    }
  }

  let timer;

  function toggleTimer() {
    if (timer) {
      clearInterval(timer);
      timer = null;
      console.log("停止自动加载下一页");
    } else {
      timer = setInterval(function() {
        if (isBottom()) {
          clickButton();
        }
      }, 500);
      console.log("开始自动加载下一页");
    }
  }

  function addClickListener(link) {
    if (!link.hasAttribute("data-listener")) {
      link.addEventListener("click", function(event) {
        event.preventDefault();
        let url = link.href;
        GM_openInTab(url, true);
      });
      link.setAttribute("data-listener", "true");
    }
  }

  let observer = new MutationObserver(function(mutations) {
    for (let mutation of mutations) {
      if (mutation.type === "childList") {
        for (let node of mutation.addedNodes) {
          if (node.nodeType === 1 && node.classList.contains("docsum-content")) {
            let link = node.querySelector("a.docsum-title");
            if (link) {
              addClickListener(link);
            }
          }
        }
      }
    }
  });

  window.addEventListener("load", function() {
    toggleTimer();
    addClickListener();
  });
})();