Greasy Fork is available in English.

QCStats

Base library to be used in scripts for stats.quake.com

Устаревшая версия на 02.09.2018. Перейти к последней версии.

Этот скрипт недоступен для установки пользователем. Он является библиотекой, которая подключается к другим скриптам мета-ключом // @require https://update.greasyfork.org/scripts/371849/625768/QCStats.js

// ==UserScript==
// @name         QCStats
// @namespace    https://github.com/aleab/
// @version      1.0.1
// @author       aleab
// @description  Base library to be used in scripts for stats.quake.com
// @icon         https://stats.quake.com/fav/favicon-32x32.png
// @icon64       https://stats.quake.com/fav/favicon-96x96.png
// @match        https://stats.quake.com
// @match        https://stats.quake.com/*
// @grant        none
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// ==/UserScript==

/* jshint esversion: 6 */


// VARIABLES & CONSTANTS

let $ = window.jQuery;
let listeners = {};

// ===================

(function(history){
    let pushState = history.pushState;
    history.pushState = function(state) {
        if (typeof history.onpushstate == "function") {
            history.onpushstate({state: state});
        }
        return pushState.apply(history, arguments);
    };
})(window.history);

$(document).ready(function() {
    let aleab = {
        qcstats: {
            addPageChangedListener: function(pageRegex, f) {
                if (!(pageRegex instanceof RegExp) || typeof f !== "function") {
                    return;
                }

                if(!listeners.hasOwnProperty(pageRegex)) {
                    listeners[pageRegex] = [];
                }
                listeners[pageRegex].push(f);
            }
        }
    };
    window.aleab = aleab;

    window.onpopstate = history.onpushstate = async function(e) {
        // let location.href update
        await sleep(250);

        $.each(listeners, (regex, functions) => {
            $.each(functions, (i, f) => f());
        });
    };
});

// ===================

// FUNCTIONS

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}