Skip to content

Commit f2b83f4

Browse files
committed
adsense改注入
1 parent 2c6b784 commit f2b83f4

4 files changed

Lines changed: 27 additions & 186 deletions

File tree

docs/javascripts/adsense.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,36 @@
77
var HOME_PATHS = new Set(['/', '/zh/', '/en/', '/es/', '/ar/']);
88
var HOME_TITLES = new Set(['Home', "Power's Wiki"]);
99
var ADSENSE_SCRIPT_SELECTOR = 'script[src*="pagead/js/adsbygoogle.js"]';
10+
var STYLE_ID = 'adsense-inline-style';
11+
var STYLE_CONTENT = `
12+
.adsense-container{margin:1rem 0;padding:.5rem;text-align:center;background:transparent;border:0;box-shadow:none;transition:opacity .3s ease;}
13+
.adsense-container.hidden,.adsense-container.error,.adsense-container[style*='display: none']{display:none!important;margin:0!important;padding:0!important;height:0!important;width:0!important;overflow:hidden!important;opacity:0!important;visibility:hidden!important;position:absolute!important;left:-9999px!important;top:-9999px!important;pointer-events:none!important;}
14+
.adsense-container.hidden *,.adsense-container[style*='display: none'] *{display:none!important;}
15+
.adsense-container ins.adsbygoogle{display:block!important;width:100%;border:0;min-height:auto;}
16+
[data-md-color-scheme='slate'] .adsense-container{background:transparent;border:0;box-shadow:none;}
17+
.adsense-container.loading{opacity:.7;}
18+
.adsense-container.loaded{opacity:1;}
19+
@media(max-width:768px){.adsense-container{margin:.75rem 0;padding:.25rem;}}
20+
`
21+
22+
function ensureStyles() {
23+
if (document.getElementById(STYLE_ID)) {
24+
return;
25+
}
26+
var style = document.createElement('style');
27+
style.id = STYLE_ID;
28+
style.textContent = STYLE_CONTENT;
29+
document.head.appendChild(style);
30+
}
31+
1032

1133
if (!AD_SLOT) {
1234
console.warn('[AdSense] Missing data-ad-slot id. Set window.ADSENSE_SLOT_ID before this script runs.');
1335
return;
1436
}
1537

1638
document.addEventListener('DOMContentLoaded', function() {
39+
ensureStyles();
1740
if (isHomePage()) {
1841
return;
1942
}

docs/javascripts/test-adsense.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,14 @@
5656
}
5757

5858
function testStyles() {
59-
const styleSheets = Array.from(document.styleSheets);
60-
const adsenseStyle = styleSheets.find(sheet =>
61-
sheet.href && sheet.href.includes('adsense.css')
62-
);
59+
const inlineStyle = document.getElementById('adsense-inline-style');
6360

64-
if (adsenseStyle) {
65-
debugLog('✅ AdSense样式已加载');
61+
if (inlineStyle) {
62+
debugLog('✅ AdSense样式已注入');
6663
return true;
6764
}
6865

69-
console.warn('❌ AdSense样式未加载');
66+
console.warn('⚠️ AdSense样式未注入');
7067
return false;
7168
}
7269

docs/stylesheets/adsense.css

Lines changed: 0 additions & 178 deletions
This file was deleted.

mkdocs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,3 @@ extra_javascript:
285285

286286
extra_css:
287287
- stylesheets/extra.css
288-
- stylesheets/adsense.css

0 commit comments

Comments
 (0)