Skip to content

Commit ab9adb0

Browse files
committed
Revert "优化 AMP 自动广告插入逻辑,确保在 DOM 加载完成后再插入广告代码"
This reverts commit b020f58.
1 parent b020f58 commit ab9adb0

File tree

2 files changed

+16
-39
lines changed

2 files changed

+16
-39
lines changed

docusaurus.config.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,16 @@ const config = {
1717
experimental_faster: true, // 启用所有性能优化,包括 Rspack
1818
},
1919
scripts: [
20-
{ src: 'https://hm.baidu.com/hm.js?de86b38bbc3dec5ed31c4da285286374', async: true },
21-
// Google AdSense 自动广告脚本
22-
{
23-
src: 'https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js',
24-
async: true,
25-
'custom-element': 'amp-auto-ads'
26-
}
20+
{ src: 'https://hm.baidu.com/hm.js?de86b38bbc3dec5ed31c4da285286374', async: true }
2721
],
2822
headTags: [
29-
// Google AdSense 元标签(可选)
23+
// Google AdSense AMP 自动广告脚本
3024
{
31-
tagName: 'meta',
25+
tagName: 'script',
3226
attributes: {
33-
name: 'google-adsense-account',
34-
content: 'ca-pub-9617969692940509',
27+
'custom-element': 'amp-auto-ads',
28+
src: 'https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js',
29+
async: 'async',
3530
},
3631
},
3732
],

src/theme/Root.js

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,23 @@ import React, { useEffect } from 'react';
22

33
export default function Root({ children }) {
44
useEffect(() => {
5-
// 等待 DOM 完全加载后再插入广告代码
6-
const insertAdsenseCode = () => {
7-
// 检查是否已经插入过广告代码
8-
if (document.querySelector('amp-auto-ads')) {
9-
return;
10-
}
11-
12-
// 创建 AMP 自动广告元素
13-
const ampAutoAds = document.createElement('amp-auto-ads');
14-
ampAutoAds.setAttribute('type', 'adsense');
15-
ampAutoAds.setAttribute('data-ad-client', 'ca-pub-9617969692940509');
16-
17-
// 将广告代码插入到 body 的开头(紧跟在 <body> 标签后)
18-
if (document.body && document.body.firstChild) {
19-
document.body.insertBefore(ampAutoAds, document.body.firstChild);
20-
} else if (document.body) {
21-
document.body.appendChild(ampAutoAds);
22-
}
23-
};
24-
25-
// 如果 DOM 已经加载完成,直接插入
26-
if (document.readyState === 'complete' || document.readyState === 'interactive') {
27-
insertAdsenseCode();
28-
} else {
29-
// 否则等待 DOM 加载完成
30-
window.addEventListener('DOMContentLoaded', insertAdsenseCode);
5+
// 创建 AMP 自动广告元素
6+
const ampAutoAds = document.createElement('amp-auto-ads');
7+
ampAutoAds.setAttribute('type', 'adsense');
8+
ampAutoAds.setAttribute('data-ad-client', 'ca-pub-9617969692940509');
9+
10+
// 将广告代码插入到 body 的开头
11+
if (document.body && document.body.firstChild) {
12+
document.body.insertBefore(ampAutoAds, document.body.firstChild);
13+
} else if (document.body) {
14+
document.body.appendChild(ampAutoAds);
3115
}
3216

3317
// 清理函数
3418
return () => {
35-
const ampAutoAds = document.querySelector('amp-auto-ads');
3619
if (ampAutoAds && ampAutoAds.parentNode) {
3720
ampAutoAds.parentNode.removeChild(ampAutoAds);
3821
}
39-
window.removeEventListener('DOMContentLoaded', insertAdsenseCode);
4022
};
4123
}, []);
4224

0 commit comments

Comments
 (0)