Skip to content

Commit 1b2dca7

Browse files
committed
docs: ad banner mod
1 parent f4e3ad7 commit 1b2dca7

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

website/src/css/custom.css

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ html[data-theme='dark'] .docusaurus-highlight-code-line {
5151
justify-content: space-between;
5252
}
5353

54-
#eab {
54+
.eab {
5555
margin: calc(var(--ifm-menu-link-padding-horizontal) + 0.5rem) 0 0 -0.5rem;
5656
}
57+
58+
.crbn {
59+
margin: calc(var(--ifm-menu-link-padding-horizontal) + 0.5rem) 0.5rem 0 0;
60+
61+
&.dark #carbon-responsive {
62+
filter: invert(1);
63+
--carbon-bg-primary: transparent;
64+
}
65+
}

website/src/theme/DocSidebar/index.js

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,75 @@ import React, { useEffect, useRef } from 'react';
22
import { useColorMode } from '@docusaurus/theme-common';
33
import OriginalDocSidebar from '@theme-original/DocSidebar';
44

5+
async function shouldUseFallback() {
6+
return true; // Temporarily disable EthicalAds
7+
8+
while (typeof ethicalads === 'undefined') {
9+
await new Promise((resolve) => setTimeout(resolve, 300));
10+
}
11+
12+
const placements = await ethicalads.wait;
13+
14+
return !placements.length || placements[0].response.campaign_type !== 'paid';
15+
}
16+
17+
function createEthicalAdsBlock(root) {
18+
const banner = document.createElement('div');
19+
20+
banner.className = 'eab flat horizontal'
21+
banner.id = 'bwndw';
22+
banner.setAttribute('data-ea-publisher', 'react-chartjs-2jsorg');
23+
banner.setAttribute('data-ea-type', 'image');
24+
25+
root?.appendChild(banner);
26+
27+
if (typeof ethicalads !== 'undefined') {
28+
ethicalads.reload();
29+
}
30+
31+
return banner;
32+
}
33+
34+
function createCarbonAdsBlock(root) {
35+
const banner = document.createElement('div');
36+
const script = document.createElement('script');
37+
38+
banner.className = 'crbn';
39+
banner.id = 'bwndw';
40+
41+
script.src = '//cdn.carbonads.com/carbon.js?serve=CWBDT53N&placement=react-chartjs-2jsorg&format=cover';
42+
script.id = '_carbonads_js';
43+
script.async = true;
44+
45+
banner.appendChild(script);
46+
root?.appendChild(banner);
47+
48+
return banner;
49+
}
50+
51+
function setColorMode(banner, colorMode) {
52+
banner?.classList.toggle('dark', colorMode === 'dark');
53+
}
54+
555
export default function DocSidebar(props) {
656
const bannerRef = useRef();
757
const { colorMode } = useColorMode();
858

959
useEffect(() => {
10-
if (!document.getElementById('eab')) {
11-
const banner = document.createElement('div');
12-
13-
bannerRef.current = banner;
14-
banner.className = 'flat horizontal'
15-
banner.id = 'eab';
16-
banner.setAttribute('data-ea-publisher', 'react-chartjs-2jsorg');
17-
banner.setAttribute('data-ea-type', 'image');
18-
document.querySelector('.theme-doc-sidebar-menu')?.parentElement?.appendChild(banner);
19-
20-
if (typeof ethicalads !== 'undefined') {
21-
ethicalads.reload();
22-
}
60+
if (!document.getElementById('bwndw')) {
61+
shouldUseFallback().then((useFallback) => {
62+
if (!document.getElementById('bwndw')) {
63+
const root = document.querySelector('.theme-doc-sidebar-menu')?.parentElement;
64+
65+
bannerRef.current = useFallback ? createCarbonAdsBlock(root) : createEthicalAdsBlock(root);
66+
setColorMode(bannerRef.current, colorMode);
67+
}
68+
});
2369
}
2470
}, []);
2571

2672
useEffect(() => {
27-
bannerRef.current?.classList.toggle('dark', colorMode === 'dark');
73+
setColorMode(bannerRef.current, colorMode);
2874
}, [colorMode]);
2975

3076
return (

0 commit comments

Comments
 (0)