one-api/web/src/hooks/useHeaderScript.js
Jungley Yeh 90fe5d7b23 feat: add HeaderScript option to render header script
Purpose: To load third-party statistical tracking code.
2023-08-13 14:31:39 +08:00

25 lines
632 B
JavaScript

// 从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;