ParseUtil

Utility methods for DOM and string parsing

Versione datata 04/11/2019. Vedi la nuova versione l'ultima versione.

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greasyfork.org/scripts/391979/746747/ParseUtil.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         ParseUtil
// @namespace    hoehleg.userscripts.private
// @version      0.3.1
// @description  Utility methods for DOM and string parsing
// @author       Gerrit Höhle
// @grant        GM_xmlhttpRequest
// ==/UserScript==

/* jshint esnext: true */
const ParseUtil = {
    getPageAsync: (url, onSuccess, onError = () => {}) => {
        return GM_xmlhttpRequest({
            method: 'GET',
            url: url,
            onload: (resp) => {
                resp.html = new DOMParser().parseFromString(resp.responseText || "", 'text/html');
                switch (resp.status) {
                    case 200:
                    case 304:
                        onSuccess(resp);
                        return;
                    default:
                        onError(resp);
                        return;
                }
            },
            onerror: onError
        });
    },

    parseInt: (str, maxIntPlaces = 1, fallbackValue = null) => {
        let regex = "\\d";
        if (maxIntPlaces > 1) {
            regex += "{1," + maxIntPlaces + "}";
        }
        return Util.parseIntWithRegex(str, new RegExp(regex), fallbackValue);
    },

    parseFloat: (str, maxIntPlaces = 1, maxDecPlaces = 0, fallbackValue = null) => {
        let regex = "\\d";
        if (maxIntPlaces > 1) {
            regex += "{1," + maxIntPlaces + "}";
        }
        if (maxDecPlaces > 0) {
            regex += "\\s?[,\.]?\\s?\\d{0," + maxDecPlaces + "}";
        }
        return Util.parseFloatWithRegex(str, new RegExp(regex), fallbackValue);
    },

    parseIntWithRegex: (str, regex, fallbackValue = null) => {
        const match = Util.parseStrWithRegex(str, regex);
        const value = parseInt(match);
        return Number.isNaN(value) ? fallbackValue : value;
    },

    parseFloatWithRegex: (str, regex, fallbackValue = null) => {
        const match = Util.parseStrWithRegex(str, regex, "").replace(",",".");
        const value = parseFloat(match);
        return Number.isNaN(value) ? fallbackValue : value;
    },

    parseStrWithRegex: (str, regex, fallbackValue = null) => {
        const match = regex.exec(str || "");
        return (match && match.length >= 1) ? match[0] : fallbackValue;
    }
};