InfiniteJandan

Waste your time faster by making images infinite on JANDAN.NET

目前為 2017-11-09 提交的版本,檢視 最新版本

// ==UserScript==
// @name         InfiniteJandan
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Waste your time faster by making images infinite on JANDAN.NET
// @author       w1ndy
// @match        https://jandan.net/pic
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let loading = false;
    let currentPage = $;

    function getNextPage (jq) {
        return new Promise((resolve, reject) => {
            const URI = $(jq.find('.previous-comment-page:first')).attr('href');
            if (!URI) {
                reject('No next page!');
            } else {
                $.get(URI, data => {
                    const page = '<div id="new-page">' + data.replace(/^[\s\S]*<body.*?>|<\/body>[\s\S]*$/ig, '') + '</div>';
                    window.history.pushState(null, '', URI);
                    resolve($(page));
                })
                .fail(() => {
                    reject('Unable to load next page!');
                });
            }
        });
    }

    // Move up the comment box
    $('#commentform').insertBefore('#comments > div:nth-child(1)');
    $('#comments > div:nth-last-child(1)').insertBefore('#commentform');

    // Insert infinite sensor
    $('<div id="infinite-status">Loading next page...</div>')
        .css('height', '50px')
        .css('line-height', '50px')
        .css('text-align', 'center')
        .insertAfter('.commentlist');

    document.addEventListener('scroll', () => {
        if (loading) {
            return;
        }
        if ($(window).scrollTop() + $(window).height() > $('#infinite-status').offset().top) {
            console.log('loading...');
            loading = true;
            getNextPage(currentPage)
                .then(page => {
                    $('.commentlist').append(page.find('.commentlist > *'));
                    currentPage = page;
                    loading = false;
                })
                .catch(err => {
                    $('#infinite-status').html(err);
                });
        }
    });
})();