Hacker News Infinite Scroll

Adds infinite scroll to HackerNews

当前为 2019-10-06 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @description Adds infinite scroll to HackerNews
// @name Hacker News Infinite Scroll
// @namespace Violentmonkey Scripts
// @match https://news.ycombinator.com/
// @version 1.0.0
// @grant none
// ==/UserScript==

const xhr = new XMLHttpRequest();
const regex = new RegExp(/<center>.*(<table id="hnmain".*)<\/center>/gms);
let count = 2;
window.onscroll = function() {
    if ((window.innerHeight + Math.ceil(window.pageYOffset)) >= document.body.offsetHeight) {
        let testResponse;
        let parsed;
        xhr.open('GET', `https://news.ycombinator.com/news?p=${count}`);
        xhr.onload = function() {
            if (xhr.status === 200) {
                testResponse = xhr.responseText;
                parsed = regex.exec(testResponse);
                let newDiv = document.createElement('div');
                newDiv.innerHTML = parsed[1];
                let table = document.querySelector('table.itemlist');
                let tableMore = [...table.querySelectorAll('tr')].slice(-2)[0];
                [...newDiv.querySelector('tr#pagespace + tr tbody').querySelectorAll('tr')].slice(0, 90).forEach((newTr, idx) => {
                    tableMore.parentNode.insertBefore(newTr, tableMore);
                })
                count++;
            }
            else {
                alert('Request failed.  Returned status of ' + xhr.status);
            }
        };
        xhr.send();
    }
};