Steam cart sorter

sort the Steam cart by price

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name                Steam cart sorter
// @name:ZH-CN          Steam 购物车自动排序
// @namespace           http://tampermonkey.net/
// @version             0.8
// @description         sort the Steam cart by price
// @description:ZH-CN   对 Steam 购物车中的物品按照价格排序
// @author              SLAPaper
// @match               http://store.steampowered.com/cart/*
// @match               https://store.steampowered.com/cart/*
// @grant               none
// @license             MIT
// ==/UserScript==

(function() {
    'use strict';

    function main() {
        function sorter(isAsc) {
            let cart_item_list = document.querySelector('.cart_item_list');
            let old_items = Array.from(cart_item_list.children);

            let indicies = new Array(old_items.length);
            for (let i = 0; i < indicies.length; ++i) {
                indicies[i] = i;
            }

            indicies.sort((a, b) => {
                let anode = old_items[a].querySelector('.price:last-of-type');
                let bnode = old_items[b].querySelector('.price:last-of-type');

                if (!anode) {
                    return -1;
                }
                if (!bnode) {
                    return 1;
                }

                let aval = parseInt(anode.textContent.slice(2));
                let bval = parseInt(bnode.textContent.slice(2));

                if (aval - bval == 0) {
                    return a - b;
                }

                if (isAsc) {
                    return aval - bval;
                }
                else {
                    return bval - aval;
                }
            });

            for (let i = 0; i < old_items.length; ++i) {
                cart_item_list.appendChild(old_items[indicies[i]]);
            }
        }

        let sort_btn = document.createElement('div');
        sort_btn.className = 'SortCart';
        sort_btn.innerHTML = `
<button class="SortCartAsc">Sort Cart Ascend</button>
<button class="SortCartDesc">Sort Cart Descent</button>
`;

        document.querySelector('.rightcol').insertBefore(sort_btn, document.querySelector('.rightcol cart_recommendations_ctn'));

        document.querySelector('.SortCartAsc').addEventListener('click', () => {sorter(true);});
        document.querySelector('.SortCartDesc').addEventListener('click', () => {sorter(false);});
    }

    window.addEventListener('load', main);
})();