JS Helper UI (TamperMonkey Adapted)

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

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greatest.deepsurf.us/scripts/575419/1808366/JS%20Helper%20UI%20%20%28TamperMonkey%20Adapted%29.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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