UtilLibrary

Util function library

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greatest.deepsurf.us/scripts/405927/1817579/UtilLibrary.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// ==UserLibrary==
// @name         UtilLibrary
// @namespace    [email protected]
// @version      0.8.0
// @description  Util function library
// @author       [email protected]
// @license      MIT
// @exclude      *
// @match        *://*/*
// @grant        GM_openInTab
// ==/UserLibrary==
// ==/UserScript==


//
// NOTE: To make this work in TamperMonkey, also install:
// https://greatest.deepsurf.us/en/scripts/433051-trusted-types-helper
//

// LIBRARY: START
const Util = (() => {
const exports = {};


exports.LOGGING_ID = "";

exports.LOGGING_LEVELS = [
    "DEBUG",
    "INFO",
    "NOTE",
    "WARN",
    "ERROR"
];

exports.MINIMUM_LOGGING_LEVEL = exports.LOGGING_LEVELS.findIndex(
    (element) => (element == "NOTE")
);

exports.SetMinimumLoggingLevel = (minimumLevel) => {
    exports.MINIMUM_LOGGING_LEVEL = exports.LOGGING_LEVELS.findIndex(
        (element) => (element == minimumLevel)
    );
};

exports.LOG_MESSAGES_WITHOUT_LOGGING_LEVEL = true;


///
/// Log a message.
///
exports.Log = (...args) => {
    const loggingLevelIndex = exports.LOGGING_LEVELS.findIndex(
        (element) => (element == args[0])
    );
    
    if (loggingLevelIndex != -1) {
        if (loggingLevelIndex < exports.MINIMUM_LOGGING_LEVEL) {
            return;
        }
        
        //args = args.slice(1);
    }
    else if (!exports.LOG_MESSAGES_WITHOUT_LOGGING_LEVEL) {
        // Don't log messages without a logging level
        return;
    }
    
    console.log.apply(
        null,
        [
            (new Date()).toISOString()
            + " " + exports.LOGGING_ID + ":\n"
        ].concat(args)
    );
};


///
/// Log a message (warn).
///
exports.Warn = (...args) => {
    const loggingLevelIndex = exports.LOGGING_LEVELS.findIndex(
        (element) => (element == args[0])
    );
    
    if (loggingLevelIndex != -1) {
        if (loggingLevelIndex < exports.MINIMUM_LOGGING_LEVEL) {
            return;
        }
        
        //args = args.slice(1);
    }
    else if (!exports.LOG_MESSAGES_WITHOUT_LOGGING_LEVEL) {
        // Don't log messages without a logging level
        return;
    }
    
    console.warn.apply(
        null,
        [
            (new Date()).toISOString()
            + " " + exports.LOGGING_ID + ":\n"
        ].concat(args)
    );
};


///
/// Log a message (error).
///
exports.Error = (...args) => {
    const loggingLevelIndex = exports.LOGGING_LEVELS.findIndex(
        (element) => (element == args[0])
    );
    
    if (loggingLevelIndex != -1) {
        if (loggingLevelIndex < exports.MINIMUM_LOGGING_LEVEL) {
            return;
        }
        
        //args = args.slice(1);
    }
    else if (!exports.LOG_MESSAGES_WITHOUT_LOGGING_LEVEL) {
        // Don't log messages without a logging level
        return;
    }
    
    console.error.apply(
        null,
        [
            (new Date()).toISOString()
            + " " + exports.LOGGING_ID + ":\n"
        ].concat(args)
    );
};


///
/// Create a click event.
///
/// NOTE: initEvent() is deprecated, so this will stop working at some point.
///
exports.CreateClickEvent = () => {
    const clickEvent = document.createEvent("Events");
    clickEvent.initEvent("click", true, false);
    
    return clickEvent;
};


///
/// Create a CSS element.
///
exports.CreateCssElement = (css) => {
    exports.Log("CreateCssElement()");
    
    const style = document.createElement("style");
    style.classList.add("custom-css");
    style.innerHTML = css;
    const body = document.getElementsByTagName("body")[0];
    if (body != null) {
        body.append(style);
    }
};


///
/// Get the value of an attibute, or return a default value.
///
exports.GetAttributeValue = (elem, attributeName, defaultValue = null) => {
    const attribute = elem.attributes[attributeName];
    
    if (attribute == null) {
        return defaultValue;
    }
    
    return attribute.value;
};


///
/// Recurse parent elements until one with the given class is found.
///
exports.GetParentWithClass = (element, className) => {
    while (element != null) {
        if (element.classList.contains(className)) {
            return element;
        }
        
        element = element.parentElement;
    }
    
    return null;
};


///
/// Listen for mouse movements and save mouse position.
///
exports.mousePosition = {
    "x": 0,
    "y": 0
};
document.addEventListener(
    "mousemove",
    (event) => {
        exports.mousePosition.x = event.clientX;
        exports.mousePosition.y = event.clientY;
    },
    true
);


///
/// Get the element currently under the mouse.
///
exports.GetElementUnderMouse = () => {
    const hoverElement = document.elementFromPoint(
        Util.mousePosition.x,
        Util.mousePosition.y
    );
    
    return hoverElement;
};


///
/// Get the element currently under the mouse.
///
exports.InIframe = () => {
    try {
        return (window.self !== window.top);
    }
    catch (e) {
        return true;
    }
};


exports.URL_OPEN_LIMIT = 1000;
let _urlsOpened = 0;

///
/// Open a window with a given URL and delay (timeout).
///
exports.OpenUrl = (url, openInBackground) => {
    if (!openInBackground) {
        openInBackground = false;
    }
    
    exports.Log(_urlsOpened, exports.URL_OPEN_LIMIT);
    if (_urlsOpened >= exports.URL_OPEN_LIMIT) {
        return false;
    }
    
    exports.Log("OpenUrl(" + url + ")");
    ++_urlsOpened;
    /*/
    setTimeout(
        function () {
            window.open(url);
        },
        10 * _urlsOpened
    );
    /*/
    GM_openInTab(url, openInBackground);
    //*/
    
    return true;
};


///
/// Reset the opened URL counter.
///
exports.ResetOpenedUrlCounter = () => {
    _urlsOpened = 0;
};


///
/// Parse float from a string (any numeric value from a string).
///
exports.ParseFloatFromString = (string) => {
    const regex = /[+-]?\d+([.,]\d+)?/g;
    
    const matches = string.match(regex);
    
    if (matches == null) {
        return 0.0;
    }
    
    const floats = matches.map(
        function(v) {
            return parseFloat(v.replace(",", "."));
        }
    );
    
    return ((floats.length != 0) ? floats[0] : 0.0);
};


///
/// Query a selector until it isn't null.
///
exports.WaitForSelector = (selector, maxWaitTime) => {
    let querySelector = null;
    querySelector = (resolve, maxWait, startTime) => {
        startTime = startTime || (new Date()).getMilliseconds();
        const currentTime = (new Date()).getMilliseconds();
        
        //exports.Log("WaitForSelector.querySelector()", startTime, startTime + maxWait, currentTime);
        
        if (currentTime >= (startTime + maxWait)) {
            resolve(null);
            
            return;
        }
        
        const element = document.querySelector(selector);
        
        if (element == null) {
            // Element not found, try again next frame
            setTimeout(
                () => querySelector(resolve, maxWait, startTime),
                0
            );
            
            return;
        }
        
        // Element found, resolve
        resolve(element);
    };
    
    maxWaitTime = maxWaitTime || 500;
    
    return new Promise(
        (resolve) => querySelector(resolve, maxWaitTime)
    );
};


// LIBRARY: END
return exports;
})();