ParseUtil

Utility methods for DOM and string parsing

目前為 2019-11-04 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/391979/746748/ParseUtil.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ParseUtil
// @namespace    hoehleg.userscripts.private
// @version      0.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;
    }
};