aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/root-helpers/utils.ts')
-rw-r--r--client/src/root-helpers/utils.ts38
1 files changed, 38 insertions, 0 deletions
diff --git a/client/src/root-helpers/utils.ts b/client/src/root-helpers/utils.ts
index acfb565a3..6df151ad9 100644
--- a/client/src/root-helpers/utils.ts
+++ b/client/src/root-helpers/utils.ts
@@ -1,3 +1,5 @@
1import { environment } from '../environments/environment'
2
1function objectToUrlEncoded (obj: any) { 3function objectToUrlEncoded (obj: any) {
2 const str: string[] = [] 4 const str: string[] = []
3 for (const key of Object.keys(obj)) { 5 for (const key of Object.keys(obj)) {
@@ -7,6 +9,42 @@ function objectToUrlEncoded (obj: any) {
7 return str.join('&') 9 return str.join('&')
8} 10}
9 11
12// Thanks: https://github.com/uupaa/dynamic-import-polyfill
13function importModule (path: string) {
14 return new Promise((resolve, reject) => {
15 const vector = '$importModule$' + Math.random().toString(32).slice(2)
16 const script = document.createElement('script')
17
18 const destructor = () => {
19 delete window[ vector ]
20 script.onerror = null
21 script.onload = null
22 script.remove()
23 URL.revokeObjectURL(script.src)
24 script.src = ''
25 }
26
27 script.defer = true
28 script.type = 'module'
29
30 script.onerror = () => {
31 reject(new Error(`Failed to import: ${path}`))
32 destructor()
33 }
34 script.onload = () => {
35 resolve(window[ vector ])
36 destructor()
37 }
38 const absURL = (environment.apiUrl || window.location.origin) + path
39 const loader = `import * as m from "${absURL}"; window.${vector} = m;` // export Module
40 const blob = new Blob([ loader ], { type: 'text/javascript' })
41 script.src = URL.createObjectURL(blob)
42
43 document.head.appendChild(script)
44 })
45}
46
10export { 47export {
48 importModule,
11 objectToUrlEncoded 49 objectToUrlEncoded
12} 50}