JS Helper UI (TamperMonkey Adapted)

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

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greatest.deepsurf.us/scripts/575419/1808366/JS%20Helper%20UI%20%20%28TamperMonkey%20Adapted%29.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

/* 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
}