Steam cart sorter

sort the Steam cart by price

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