diff --git a/components/Busuanzi.js b/components/Busuanzi.js
new file mode 100644
index 00000000..a2456a75
--- /dev/null
+++ b/components/Busuanzi.js
@@ -0,0 +1,17 @@
+import busuanzi from '@/lib/busuanzi'
+import { useRouter } from 'next/router'
+import { useEffect } from 'react'
+
+export default function Busuanzi () {
+ const router = useRouter()
+ useEffect(() => {
+ const busuanziRouteChange = url => {
+ busuanzi.fetch()
+ }
+ router.events.on('routeChangeComplete', busuanziRouteChange)
+ return () => {
+ router.events.off('routeChangeComplete', busuanziRouteChange)
+ }
+ }, [router.events])
+ return null
+}
diff --git a/components/GoogleAdsense.js b/components/GoogleAdsense.js
new file mode 100644
index 00000000..dd9ec543
--- /dev/null
+++ b/components/GoogleAdsense.js
@@ -0,0 +1,29 @@
+import { useRouter } from 'next/router'
+import { useEffect } from 'react'
+
+export default function GoogleAdsense () {
+ const initGoogleAdsense = () => {
+ const ads = document.getElementsByClassName('adsbygoogle').length
+ const newAdsCount = ads
+ if (newAdsCount > 0) {
+ for (let i = 0; i <= newAdsCount; i++) {
+ try {
+ // eslint-disable-next-line no-undef
+ (adsbygoogle = window.adsbygoogle || []).push({})
+ } catch (e) {
+
+ }
+ }
+ }
+ }
+
+ const router = useRouter()
+ useEffect(() => {
+ initGoogleAdsense()
+ router.events.on('routeChangeComplete', initGoogleAdsense)
+ return () => {
+ router.events.off('routeChangeComplete', initGoogleAdsense)
+ }
+ }, [router.events])
+ return null
+}
diff --git a/components/Gtag.js b/components/Gtag.js
index a1d9c45c..0314b55f 100644
--- a/components/Gtag.js
+++ b/components/Gtag.js
@@ -5,12 +5,12 @@ import * as gtag from '@/lib/gtag'
const Gtag = () => {
const router = useRouter()
useEffect(() => {
- const handleRouteChange = url => {
+ const gtagRouteChange = url => {
gtag.pageview(url)
}
- router.events.on('routeChangeComplete', handleRouteChange)
+ router.events.on('routeChangeComplete', gtagRouteChange)
return () => {
- router.events.off('routeChangeComplete', handleRouteChange)
+ router.events.off('routeChangeComplete', gtagRouteChange)
}
}, [router.events])
return null
diff --git a/components/ThirdPartyScript.js b/components/ThirdPartyScript.js
index 1b81d5d3..37ae0f19 100644
--- a/components/ThirdPartyScript.js
+++ b/components/ThirdPartyScript.js
@@ -37,6 +37,13 @@ const ThirdPartyScript = () => {
/>
>)}
+ {/* 不蒜子
+ {BLOG.analytics.busuanzi && (
+
+ )} */}
+
{/* 代码统计 */}
{BLOG.isProd && (<>
@@ -63,12 +70,6 @@ const ThirdPartyScript = () => {
}}
/>
)}
- {/* 不蒜子 */}
- {BLOG.analytics.busuanzi && (
-
- )}
{/* 站长统计 */}
{BLOG.analytics.cnzzAnalytics && (
diff --git a/lib/busuanzi.js b/lib/busuanzi.js
new file mode 100644
index 00000000..49713446
--- /dev/null
+++ b/lib/busuanzi.js
@@ -0,0 +1,92 @@
+/* eslint-disable */
+let bszCaller, bszTag, scriptTag, ready
+
+let t; let e; let n; let a = !1
+let c = []
+
+// 修复Node同构代码的问题
+if (typeof document !== 'undefined') {
+ ready = function (t) {
+ return a || document.readyState === 'interactive' || document.readyState === 'complete'
+ ? t.call(document)
+ : c.push(function () {
+ return t.call(this)
+ }), this
+ }, e = function () {
+ for (let t = 0, e = c.length; t < e; t++) c[t].apply(document)
+ c = []
+ }, n = function () {
+ a || (a = !0, e.call(window),
+ document.removeEventListener ? document.removeEventListener('DOMContentLoaded', n, !1) : document.attachEvent && (document.detachEvent('onreadystatechange', n), window == window.top && (clearInterval(t), t = null)))
+ }, document.addEventListener
+ ? document.addEventListener('DOMContentLoaded', n, !1)
+ : document.attachEvent && (document.attachEvent('onreadystatechange', function () {
+ /loaded|complete/.test(document.readyState) && n()
+ }), window == window.top && (t = setInterval(function () {
+ try {
+ a || document.documentElement.doScroll('left')
+ } catch (t) {
+ return
+ }
+ n()
+ }, 5)))
+}
+
+bszCaller = {
+ fetch: function (t, e) {
+ const n = 'BusuanziCallback_' + Math.floor(1099511627776 * Math.random())
+ t = t.replace('=BusuanziCallback', '=' + n)
+ scriptTag = document.createElement('SCRIPT'), scriptTag.type = 'text/javascript', scriptTag.defer = !0, scriptTag.src = t, scriptTag.referrerPolicy = "no-referrer-when-downgrade", document.getElementsByTagName('HEAD')[0].appendChild(scriptTag)
+ window[n] = this.evalCall(e)
+ },
+ evalCall: function (e) {
+ return function (t) {
+ ready(function () {
+ try {
+ e(t), scriptTag && scriptTag.parentElement && scriptTag.parentElement.removeChild && scriptTag.parentElement.removeChild(scriptTag)
+ } catch (t) {
+ console.log(t), bszTag.hides()
+ }
+ })
+ }
+ }
+}
+
+const fetch = () => {
+ bszTag && bszTag.hides()
+ bszCaller.fetch('//busuanzi.ibruce.info/busuanzi?jsonpCallback=BusuanziCallback', function (t) {
+ // console.log('不蒜子请求结果',t)
+ bszTag.texts(t), bszTag.shows()
+ })
+}
+
+bszTag = {
+ bszs: ['site_pv', 'page_pv', 'site_uv'],
+ texts: function (n) {
+ this.bszs.map(function (t) {
+ const e = document.getElementById('busuanzi_value_' + t)
+ e && (e.innerHTML = n[t])
+ })
+ },
+ hides: function () {
+ this.bszs.map(function (t) {
+ const e = document.getElementById('busuanzi_container_' + t)
+ e && (e.style.display = 'none')
+ })
+ },
+ shows: function () {
+ this.bszs.map(function (t) {
+ const e = document.getElementById('busuanzi_container_' + t)
+ e && (e.style.display = 'inline')
+ })
+ }
+}
+
+// 修复Node同构代码的问题
+if (typeof document !== 'undefined') {
+ fetch()
+}
+
+module.exports = {
+ fetch
+}
diff --git a/lib/global.js b/lib/global.js
index 4b693135..7329ac72 100644
--- a/lib/global.js
+++ b/lib/global.js
@@ -2,7 +2,6 @@ import lang from './lang'
import { useContext, createContext, useState, useEffect } from 'react'
import cookie from 'react-cookies'
import Router from 'next/router'
-
const GlobalContext = createContext()
/**
@@ -104,28 +103,6 @@ const initTheme = (theme, changeTheme) => {
}
}
-export function handleRouteChange (url) {
- initGoogleAdsense()
-}
-
-/**
- * 初始化谷歌广告
- */
-function initGoogleAdsense () {
- const ads = document.getElementsByClassName('adsbygoogle').length
- const newAdsCount = ads
- if (newAdsCount > 0) {
- for (let i = 0; i <= newAdsCount; i++) {
- try {
- // eslint-disable-next-line no-undef
- (adsbygoogle = window.adsbygoogle || []).push({})
- } catch (e) {
-
- }
- }
- }
-}
-
/**
* 读取默认主题
* @returns {*}
diff --git a/pages/_app.js b/pages/_app.js
index 3ddeecbf..cd07f217 100644
--- a/pages/_app.js
+++ b/pages/_app.js
@@ -6,29 +6,17 @@ import 'rc-dropdown/assets/index.css'
import 'prismjs/themes/prism-okaidia.css'
import 'katex/dist/katex.min.css'
import dynamic from 'next/dynamic'
-import { GlobalContextProvider, handleRouteChange } from '@/lib/global'
-import { useEffect } from 'react'
-import { useRouter } from 'next/router'
+import { GlobalContextProvider } from '@/lib/global'
import { config } from '@fortawesome/fontawesome-svg-core'
import '@fortawesome/fontawesome-svg-core/styles.css'
config.autoAddCss = false
const Ackee = dynamic(() => import('@/components/Ackee'), { ssr: false })
const Gtag = dynamic(() => import('@/components/Gtag'), { ssr: false })
+const Busuanzi = dynamic(() => import('@/components/Busuanzi'), { ssr: false })
+const GoogleAdsense = dynamic(() => import('@/components/GoogleAdsense'), { ssr: false })
const MyApp = ({ Component, pageProps }) => {
- // 全局监听路由变化
- const router = useRouter()
- useEffect(() => {
- handleRouteChange(router.pathname)
- const routerListener = (url) => {
- handleRouteChange(url)
- }
- router.events.on('routeChangeComplete', routerListener)
- return () => {
- router.events.off('routeChangeComplete', routerListener)
- }
- })
return (
{BLOG.isProd && BLOG?.analytics?.provider === 'ackee' && (
@@ -38,6 +26,8 @@ const MyApp = ({ Component, pageProps }) => {
/>
)}
{BLOG.isProd && BLOG?.analytics?.provider === 'ga' && }
+ {BLOG.analytics.busuanzi && }
+ {BLOG.googleAdsenseId && }
)