1
1
import React , { useEffect , useRef } from 'react' ;
2
2
import { useRouter } from 'next/router' ;
3
3
4
+ declare global {
5
+ interface Window {
6
+ _carbonads : {
7
+ refresh : ( ) => void ;
8
+ reload : ( where : string , force_serve : boolean ) => void ;
9
+ remove : ( el : HTMLElement ) => void ;
10
+ srv : ( ) => void ;
11
+ } ;
12
+ }
13
+ }
14
+
4
15
type Props = {
5
16
className ?: string ;
6
17
variant ?: 'sidebar' ;
@@ -11,28 +22,41 @@ function CarbonAds({ className, variant = 'sidebar' }: Props) {
11
22
const router = useRouter ( ) ;
12
23
13
24
useEffect ( ( ) => {
14
- const hasCarbonAdsScript = document . querySelector ( '#_carbonads_js' ) ;
15
- if ( ! hasCarbonAdsScript ) {
16
- const carbonAdsScript = document . createElement ( 'script' ) ;
17
- carbonAdsScript . id = '_carbonads_js' ;
18
- carbonAdsScript . type = 'text/javascript' ;
19
- carbonAdsScript . async = true ;
20
- document
21
- . querySelector ( '#carbonads-container' )
22
- ?. appendChild ( carbonAdsScript ) ;
23
- carbonAdsScript . src = `//cdn.carbonads.com/carbon.js?serve=CE7I627Y&placement=json-schemaorg&rnd=${ Math . random ( ) } ` ;
24
- }
25
+ const mobileMediaQuery = window . matchMedia ( '(max-width: 1023px)' ) ;
26
+ if ( ! mobileMediaQuery . matches ) {
27
+ const hasCarbonAds = document . querySelector ( '#carbonads' ) ;
28
+ // Check if another ad is present to refresh
29
+ if ( hasCarbonAds ) {
30
+ window . _carbonads . refresh ( ) ;
31
+ return ;
32
+ } else {
33
+ // Check if the script is present (ad is not yet present) so that duplicate requests are not made to carbon ads
34
+ const hasCarbonAdsScript = document . querySelector ( '#_carbonads_js' ) ;
35
+ if ( ! hasCarbonAdsScript ) {
36
+ const carbonAdsScript = document . createElement ( 'script' ) ;
37
+ carbonAdsScript . id = '_carbonads_js' ;
38
+ carbonAdsScript . type = 'text/javascript' ;
39
+ carbonAdsScript . async = true ;
40
+ document
41
+ . querySelector ( '#carbonads-container' )
42
+ ?. appendChild ( carbonAdsScript ) ;
43
+ carbonAdsScript . src = `//cdn.carbonads.com/carbon.js?serve=CE7I627Y&placement=json-schemaorg&rnd=${ Math . random ( ) } ` ;
44
+ }
45
+ }
25
46
26
- const existingStyleSheet = document . querySelector ( '#_carbonads_css' ) ;
27
- if ( existingStyleSheet ) {
28
- existingStyleSheet . innerHTML = CarbonAds . stylesheet [ variant ] ;
47
+ const existingStyleSheet = document . querySelector ( '#_carbonads_css' ) ;
48
+ if ( existingStyleSheet ) {
49
+ existingStyleSheet . innerHTML = CarbonAds . stylesheet [ variant ] ;
50
+ } else {
51
+ const carbonAdsStyleSheet = document . createElement ( 'style' ) ;
52
+ carbonAdsStyleSheet . id = '_carbonads_css' ;
53
+ carbonAdsStyleSheet . innerHTML = CarbonAds . stylesheet [ variant ] ;
54
+ document
55
+ . querySelector ( '#carbonads-container' )
56
+ ?. appendChild ( carbonAdsStyleSheet ) ;
57
+ }
29
58
} else {
30
- const carbonAdsStyleSheet = document . createElement ( 'style' ) ;
31
- carbonAdsStyleSheet . id = '_carbonads_css' ;
32
- carbonAdsStyleSheet . innerHTML = CarbonAds . stylesheet [ variant ] ;
33
- document
34
- . querySelector ( '#carbonads-container' )
35
- ?. appendChild ( carbonAdsStyleSheet ) ;
59
+ ( carbonRef . current as HTMLElement ) . style . display = 'none' ;
36
60
}
37
61
} , [ router . asPath ] ) ;
38
62
0 commit comments