mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-14 07:26:52 +00:00
30 lines
764 B
JavaScript
30 lines
764 B
JavaScript
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
|
|
}
|