diff --git a/.gitignore b/.gitignore index 0b2856cc..2d456bc7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ upload *.exe *.db build -*.db-journal \ No newline at end of file +*.db-journal +logs diff --git a/common/constants.go b/common/constants.go index 4b9df311..8ed54348 100644 --- a/common/constants.go +++ b/common/constants.go @@ -14,6 +14,7 @@ var Version = "v0.0.0" // this hard coding will be replaced automatic var SystemName = "One API" var ServerAddress = "http://localhost:3000" var Footer = "" +var HeaderScript = "" var Logo = "" var TopUpLink = "" var ChatLink = "" diff --git a/controller/misc.go b/controller/misc.go index 2bcbb41f..e62bddfc 100644 --- a/controller/misc.go +++ b/controller/misc.go @@ -24,6 +24,7 @@ func GetStatus(c *gin.Context) { "system_name": common.SystemName, "logo": common.Logo, "footer_html": common.Footer, + "header_script": common.HeaderScript, "wechat_qrcode": common.WeChatAccountQRCodeImageURL, "wechat_login": common.WeChatAuthEnabled, "server_address": common.ServerAddress, diff --git a/i18n/en.json b/i18n/en.json index a9402419..b8d637c0 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -246,6 +246,9 @@ "页脚": "Footer", "在此输入新的页脚,留空则使用默认页脚,支持 HTML 代码": "Enter the new footer here, leave blank to use the default footer, supports HTML code.", "设置页脚": "Set Footer", + "Header自定义脚本": "Custom Header Script", + "在此输入新的header自定义脚本,支持 第三方 HTML/script 代码": "Enter the new header custom script here, supports third-party HTML/script code.", + "设置Header自定义脚本": "Set Custom Header Script", "新版本": "New Version", "关闭": "Close", "密码已重置并已复制到剪贴板": "Password has been reset and copied to clipboard", diff --git a/model/option.go b/model/option.go index 4ef4d260..25bef01d 100644 --- a/model/option.go +++ b/model/option.go @@ -50,6 +50,7 @@ func InitOptionMap() { common.OptionMap["About"] = "" common.OptionMap["HomePageContent"] = "" common.OptionMap["Footer"] = common.Footer + common.OptionMap["HeaderScript"] = common.HeaderScript common.OptionMap["SystemName"] = common.SystemName common.OptionMap["Logo"] = common.Logo common.OptionMap["ServerAddress"] = "" @@ -179,6 +180,8 @@ func updateOptionMap(key string, value string) (err error) { common.GitHubClientSecret = value case "Footer": common.Footer = value + case "HeaderScript": + common.HeaderScript = value case "SystemName": common.SystemName = value case "Logo": diff --git a/web/src/App.js b/web/src/App.js index c967ce2c..df7276df 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -24,6 +24,7 @@ import EditRedemption from './pages/Redemption/EditRedemption'; import TopUp from './pages/TopUp'; import Log from './pages/Log'; import Chat from './pages/Chat'; +import useHeaderScript from './hooks/useHeaderScript'; const Home = lazy(() => import('./pages/Home')); const About = lazy(() => import('./pages/About')); @@ -48,6 +49,7 @@ function App() { localStorage.setItem('system_name', data.system_name); localStorage.setItem('logo', data.logo); localStorage.setItem('footer_html', data.footer_html); + localStorage.setItem('header_script', data.header_script || ''); localStorage.setItem('quota_per_unit', data.quota_per_unit); localStorage.setItem('display_in_currency', data.display_in_currency); if (data.chat_link) { @@ -85,6 +87,8 @@ function App() { } }, []); + useHeaderScript(); + return ( { let [inputs, setInputs] = useState({ Footer: '', + HeaderScript: '', Notice: '', About: '', SystemName: '', @@ -66,6 +67,10 @@ const OtherSetting = () => { await updateOption('Footer', inputs.Footer); }; + const submitHeaderScript = async () => { + await updateOption('HeaderScript', inputs.HeaderScript); + }; + const submitSystemName = async () => { await updateOption('SystemName', inputs.SystemName); }; @@ -165,6 +170,17 @@ const OtherSetting = () => { /> 保存关于 + + + + 设置Header自定义脚本 移除 One API 的版权标识必须首先获得授权,项目维护需要花费大量精力,如果本项目对你有意义,请主动支持本项目。 { SMTPFrom: '', SMTPToken: '', ServerAddress: '', - Footer: '', WeChatAuthEnabled: '', WeChatServerAddress: '', WeChatServerToken: '', diff --git a/web/src/helpers/utils.js b/web/src/helpers/utils.js index 3871a43e..1db595e1 100644 --- a/web/src/helpers/utils.js +++ b/web/src/helpers/utils.js @@ -36,6 +36,10 @@ export function getFooterHTML() { return localStorage.getItem('footer_html'); } +export function getHeaderScript() { + return localStorage.getItem('header_script'); +} + export async function copy(text) { let okay = true; try { diff --git a/web/src/hooks/useHeaderScript.js b/web/src/hooks/useHeaderScript.js new file mode 100644 index 00000000..b8e4351a --- /dev/null +++ b/web/src/hooks/useHeaderScript.js @@ -0,0 +1,24 @@ +// 从localStorage中获取 headerScript +// 用useEffect将headerScript插入到页面中 + +import { useEffect, useRef } from 'react'; +import { getHeaderScript } from '../helpers'; + +const useHeaderScript = () => { + const oldScript = useRef(''); + const headerScript = getHeaderScript(); + + useEffect(() => { + if (!oldScript.current || oldScript.current !== headerScript) { + if (headerScript) { + // Insert the code into the header + document.head.innerHTML += headerScript; + oldScript.current = headerScript; + } + } + }, [headerScript]); + + return headerScript; +}; + +export default useHeaderScript;