1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- export function sleep(ms) {
- return new Promise(resolve => setTimeout(resolve, ms));
- }
- export function stringToHex(str) {
- let result = '';
- for (let i = 0; i < str.length; i++) {
- result += str.charCodeAt(i).toString(16);
- }
- return result;
- }
- export function hexToString(str) {
- let result = '';
- for (let i = 0; i < str.length; i += 2) {
- result += String.fromCharCode(parseInt(str.substr(i, 2), 16));
- }
- return result;
- }
- export function formatDate(d, format) {
- if (!format)
- format = 'normal';
- switch (format) {
- case 'normal':
- return `${d.getDate().toString().padStart(2, '0')}.${(d.getMonth() + 1).toString().padStart(2, '0')}.${d.getFullYear()} ` +
- `${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`;
- }
-
- }
- export function fallbackCopyTextToClipboard(text) {
- let textArea = document.createElement('textarea');
- textArea.value = text;
- document.body.appendChild(textArea);
- textArea.focus();
- textArea.select();
- let result = false;
- try {
- result = document.execCommand('copy');
- } catch (e) {
- //
- }
- document.body.removeChild(textArea);
- return result;
- }
- export async function copyTextToClipboard(text) {
- if (!navigator.clipboard) {
- return fallbackCopyTextToClipboard(text);
- }
- let result = false;
- try {
- await navigator.clipboard.writeText(text);
- result = true;
- } catch (e) {
- //
- }
- return result;
- }
|