plugins: HFS.loadScript

Massimo Melina committed Jun 8, 2024 at 15:35 UTC 4ec502654c078f6f32bcc3ee506d136a5aa9e30b
2 files changed +11 -8
dev-plugins.md
+10 -8
@@ -220,30 +220,31 @@ The HFS objects contains many properties:
220 - `logout`
221 - `prefixUrl: string` normally an empty string, it will be set in case a [reverse-proxy wants to mount HFS on a path](https://github.com/rejetto/hfs/wiki/Reverse-proxy).
222 - `state: StateObject` [object with many values in it](https://github.com/rejetto/hfs/blob/main/frontend/src/state.ts)
223 -- `watchState: (key: string, callback)=>function`
223 +- `watchState(key: string, callback): function`
224 - watch the `key` property of the state object above
225 - `callback(newValue)` will be called at each change
226 - use returned callback to stop watching
227 -- `useSnapState: () => StateObject` React hook version of the `state` object above
227 +- `useSnapState(): StateObject` React hook version of the `state` object above
228 - `React` whole React object, as for `require('react')` (JSX syntax is not supported here)
229 - `h` shortcut for React.createElement
230 - `t` [translator function](https://github.com/rejetto/hfs/blob/main/frontend/src/i18n.ts)
231 - `_` [lodash library](https://lodash.com/docs/)
232 -- `toast: (message: string | ReactElement, type: ToastType='info')`
232 +- `toast(message: string | ReactElement, type: ToastType='info')`
233 - show a brief message that doesn't steal focus
234 - `ToastType = 'error' | 'warning' | 'info' | 'success'`
235 - `dialogLib` this exposes all functions available in [dialog.ts](https://github.com/rejetto/hfs/blob/main/frontend/src/dialog.ts), for example alertDialog and newDialog. These are not documented yet, and subject to change without notification, but you can study the sources if you are interested in using them.
236 - `misc` many functions and constants available in [cross.ts](https://github.com/rejetto/hfs/blob/main/src/cross.ts). These are not documented, probably never will, and are subject to change without notifications, but you can study the sources if you are interested in using them.
237 -- `navigate: (uri: string) => void` use this if you have to change the page address without causing reload
238 -- `emit: (name: string, params?: object) => any[]` use this to emit a custom event. Prefix name with your plugin name to avoid conflicts.
237 +- `navigate(uri: string)` use this if you have to change the page address without causing reload
238 +- `emit(name: string, params?: object): any[]` use this to emit a custom event. Prefix name with your plugin name to avoid conflicts.
239 - `Icon: ReactComponent` Properties:
240 - `name: string` refer to file `icons.ts` for names, but you can also enter an emoji instead.
241 -- `useBatch: (worker, job) => any`
241 +- `useBatch(worker, job): any`
242 - `getNotifications(channel: string, cb: (eventName: string, data:any) => void)`
243 receive messages when the backend uses `notifyClient` on the same channel.
244 -- `html: (html: string) => ReactNode` convert html code to React
244 +- `html(html: string): ReactNode` convert html code to React
245 - `debounceAsync: function` like lodash.debounce, but also avoids async invocations to overlap.
246 - For details please refer to `src/debounceAsync.ts`.
246 + For details please refer to `src/debounceAsync.ts`.
247 +- `loadScript(uri: string): Promise` load a js file. If uri is relative, it is based on the plugin's public folder.
248
249 The following properties are accessible only immediately at top-level; don't call it later in a callback.
250 - `getPluginConfig()` returns object of all config keys that are declared frontend-accessible by this plugin.
@@ -596,6 +597,7 @@ If you want to override a text regardless of the language, use the special langu
597 - HFS.html
598 - HFS.useSnapState
599 - HFS.debounceAsync
600 + - HFS.loadScript
601 - middleware: ctx.stop()
602 - the old way of returning true is now deprecated
603 - exports.customHtml
shared/index.ts
+1
@@ -26,6 +26,7 @@ Object.assign(HFS, {
26 getPluginKey: () => getScriptAttr('plugin'),
27 getPluginPublic: () => getScriptAttr('src')?.match(/^.*\//)?.[0],
28 getPluginConfig: () => HFS.plugins[HFS.getPluginKey()] || {},
29 + loadScript: (uri: string) => loadScript(uri.includes('//') || uri.startsWith('/') ? uri : HFS.getPluginPublic() + uri),
30 cpuSpeedIndex,
31 })
32