JS Helper UI (TamperMonkey Adapted)

Набір допоміжних UI-функцій для юзерскриптів (копіювання, завантаження файлів, керування меню, шортенери)

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.greatest.deepsurf.us/scripts/575419/1808366/JS%20Helper%20UI%20%20%28TamperMonkey%20Adapted%29.js

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

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

/* jshint esversion: 8 */

const jsHelperUi = (function() {
    const lib = {
        const: {
            corsProxyUrl: 'https://corsproxy.io/?url=',
            shorteners: {
                'clck.ru': 'https://clck.ru/--?url=',
                'is.gd': 'https://corsproxy.io/?url=' + encodeURIComponent('https://is.gd/create.php?format=simple&url='),
                'v.gd': 'https://corsproxy.io/?url=' + encodeURIComponent('https://v.gd/create.php?format=simple&url='),
            }
        }
    };

    /**
     * Копіювання тексту в буфер (з фолбеком для старих браузерів)
     */
    lib.copyTextToClipboard = async text => {
        try {
            if (window.isSecureContext && navigator.clipboard) {
                await navigator.clipboard.writeText(text);
            } else {
                const textarea = document.createElement('textarea');
                textarea.style.opacity = '0';
                textarea.style.position = 'absolute';
                textarea.value = text;
                document.body.appendChild(textarea);
                textarea.select();
                document.execCommand('copy');
                document.body.removeChild(textarea);
            }
        } catch (e) { console.error('2xGeMINI Lib Error:', e); }
    };

    /**
     * Завантаження даних як файлу
     */
    lib.downloadDataAsFile = (data, filename = 'download.txt', contentType = 'text/plain') => {
        const blob = new Blob(data, {type: contentType});
        const fileURL = URL.createObjectURL(blob);
        const downloadLink = document.createElement('a');
        downloadLink.href = fileURL;
        downloadLink.download = filename;
        document.body.appendChild(downloadLink);
        downloadLink.click();
        downloadLink.remove();
        URL.revokeObjectURL(fileURL);
    };

    /**
     * Секретні послідовності клавіш (Коди: https://toptal.com/developers/keycode)
     */
    lib.onKeyCodeSequence = (keyCodeSequence, callback, once = false) => {
        keyCodeSequence = keyCodeSequence.replace(/ /g, '');
        let inputKeySequence = [];
        function listener(keyboardEvent) {
            if (keyboardEvent.code === 'Escape') {
                inputKeySequence = [];
            } else {
                inputKeySequence.push(keyboardEvent.code);
                if (inputKeySequence.toString().includes(keyCodeSequence)) {
                    if (once) window.removeEventListener('keyup', listener);
                    inputKeySequence = [];
                    callback();
                }
            }
        }
        window.addEventListener('keyup', listener);
    };

    /**
     * Скорочення URL
     */
    lib.shortUrl = async (urlForShortening, shortenerUrl = 'clck.ru', corsProxy = false) => {
        let finalUrl = lib.const.shorteners[shortenerUrl] || shortenerUrl;
        if (corsProxy && !lib.const.shorteners[shortenerUrl]) {
            finalUrl = lib.const.corsProxyUrl + encodeURIComponent(finalUrl);
        }
        try {
            const response = await fetch(finalUrl + encodeURIComponent(urlForShortening));
            if (response.ok) return await response.text();
        } catch (e) { console.error('2xGeMINI Lib Error:', e); }
    };

    // Блокування контекстного меню
    lib.disableContextMenu = () => window.addEventListener('contextmenu', lib._cmL);
    lib.enableContextMenu = () => window.removeEventListener('contextmenu', lib._cmL);
    lib._cmL = e => e.preventDefault();

    return lib;
})();

// Експорт для різних середовищ
if (typeof module !== 'undefined' && module.exports) {
    module.exports = jsHelperUi; // Для Node.js/CommonJS
} else if (typeof define === 'function' && define.amd) {
    define(() => jsHelperUi); // Для AMD
} else {
    window.jsHelperUi = jsHelperUi; // Для браузера та Tampermonkey
}