eBay Collection Sorter

Sorts Collections on eBay

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        eBay Collection Sorter
// @namespace   http://www.facebook.com/Tophness
// @description Sorts Collections on eBay
// @include     http://*ebay.tld/cln/*
// @version     1
// @grant       none
// ==/UserScript==

var prevY = 0;
windowscroll = setInterval(doscroll, 800);
function doscroll() {
	if (window.scrollMaxY !== prevY) {
		prevY = window.scrollMaxY;
		window.scrollTo(0, window.scrollMaxY);
	} else {
		clearInterval(windowscroll);
		window.scrollTo(0, 0);
		doSort();
	}
}

function doSort() {
	var prices = new Array();
	var items = document.getElementsByClassName('collection-gallery')[0].childNodes;
	for (var i = 0; i < items.length; ++i) {
		if (items[i].tagName == 'DIV') {
			var nitems = items[i].getElementsByTagName('div');
			for (var j = 0; j < nitems.length; ++j) {
				if (nitems[j].className == 'itemPrice') {
					if (nitems[j].textContent.indexOf('$') != -1) {
						var price = nitems[j].textContent.substring(nitems[j].textContent.indexOf('$') + 1);
						prices.push({
							price : price,
							el : items[i]
						});
					}
				}
			}
		}
	}

	prices.sort(function (a, b) {
		return a.price - b.price;
	});

	for (var ni = 0; ni < items.length; ++ni) {
		if (items[ni].tagName == 'DIV') {
			document.getElementsByClassName('collection-gallery')[0].removeChild(items[ni]);
		}
	}

	for (var nj = 0; nj < prices.length; nj++) {
		document.getElementsByClassName('collection-gallery')[0].appendChild(prices[nj].el);
	}
}