1 import { environment } from '../environments/environment'
3 function objectToUrlEncoded (obj: any) {
4 const str: string[] = []
5 for (const key of Object.keys(obj)) {
6 str.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
12 function copyToClipboard (text: string) {
13 const el = document.createElement('textarea')
15 el.setAttribute('readonly', '')
16 el.style.position = 'absolute'
17 el.style.left = '-9999px'
18 document.body.appendChild(el)
20 document.execCommand('copy')
21 document.body.removeChild(el)
24 // Thanks: https://github.com/uupaa/dynamic-import-polyfill
25 function importModule (path: string) {
26 return new Promise((resolve, reject) => {
27 const vector = '$importModule$' + Math.random().toString(32).slice(2)
28 const script = document.createElement('script')
30 const destructor = () => {
31 delete window[ vector ]
35 URL.revokeObjectURL(script.src)
40 script.type = 'module'
42 script.onerror = () => {
43 reject(new Error(`Failed to import: ${path}`))
46 script.onload = () => {
47 resolve(window[ vector ])
50 const absURL = (environment.apiUrl || window.location.origin) + path
51 const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module
52 const blob = new Blob([ loader ], { type: 'text/javascript' })
53 script.src = URL.createObjectURL(blob)
55 document.head.appendChild(script)
59 function wait (ms: number) {
60 return new Promise(res => {
61 setTimeout(() => res(), ms)