Greasy Fork is available in English.

UtilLibrary

Util function library

Ekde 2020/06/23. Vidu La ĝisdata versio.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/405927/819500/UtilLibrary.js

// ==UserScript==
// @name         UtilLibrary
// @namespace    sami@kankaristo.fi
// @version      0.2.1
// @description  Util function library
// @author       sami@kankaristo.fi
// @match        *://*/*
// @grant        GM_openInTab
// ==/UserScript==


const Util = (() => {
// START LIBRARY


const LOGGING_LEVELS = [
    "INFO",
    "NOTE",
    "WARNING",
    "ERROR"
];

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

var LOG_MESSAGES_WITHOUT_LOGGING_LEVEL = true;


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


///
/// Query a selector until it isn't null.
///
const WaitForSelector = (selector, maxWaitTime) => {
    var querySelector = null;
    querySelector = (resolve, maxWait, startTime) => {
        startTime = startTime || (new Date()).getMilliseconds();
        var currentTime = (new Date()).getMilliseconds();
        
        Log(startTime, startTime + maxWait, currentTime);
        
        if (currentTime >= (startTime + maxWait)) {
            resolve(null);
            
            return;
        }
        
        var 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)
    );
};


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

// END LIBRARY
})();