Sort Steam Achievement

Makes Steam achievements sorted by unlock date.

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         Sort Steam Achievement
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Makes Steam achievements sorted by unlock date.
// @author       Makazeu
// @match        *://steamcommunity.com/id/*/stats/*
// @grant        none
// @require      https://cdn.bootcss.com/moment.js/2.18.1/moment.min.js
// ==/UserScript==

/* jshint esversion: 6 */

(function() {
    'use strict';

    let comparator = (a, b) => {
        let a_datetime = datetimeParser(
            a.children[1].children[0].children[0].innerText.trim());
        let b_datetime = datetimeParser(
            b.children[1].children[0].children[0].innerText.trim());
        return a_datetime.isBefore(b_datetime) ? -1 : 1;
    };

    let datetimeParser =  datetime => {
        datetime = datetime.substring('解锁日期 '.length);
    
        datetime = datetime.replace('上午', ' AM ');
        datetime = datetime.replace('下午', ' PM ');
    
        if (datetime.includes('年')) {
            return moment(datetime, 'YYYY年MM月DD日 A HH:mm');
        } else {
            return moment(datetime, 'MM月DD日 A HH:mm');
        }
    };

    let achieveFilter = (key, value) => {
        if (value.innerHTML === '') return false; 
        if (value.children[1].children[0].children.length < 3) return false;
        if (!value.children[1].children[0].children[0]
            .innerText.includes('解锁')) return false;
        return true;
    };

    let achieveSet = jQuery('#personalAchieve');
    let achieveRows = achieveSet.children().filter(achieveFilter);
    let arr = Array.from(achieveRows);
    
    arr.sort(comparator);

    for (let i = 0; i < achieveRows.length; i++) {
        let element = achieveRows[i];
        element.remove();
    }

    arr.forEach( achieve => {
        //console.log(achieve.children[1].children[0].children[1].innerText);
        achieveSet.prepend(achieve);
    });
    
})();