X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Froot-helpers%2Futils.ts;h=06591b95e25414187cd72f76a4fead42d1bac223;hb=72aa835e170f5dfa5fb7b78d50a5d097ea194ef6;hp=acfb565a3635af92122cddb89e68374358eaeb19;hpb=4504f09f6e85f09b0489debb547a17209d7176ea;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/root-helpers/utils.ts b/client/src/root-helpers/utils.ts index acfb565a3..06591b95e 100644 --- a/client/src/root-helpers/utils.ts +++ b/client/src/root-helpers/utils.ts @@ -1,3 +1,5 @@ +import { environment } from '../environments/environment' + function objectToUrlEncoded (obj: any) { const str: string[] = [] for (const key of Object.keys(obj)) { @@ -7,6 +9,62 @@ function objectToUrlEncoded (obj: any) { return str.join('&') } +function copyToClipboard (text: string) { + const el = document.createElement('textarea') + el.value = text + el.setAttribute('readonly', '') + el.style.position = 'absolute' + el.style.left = '-9999px' + document.body.appendChild(el) + el.select() + document.execCommand('copy') + document.body.removeChild(el) +} + +// Thanks: https://github.com/uupaa/dynamic-import-polyfill +function importModule (path: string) { + return new Promise((resolve, reject) => { + const vector = '$importModule$' + Math.random().toString(32).slice(2) + const script = document.createElement('script') + + const destructor = () => { + delete window[ vector ] + script.onerror = null + script.onload = null + script.remove() + URL.revokeObjectURL(script.src) + script.src = '' + } + + script.defer = true + script.type = 'module' + + script.onerror = () => { + reject(new Error(`Failed to import: ${path}`)) + destructor() + } + script.onload = () => { + resolve(window[ vector ]) + destructor() + } + const absURL = (environment.apiUrl || window.location.origin) + path + const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module + const blob = new Blob([ loader ], { type: 'text/javascript' }) + script.src = URL.createObjectURL(blob) + + document.head.appendChild(script) + }) +} + +function wait (ms: number) { + return new Promise(res => { + setTimeout(() => res(), ms) + }) +} + export { - objectToUrlEncoded + copyToClipboard, + importModule, + objectToUrlEncoded, + wait }