dao-game / peerjs-browser-bug / files / src / utils.ts
Branch peerjs-browser-bug · default branch · all branches
utils.ts
| 1 | export function generateHash(text: string) { |
| 2 | var hash = 0; |
| 3 | if (text.length == 0) { |
| 4 | return hash; |
| 5 | } |
| 6 | for (let i = 0; i < text.length; i++) { |
| 7 | var charCode = text.charCodeAt(i); |
| 8 | hash = (hash << 7) - hash + charCode; |
| 9 | hash = hash & hash; |
| 10 | } |
| 11 | return hash; |
| 12 | } |
| 13 | |
| 14 | export async function delay(delayInMs: number) { |
| 15 | return new Promise((resolve) => { |
| 16 | setTimeout(() => { |
| 17 | resolve(2); |
| 18 | }, delayInMs); |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | export async function copyToClipboard(textToCopy): Promise<void> { |
| 23 | // navigator clipboard api needs a secure context (https) |
| 24 | if (navigator.clipboard && window.isSecureContext) { |
| 25 | // navigator clipboard api method' |
| 26 | return navigator.clipboard.writeText(textToCopy); |
| 27 | } else { |
| 28 | // text area method |
| 29 | let textArea = document.createElement("textarea"); |
| 30 | textArea.value = textToCopy; |
| 31 | // make the textarea out of viewport |
| 32 | textArea.style.position = "fixed"; |
| 33 | textArea.style.left = "-999999px"; |
| 34 | textArea.style.top = "-999999px"; |
| 35 | document.body.appendChild(textArea); |
| 36 | textArea.focus(); |
| 37 | textArea.select(); |
| 38 | return new Promise((res, rej) => { |
| 39 | // here the magic happens |
| 40 | document.execCommand('copy') ? res() : rej(); |
| 41 | textArea.remove(); |
| 42 | }); |
| 43 | } |
| 44 | } |