From 550c798a46292debb099b539a366d07db3d9f235 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 20 Jun 2023 20:52:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96adsense=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/GoogleAdsense.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/components/GoogleAdsense.js b/components/GoogleAdsense.js index c6a80b2a..def8cb06 100644 --- a/components/GoogleAdsense.js +++ b/components/GoogleAdsense.js @@ -4,28 +4,31 @@ 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) { + setTimeout(() => { + const ads = document.getElementsByClassName('adsbygoogle') + const adsbygoogle = window.adsbygoogle + if (ads.length > 0) { + for (let i = 0; i <= ads.length; i++) { + try { + adsbygoogle.push(ads[i]) + console.log('adsbygoogle', i, ads[i], adsbygoogle) + } catch (e) { + } } } - } + }, 100) } const router = useRouter() + useEffect(() => { - initGoogleAdsense() router.events.on('routeChangeComplete', initGoogleAdsense) return () => { router.events.off('routeChangeComplete', initGoogleAdsense) } - }, [router.events]) + }, [router]) + return null } @@ -33,6 +36,7 @@ export default function GoogleAdsense() { * 文章内嵌广告单元 * 请在GoogleAdsense后台配置创建对应广告,并且获取相应代码 * 修改下面广告单元中的 data-ad-slot data-ad-format data-ad-layout-key(如果有) + * 添加 可以在本地调试 */ const AdSlot = ({ type = 'show' }) => { if (!BLOG.ADSENSE_GOOGLE_ID) {