admin/custom: "script" section

Massimo Melina committed Nov 15, 2024 at 19:03 UTC 9cf21b113b0b9fc83e26ab4a5902025476340fa0
2 files changed +16 -1
frontend/src/App.ts
+15
@@ -20,6 +20,7 @@ function App() {
20 return h('h1', { style: { textAlign: 'center'} }, messageOnly)
21 if (!ready)
22 return h(Spinner, { style: { margin: 'auto' } })
23 + installScript()
24 return h(I18Nprovider, {},
25 h(BrowserRouter, {},
26 h(NavigationExtractor, {},
@@ -34,6 +35,20 @@ function App() {
35 )
36 }
37
38 +let scriptAdded = false
39 +function installScript() {
40 + if (scriptAdded) return
41 + scriptAdded = true
42 + const s = getHFS().customHtml?.script // we don't need frontend-event-generated code, i guess
43 + if (!s) return
44 + // it's important that this rendered only once, so we don't even render it, but manipulate dom
45 + const el = document.createElement('script')
46 + el.type = 'text/javascript'
47 + el.text = s
48 + el.id = 'customHtmlScript'
49 + document.head.appendChild(el)
50 +}
51 +
52 function NavigationExtractor(props: any) {
53 const go = useNavigate() // expose navigate function for programmatic usage
54 getHFS().navigate = (uri: string) => go(getPrefixUrl() + enforceStarting('/', uri))
src/customHtml.ts
+1 -1
@@ -8,7 +8,7 @@ import _ from 'lodash'
8
9 const FILE = 'custom.html'
10
11 -export const customHtmlSections: ReadonlyArray<string> = ['style', 'beforeHeader', 'afterHeader', 'afterMenuBar', 'afterList',
11 +export const customHtmlSections: ReadonlyArray<string> = ['style', 'script', 'beforeHeader', 'afterHeader', 'afterMenuBar', 'afterList',
12 'footer', 'top', 'bottom', 'afterEntryName', 'beforeLogin', 'unauthorized', 'htmlHead', 'userPanelAfterInfo']
13
14 export const customHtml = watchLoadCustomHtml()