PubMed自動加載下一頁

滾動到PubMed頁面底部時自動載入更多結果。

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
  });
})();