aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers/utils.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-08-20 11:46:25 +0200
committerChocobozzz <chocobozzz@cpy.re>2020-08-20 14:23:57 +0200
commitf95628636b6ccdf3eae2449ca718e075b072f678 (patch)
tree35d51980c87b7d6747bdff6e37bdfe37e3c989dc /client/src/root-helpers/utils.ts
parenta9f6802e7dac4f21599076bc1119bb6ff16961dc (diff)
downloadPeerTube-f95628636b6ccdf3eae2449ca718e075b072f678.tar.gz
PeerTube-f95628636b6ccdf3eae2449ca718e075b072f678.tar.zst
PeerTube-f95628636b6ccdf3eae2449ca718e075b072f678.zip
Support plugin hooks in embed
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}