JS Helper UI (TamperMonkey Adapted)

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

Questo script non dovrebbe essere installato direttamente. È una libreria per altri script da includere con la chiave // @require https://update.greatest.deepsurf.us/scripts/575419/1808366/JS%20Helper%20UI%20%20%28TamperMonkey%20Adapted%29.js

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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