集成WebWhiz

This commit is contained in:
tangly1024.com
2023-08-03 15:15:41 +08:00
parent 5867cb6ce2
commit b498b2f175
5 changed files with 69 additions and 31 deletions

View File

@@ -1,34 +1,28 @@
'use client'
import BLOG from '@/blog.config'
import { isBrowser, loadExternalResource } from '@/lib/utils'
import { isBrowser } from '@/lib/utils'
/**
* 自定义引入外部JS 和 CSS
* 自定义外部 script
* 传入参数将转为 <script>标签。
* @returns
*/
const ExternalScript = () => {
if (isBrowser) {
// 静态导入本地自定义样式
loadExternalResource('/css/custom.css', 'css')
loadExternalResource('/js/custom.js', 'js')
// 自动添加图片阴影
if (BLOG.IMG_SHADOW) {
loadExternalResource('/css/img-shadow.css', 'css')
}
if (BLOG.CUSTOM_EXTERNAL_JS && BLOG.CUSTOM_EXTERNAL_JS.length > 0) {
for (const url of BLOG.CUSTOM_EXTERNAL_JS) {
loadExternalResource(url, 'js')
}
}
if (BLOG.CUSTOM_EXTERNAL_CSS && BLOG.CUSTOM_EXTERNAL_CSS.length > 0) {
for (const url of BLOG.CUSTOM_EXTERNAL_CSS) {
loadExternalResource(url, 'css')
}
}
const ExternalScript = (props) => {
const { src } = props
if (!isBrowser || !src) {
return null
}
const element = document.querySelector(`script[src="${src}"]`)
if (element) {
return null
}
const script = document.createElement('script')
Object.entries(props).forEach(([key, value]) => {
script.setAttribute(key, value)
})
document.head.appendChild(script)
console.log('加载外部脚本', props, script)
return null
}