BasicLib

This script contains some simple basic-functions.

当前为 2026-01-02 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/547732/1725630/BasicLib.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           BasicLib
// @description    This script contains some simple basic-functions.
// @namespace      https://greasyfork.org/users/788550
// @version        1.0.4
// @author         Cyrano68
// @license        MIT
// @grant          none
// @run-at         document-start
// ==/UserScript==

// This is a IIFE (Immediately Invoked Function Expression).
(function()
{
    "use strict";

    const myVersion = "1.0.4";  // It must be the same value indicated in @version.
    consoleLog(`CY==> BasicLib: HELLO! Loading script (version: ${myVersion})...`);

    function getZeroFilledMillisecs(dateNow)
    {
        const millisecs = dateNow.getMilliseconds();
        return ("00" + millisecs).slice(-3);
    }

    function consoleLog(text, showLog = true)
    {
        if (showLog)
        {
            const dateNow = new Date();
            //const now = dateNow.toISOString();
            const now = dateNow.toLocaleString() + "." + getZeroFilledMillisecs(dateNow);
            console.log(`${now} ${text}`);
        }
    }

    function getMathRandomInteger(val1, val2)
    {
        let min = 0;
        let max = 100;

        const val1IsValid = ((val1 !== undefined) && (val1 !== undefined));
        const val2IsValid = ((val2 !== undefined) && (val2 !== undefined));
        consoleLog(`CY==> BasicLib: getMathRandomInteger - val1IsValid=${val1IsValid}, val2IsValid=${val2IsValid}`);

        if (val1IsValid || val2IsValid)
        {
            if (val1IsValid && val2IsValid)
            {
                min = val1;
                max = val2;
            }
            else
            {
                min = 0;
                max = (val1IsValid ? val1 : val2);
            }
        }

        if (max < min)
        {
            const tmp = min;
            min = max;
            max = tmp;
        }

        consoleLog(`CY==> BasicLib: getMathRandomInteger - min=${min}, max=${max}`);
        return Math.floor(Math.random() * (max - min)) + min;
    }

    function generateID()
    {
        // NOTE-1: The function "toString(36)" converts the number to a base-36 string (i.e. a string containing digits 0-9 and letters a-z).
        // NOTE-2: THe function "substring(2)" is used to remove the "0." from the start of the random number string.
        const ID = Date.now().toString(36) + "_" + Math.random().toString(36).substring(2);
        consoleLog(`CY==> BasicLib: generateID - ID='${ID}'`);
        return ID;
    }

    function setInterval2(callback, interval_ms, execCallbackNow)
    {
        // I defined a new "setInterval" function because I want to call the "setInterval" function and then
        // (if required) call immediately the callback function, instead of wait for the first timeout.
        //

        // Call the "setInterval" for the periodic timer.
        consoleLog(`CY==> BasicLib: setInterval2 - STARTING TIMER - interval_ms=${interval_ms}`);
        const timerId = setInterval(callback, interval_ms);
        consoleLog(`CY==> BasicLib: setInterval2 - TIMER STARTED - timerId=${timerId}`);

        if (execCallbackNow)
        {
            // Call immediately the callback function.
            callback(timerId);
        }

        return timerId;
    }

    function textMatchesArray(text, array, index)
    {
        // This function returns true if there is an element in the array that matches with "text".
        //
        // IMPORTANT: The input "index" must be an object with a field named "value". For example:
        //     let index = {value: -1};
        // At the end the "index.value" will contain the index of the element of the array that matched with "text" (or -1 if there is no match).
        //
        for (let i = 0; i < array.length; ++i)
        {
            if (text.startsWith(array[i]))
            {
                index.value = i;
                return true;
            }
        }

        index.value = -1;
        return false;
    }

    function getVersion()
    {
        return myVersion;
    }

    // Expose the public interface by returning an object.
    window.BasicLib =
    {
        consoleLog:           consoleLog,
        getMathRandomInteger: getMathRandomInteger,
        generateID:           generateID,
        setInterval2:         setInterval2,
        textMatchesArray:     textMatchesArray,
        getVersion:           getVersion
    };

    consoleLog("CY==> BasicLib: Script loaded");
})();