Threadless Infinite Scroll

Infinite scroll for threadless.com

Fra og med 09.07.2017. Se den nyeste version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
        }
    });
})();