mirror of
https://github.com/Buriburizaem0n/nezha-dash-v1.git
synced 2026-05-06 13:58:43 +00:00
feat: refactor custom code
This commit is contained in:
@@ -4,7 +4,7 @@ import { Skeleton } from "@/components/ui/skeleton"
|
||||
import { fetchLoginUser, fetchSetting } from "@/lib/nezha-api"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { DateTime } from "luxon"
|
||||
import { useCallback, useEffect, useRef, useState } from "react"
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
|
||||
@@ -23,50 +23,10 @@ function Header() {
|
||||
|
||||
const siteName = settingData?.data?.site_name
|
||||
|
||||
const InjectContext = useCallback((content: string) => {
|
||||
const tempDiv = document.createElement("div")
|
||||
tempDiv.innerHTML = content
|
||||
|
||||
const handlers: { [key: string]: (element: HTMLElement) => void } = {
|
||||
SCRIPT: (element) => {
|
||||
const script = document.createElement("script")
|
||||
if ((element as HTMLScriptElement).src) {
|
||||
script.src = (element as HTMLScriptElement).src
|
||||
} else {
|
||||
script.textContent = element.textContent
|
||||
}
|
||||
document.body.appendChild(script)
|
||||
},
|
||||
STYLE: (element) => {
|
||||
const style = document.createElement("style")
|
||||
style.textContent = element.textContent
|
||||
document.head.appendChild(style)
|
||||
},
|
||||
DEFAULT: (element) => {
|
||||
document.body.appendChild(element)
|
||||
},
|
||||
}
|
||||
|
||||
Array.from(tempDiv.childNodes).forEach((node) => {
|
||||
if (node.nodeType === Node.ELEMENT_NODE) {
|
||||
const element = node as HTMLElement
|
||||
;(handlers[element.tagName] || handlers.DEFAULT)(element)
|
||||
} else if (node.nodeType === Node.TEXT_NODE) {
|
||||
document.body.appendChild(document.createTextNode(node.textContent || ""))
|
||||
}
|
||||
})
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
document.title = siteName || "NEZHA"
|
||||
}, [siteName])
|
||||
|
||||
useEffect(() => {
|
||||
if (settingData?.data?.custom_code) {
|
||||
InjectContext(settingData?.data?.custom_code)
|
||||
}
|
||||
}, [settingData?.data?.custom_code])
|
||||
|
||||
return (
|
||||
<div className="mx-auto w-full max-w-5xl">
|
||||
<section className="flex items-center justify-between">
|
||||
|
||||
Reference in New Issue
Block a user