aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/root-helpers
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/root-helpers')
-rw-r--r--client/src/root-helpers/plugins.ts81
-rw-r--r--client/src/root-helpers/utils.ts38
2 files changed, 119 insertions, 0 deletions
diff --git a/client/src/root-helpers/plugins.ts b/client/src/root-helpers/plugins.ts
new file mode 100644
index 000000000..011721761
--- /dev/null
+++ b/client/src/root-helpers/plugins.ts
@@ -0,0 +1,81 @@
1import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks'
2import { ClientHookName, ClientScript, RegisterClientHookOptions, ServerConfigPlugin, PluginType, clientHookObject } from '../../../shared/models'
3import { RegisterClientHelpers } from 'src/types/register-client-option.model'
4import { ClientScript as ClientScriptModule } from '../types/client-script.model'
5import { importModule } from './utils'
6
7interface HookStructValue extends RegisterClientHookOptions {
8 plugin: ServerConfigPlugin
9 clientScript: ClientScript
10}
11
12type Hooks = { [ name: string ]: HookStructValue[] }
13
14type PluginInfo = {
15 plugin: ServerConfigPlugin
16 clientScript: ClientScript
17 pluginType: PluginType
18 isTheme: boolean
19}
20
21async function runHook<T> (hooks: Hooks, hookName: ClientHookName, result?: T, params?: any) {
22 if (!hooks[hookName]) return result
23
24 const hookType = getHookType(hookName)
25
26 for (const hook of hooks[hookName]) {
27 console.log('Running hook %s of plugin %s.', hookName, hook.plugin.name)
28
29 result = await internalRunHook(hook.handler, hookType, result, params, err => {
30 console.error('Cannot run hook %s of script %s of plugin %s.', hookName, hook.clientScript.script, hook.plugin.name, err)
31 })
32 }
33
34 return result
35}
36
37function loadPlugin (hooks: Hooks, pluginInfo: PluginInfo, peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers) {
38 const { plugin, clientScript } = pluginInfo
39
40 const registerHook = (options: RegisterClientHookOptions) => {
41 if (clientHookObject[options.target] !== true) {
42 console.error('Unknown hook %s of plugin %s. Skipping.', options.target, plugin.name)
43 return
44 }
45
46 if (!hooks[options.target]) hooks[options.target] = []
47
48 hooks[options.target].push({
49 plugin,
50 clientScript,
51 target: options.target,
52 handler: options.handler,
53 priority: options.priority || 0
54 })
55 }
56
57 const peertubeHelpers = peertubeHelpersFactory(pluginInfo)
58
59 console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name)
60
61 return importModule(clientScript.script)
62 .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers }))
63 .then(() => sortHooksByPriority(hooks))
64 .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err))
65}
66
67export {
68 HookStructValue,
69 Hooks,
70 PluginInfo,
71 loadPlugin,
72 runHook
73}
74
75function sortHooksByPriority (hooks: Hooks) {
76 for (const hookName of Object.keys(hooks)) {
77 hooks[hookName].sort((a, b) => {
78 return b.priority - a.priority
79 })
80 }
81}
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}