// ==UserScript==
// @name Steam Custom Layout, Sorting and Filtering
// @namespace http://null.frisch-live.de/
// @version 0.24
// @description try to take over the world!
// @author frisch
// @match http://store.steampowered.com/search/*
// @grant none
// @downloadURL none
// ==/UserScript==
console.log("Initializing Steam Custom Layout and Sorting...");
var jq = document.fExt.jq;
var loading = 0;
// Custom Elements
var sortContainer = jq("
Sort by:
");
sortContainer.appendTo("div.searchbar");
var srtPrice = jq("Price");
srtPrice.data("sort-type","Price");
srtPrice.appendTo(sortContainer);
var srtDiscount = jq("Discount");
srtDiscount.attr("active","true");
srtDiscount.data("sort-type","Discount");
srtDiscount.addClass("ASC");
srtDiscount.appendTo(sortContainer);
var srtDate = jq("Release Date");
srtDate.data("sort-type","Date");
srtDate.appendTo(sortContainer);
var filterContainer = jq("");
filterContainer.insertBefore(sortContainer);
jq(" €").appendTo(filterContainer);
jq(" €").appendTo(filterContainer);
jq(" %").appendTo(filterContainer);
jq('').appendTo(filterContainer);
// Styles
document.fExt.createStyle("#customSortContainer { display: block; width: 90%; padding: 4px; text-align: center; font-size: larger; }");
document.fExt.createStyle("#customSortContainer a { margin: 10px; }");
document.fExt.createStyle(".search_name { width: 58% !important; }");
document.fExt.createStyle(".search_capsule { width: 40% !important; }");
document.fExt.createStyle(".search_result_row { width: 267px !important; float: left !important; border: 2px groove rgba(28, 73, 101, 0.5); margin: 2px; }");
document.fExt.createStyle(".search_result_row:hover { border: 2px groove rgba(28, 73, 101, 0.5); margin: 2px; }");
document.fExt.createStyle("#search_result_container { width: 100%; max-width: 100% !important; }");
document.fExt.createStyle(".responsive_search_name_combined { width: 100%; height: 45px; }");
document.fExt.createStyle(".search_price_discount_combined { float: right; margin-right: 10px; width: 59%; }");
document.fExt.createStyle(".search_price { float: right; }");
document.fExt.createStyle(".leftcol.large { width: 1100px !important; }");
document.fExt.createStyle(".filterLabel { width: 110px; float: left; }");
document.fExt.createStyle(".filterInput { width: 100px; float: left; margin-right: 14px; text-align: right; }");
document.fExt.createStyle(".page_content { width: 1400px !important; } ");
document.fExt.createStyle(".search_discount.col span { position: absolute; top: 0; right: 20px; }");
document.fExt.createStyle(".search_review_summary { position: absolute; top: 40px; right: 20px; }");
document.fExt.createStyle(".search_price { position: absolute; top: 0; right: 80px;}");
document.fExt.createStyle("#customSortContainer a:after { content: ''; border-left: 5px solid transparent; border-right: 5px solid transparent; bottom: 12px; position: absolute; }");
document.fExt.createStyle("#customSortContainer a.ASC:after { border-top: 12px solid #fff; }");
document.fExt.createStyle("#customSortContainer a.DESC:after { border-bottom: 12px solid #fff; }");
// Functions
function sort(srtType){
var direction = "ASC";
var modifier = 1;
var jqSortElement;
if(srtType !== undefined){
jq("#customSortContainer a").each(function(){
var jqThis = jq(this);
var jqHtml = jqThis.html();
if(jqThis.data("sort-type") === srtType){
jqThis.attr("active","true");
direction = jqThis.hasClass("ASC") ? "ASC" : "DESC";
if(direction === "ASC") {
jqThis.removeClass("ASC");
direction = "DESC";
}
else {
jqThis.removeClass("DESC");
direction = "ASC";
}
jqSortElement = jqThis;
jqSortElement.addClass(direction);
}
else {
jqThis.removeClass("ASC");
jqThis.removeClass("DESC");
jqThis.removeAttr("active");
}
});
}
else {
jqSortElement = jq("#customSortContainer a[active=true]");
srtType = jqSortElement.data("sort-type");
if(!srtType)
return;
}
var container = jq("div#search_result_container");
var items = jq("a.search_result_row");
if(!container || items.length === 0)
return;
//items.detach();
if(direction === "DESC")
modifier = -1;
var uniqueLst = [];
var ind = items.length - 1;
while(ind >= 0){
var jqItem = jq(items[ind]);
var gameName = jqItem.find("span.title").text();
if(jq.inArray(gameName, uniqueLst) >= 0) {
jqItem.remove();
items.splice(ind);
}
else {
uniqueLst.push(gameName);
}
ind--;
}
items.sort(function(a,b){
var compValA, compValB;
switch(srtType){
case "Price":
var priceA = jq(a).find("div.search_price br");
var priceB = jq(b).find("div.search_price br");
if(priceA.length === 1)
compValA = parseFloat(priceA[0].nextSibling.textContent.trim().replace("€","").replace(",","."));
else
compValA = 0;
if(priceB.length === 1)
compValB = parseFloat(priceB[0].nextSibling.textContent.trim().replace("€","").replace(",","."));
else
compValB = 0;
break;
case "Release Date":
var dateA = jq(a).find("div.search_released");
var dateB = jq(b).find("div.search_released");
if (dateA.length === 1)
compValA = Date.parse(dateA[0].innerText);
else
compValA = 0;
if (dateB.length === 1)
compValB = Date.parse(dateB[0].innerText);
else
compValB = 0;
compValA = compValA;
compValB = compValB;
break;
case "Discount":
var discountA = jq(a).find("div.search_discount span");
var discountB = jq(b).find("div.search_discount span");
if(discountA.length === 1)
compValA = parseInt(discountA.text().replace("%",""));
else
compValA = 0;
if(discountB.length === 1)
compValB = parseInt(discountB.text().replace("%",""));
else
compValB = 0;
break;
}
if(compValA > compValB)
return 1 * modifier;
else if(compValA < compValB)
return -1 * modifier;
else
return 0;
});
items.appendTo(container);
}
function Initialize(){
jq("div.search_name").each(function(){
var jqThis = jq(this);
var jqParent = jqThis.parent().parent("a");
var jqAppImg = jqParent.find("div.search_capsule");
jqThis.detach();
jqThis.insertAfter(jqAppImg);
});
//jq("div.search_pagination").clone().insertBefore("div#search_result_container");
// http://store.steampowered.com/search/?sort_by=&sort_order=0&category1=998&special_categories=&specials=1&page=pageID
var lastPage = document.fExt.jq("div.search_pagination a:last");
if(lastPage.length === 1 && (lastPage.text() === '>' || lastPage.text() === '>'))
lastPage = lastPage.prev();
if(lastPage.length === 1){
var rx = new RegExp(/(page=[0-9]+)/g);
var matches = rx.exec(document.fExt.jq(location)[0].href);
var pages = parseInt(lastPage.text());
var currPage = 1;
if (matches)
currPage = parseInt(matches[0].replace("page=",""));
var pageLink = lastPage.attr('href').replace("page="+pages, "page=PAGENUMBER");
jq("#search_result_container").children().remove();
var totalSteps = 25; // Maximum 25 pages
if (pages < (currPage + 25))
totalSteps = pages - currPage;
document.fExt.message("Loading pages...");
var step = 0;
while(step < totalSteps){
step++;
var link = pageLink.replace("PAGENUMBER", step);
loadPageAsync(link);
}
}
}
function reInit(){
if(jq("div.search_pagination").length === 2)
setTimeout(function(){ reInit(); }, 500);
else {
jq("#search_result_container").unbind("DOMSubtreeModified");
Initialize();
}
}
function loadPageAsync(link){
loading++;
jq.ajax({
url: link,
type: 'GET',
error: function(data){
console.log("Error loading page #" + step + ": " + data);
},
complete: function(data){
loading--;
var result = jq(data.responseText).find("a.search_result_row");
if(result)
result.appendTo("div#search_result_container");
else
console.log("Error loading page #" + step + ": " + data);
if (loading === 0){
sort();
filter();
document.fExt.message("Loading completed.");
setTimeout(function(){ document.fExt.message(undefined); }, 1500);
}
},
});
}
function parsePrice(text){
return parseFloat(text.replace("€","").trim().replace(",","."));
}
function parseDiscount(text){
return parseInt(text.replace("%","").trim());
}
function filter() {
var priceMin = parsePrice(jq("#filterPriceMin").val());
var priceMax = parsePrice(jq("#filterPriceMax").val());
var discountMin = parseDiscount(jq("#filterDiscount").val()) * -1;
var items = jq("a.search_result_row");
items.each(function(index, item){
var jqItem = jq(item);
var itemPrice = parsePrice(jqItem.find(".search_price").clone().children().remove().end().text());
if(priceMin > 0)
if(itemPrice < priceMin){
jqItem.css("display","none");
return;
}
if(priceMax > 0)
if(itemPrice > priceMax){
jqItem.css("display","none");
return;
}
var itemDiscount = parseDiscount(jqItem.find(".search_discount span").text());
if(discountMin < 0)
if(itemDiscount > discountMin){
jqItem.css("display","none");
return;
}
jqItem.css("display","block");
});
}
// Events
jq("#srtPrice, #srtDiscount, #srtDate").click(function(e) {
e.preventDefault();
sort(jq(this).data("sort-type"));
return false;
});
jq(document).on('click', 'div.search_pagination_right a', function(e){
jq("#search_result_container").bind("DOMSubtreeModified", reInit());
});
jq("input.floatInput").keyup(function(){
var jqThis = jq(this);
var val = jqThis.val().replace(/[^0-9,.]/g,'');
if (val !== jqThis.val())
jqThis.val(val);
});
jq("input.intInput").keyup(function(){
var jqThis = jq(this);
var val = jqThis.val().replace(/[^0-9]/g,'');
if (val !== jqThis.val())
jqThis.val(val);
});
var filterChangeID = 0;
jq(".filterInput").keyup(function(){
filterChangeID++;
var myChangeID = filterChangeID;
setTimeout(function(){
if (filterChangeID === myChangeID) {
filterChangeID = 0;
filter();
}
}, 1000);
});
jq("#customRefresh").click(function(e){
e.preventDefault();
reInit();
return false;
});
// Init
Initialize();