Greasy Fork

Greasy Fork is available in English.

饭否-手机版自动翻页

在 m.fanfou.com 首页, 允许免刷新加载下一页,和滚动条到达底部后自动翻页

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name 饭否-手机版自动翻页
// @version 1.0.1
// @author HackMyBrain
// @description 在 m.fanfou.com 首页, 允许免刷新加载下一页,和滚动条到达底部后自动翻页
// @include http://m.fanfou.com/home
// @include https://m.fanfou.com/home
// @include http://m.fanfou.com/home/*
// @include https://m.fanfou.com/home/*
// @include http://m.fanfou.com/home?v=*
// @include https://m.fanfou.com/home?v=*
// @include http://m.fanfou.com/home?max_id=*
// @include https://m.fanfou.com/home?max_id=*
// @namespace http://greasyfork.icu/users/2844
// ==/UserScript==


(function (){
    'strict mode'
    
    //设置项开始
    var config = { doAutoPage : true }; //是否启用自动翻页:true 为启用, false 为禁用. (必须使用半角字符)
    //设置项结束

    var status = { isLoading : false, timer : undefined }; 
    
    function getScrollTop() {
        return Math.max(document.body.scrollTop, document.documentElement.scrollTop);
    }

    function getClientHeight() {
        return document.documentElement.clientHeight;
    }

    function getScrollHeight() {
        return Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
    }

    function checkPageBottom() { 
        if ( ( getScrollTop() + getClientHeight() == getScrollHeight() ) ) {
            loadNextPage();
        }
    }
    
    function clickToLoad(e) { 
        e.preventDefault();
        e.stopPropagation();
        loadNextPage();
    }
    
    function replaceNextURL() { 
        var max_id = pagi.parentElement.previousElementSibling.querySelector('span.a > a[href*="/msg.favorite"]').href.match(/\/([^\/]+$)/)[1];
        pagi.href = 'home?max_id=' + max_id;
        pagi.innerHTML = '更多';
    }
    
    function loadNextPage() {
        if ( status.isLoading ) return;
        status.isLoading = true;
        var xhr = new XMLHttpRequest();
        xhr.open('GET', pagi.href);
        xhr.timeout = 10000;
        xhr.ontimeout = function(){
            status.isLoading = false;
            pagi.innerHTML = '更多 (加载超时, 点击重试)';
        };
        xhr.onloadstart = function(){
            pagi.innerHTML = '更多 (正在载入)';
        };
        xhr.onload = function(){
            var nextpage_doc = document.implementation.createHTMLDocument('');
            nextpage_doc.body.innerHTML = xhr.responseText;
            xhr = null;
            var mes = nextpage_doc.evaluate(".//p/a[@class='p']/.." , nextpage_doc.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            if ( mes.snapshotLength == 0 ) {
                pagi.innerHTML = '达到首页(/home)允许的时间回溯限制, 无法显示更早的消息';
                clearInterval(status.timer);
                return;
            }
            var newElement = document.createDocumentFragment();
            for (var i = 0; i < mes.snapshotLength; i++) {
                newElement.appendChild( mes.snapshotItem(i) );
            }
            mes = null;
            nextpage_doc = null;
            pagi.parentElement.parentElement.insertBefore(newElement, pagi.parentElement);
            replaceNextURL();
            status.isLoading = false;
        };
        xhr.send();
    }

    var pagi = document.querySelector('[accesskey="6"]');
    pagi.addEventListener('click', clickToLoad, false);
    
    replaceNextURL();
    
    if ( config.doAutoPage ) {
        status.timer = setInterval(checkPageBottom, 300);
    }
})();