Threadless Infinite Scroll

Infinite scroll for threadless.com

Stan na 09-07-2017. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Threadless Infinite Scroll
// @namespace    https://github.com/arieljannai
// @version      0.2
// @description  Infinite scroll for threadless.com
// @author       Ariel Jannai
// @match        *://www.threadless.com/*
// @icon         https://www.threadless.com/favicon.ico
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var infiniteLoadingGif = "<div class='infinite-loading' style='text-align:center; margin:0 auto; width:50%'><img src='https://i.imgur.com/NH1zQtD.gif'></img></div>";

    function isScrolledIntoView(elem) {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();

        var elemTop = $(elem).offset().top;
        var elemBottom = elemTop + $(elem).height();

        return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
    }

    var back_top = $('.back_top');
    var pathname = document.location.pathname;
    var pageMatch = pathname.match(/page,(\d+)/);
    var currPage = pageMatch ? pageMatch[1] : 1;
    var requestNext = true;
    pathname = pathname.replace(/\/page,\d+/, '') + '/page,' + currPage;

    function getPagePath(pageNum) {
        return pathname.replace(/page,\d+/, 'page,' + pageNum);
    }

    function appendPageItems(pageNum) {
        console.log('loading page: ', pageNum);
        var msg = '';
        $('<div>').load(getPagePath(pageNum) + ' #catalog_products', function(html, status) {
            console.log('status: ', status);
            if (status !== 'success') {
                requestNext = false;
                msg = 'Oops. There was an error while loading the next page items (page ' + pageNum + ').';
                console.error(msg);
                $('<div style="text-align:center; color:red">' + msg + '</div>').insertAfter('#catalog_products');
                $('div .catalog_browsing').show();
            } else if (html.indexOf('class="no-results"') > -1) {
                console.log('not loaded: ', pageNum);
                msg = 'No more items :(';
                console.log(msg);
                $('<div style="text-align:center">' + msg + '</div>').insertAfter('#catalog_products');
            } else {
                $('#catalog_products').append($(this));
                requestNext = true;
                console.log('loaded:', pageNum);
            }

            $('.infinite-loading').hide();
        });
    }

    $('div .catalog_browsing').hide();
    $(infiniteLoadingGif).insertBefore('div .catalog_browsing');
    $('.infinite-loading').hide();

    $(window).scroll(function() {
        if (requestNext && isScrolledIntoView(back_top)) {
            $('.infinite-loading').show();
            requestNext = false;
            appendPageItems(++currPage);
        }
    });
})();