Набір допоміжних UI-функцій для юзерскриптів (копіювання, завантаження файлів, керування меню, шортенери)
此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.greatest.deepsurf.us/scripts/575419/1808366/JS%20Helper%20UI%20%20%28TamperMonkey%20Adapted%29.js
/* 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
}