|
import { siteConfig } from '@/lib/config' |
|
import { loadExternalResource } from '@/lib/utils' |
|
import { useRouter } from 'next/router' |
|
import { useEffect } from 'react' |
|
|
|
|
|
|
|
|
|
|
|
export default function GoogleAdsense() { |
|
const initGoogleAdsense = () => { |
|
loadExternalResource(`https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=${siteConfig('ADSENSE_GOOGLE_ID')}`, 'js').then(url => { |
|
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]) |
|
} catch (e) { |
|
|
|
} |
|
} |
|
} |
|
}, 100) |
|
}) |
|
} |
|
|
|
const router = useRouter() |
|
useEffect(() => { |
|
|
|
setTimeout(() => { |
|
initGoogleAdsense() |
|
}, 3000) |
|
}, [router]) |
|
|
|
return null |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const AdSlot = ({ type = 'show' }) => { |
|
if (!siteConfig('ADSENSE_GOOGLE_ID')) { |
|
return null |
|
} |
|
|
|
if (type === 'in-article') { |
|
return <ins className="adsbygoogle" |
|
style={{ display: 'block', textAlign: 'center' }} |
|
data-ad-layout="in-article" |
|
data-ad-format="fluid" |
|
data-adtest={siteConfig('ADSENSE_GOOGLE_TEST') ? 'on' : 'off'} |
|
data-ad-client={siteConfig('ADSENSE_GOOGLE_ID')} |
|
data-ad-slot={siteConfig('ADSENSE_GOOGLE_SLOT_IN_ARTICLE')}></ins> |
|
} |
|
|
|
|
|
if (type === 'flow') { |
|
return <ins className="adsbygoogle" |
|
data-ad-format="fluid" |
|
data-ad-layout-key="-5j+cz+30-f7+bf" |
|
style={{ display: 'block' }} |
|
data-adtest={siteConfig('ADSENSE_GOOGLE_TEST') ? 'on' : 'off'} |
|
data-ad-client={siteConfig('ADSENSE_GOOGLE_ID')} |
|
data-ad-slot={siteConfig('ADSENSE_GOOGLE_SLOT_FLOW')}></ins> |
|
} |
|
|
|
|
|
if (type === 'native') { |
|
return <ins className="adsbygoogle" |
|
style={{ display: 'block', textAlign: 'center' }} |
|
data-ad-format="autorelaxed" |
|
data-adtest={siteConfig('ADSENSE_GOOGLE_TEST') ? 'on' : 'off'} |
|
data-ad-client={siteConfig('ADSENSE_GOOGLE_ID')} |
|
data-ad-slot={siteConfig('ADSENSE_GOOGLE_SLOT_NATIVE')}></ins> |
|
} |
|
|
|
|
|
return <ins className="adsbygoogle" |
|
style={{ display: 'block' }} |
|
data-ad-client={siteConfig('ADSENSE_GOOGLE_ID')} |
|
data-adtest={siteConfig('ADSENSE_GOOGLE_TEST') ? 'on' : 'off'} |
|
data-ad-slot={siteConfig('ADSENSE_GOOGLE_SLOT_AUTO')} |
|
data-ad-format="auto" |
|
data-full-width-responsive="true"></ins> |
|
} |
|
|
|
export { AdSlot } |
|
|