51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { environment } from '../environments/environment'
|
|
|
|
function objectToUrlEncoded (obj: any) {
|
|
const str: string[] = []
|
|
for (const key of Object.keys(obj)) {
|
|
str.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
|
|
}
|
|
|
|
return str.join('&')
|
|
}
|
|
|
|
// 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)
|
|
})
|
|
}
|
|
|
|
export {
|
|
importModule,
|
|
objectToUrlEncoded
|
|
}
|