Add shared AdSense injection for all pages

This commit is contained in:
TianLun Song 2025-10-04 22:16:58 +08:00
parent 4312baffda
commit bb8c7d095f
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,81 @@
(function () {
const AD_SLOT = "2056784980";
const AD_CLIENT = "ca-pub-7296634171837358";
const MAX_RENDER_ATTEMPTS = 10;
const RETRY_DELAY = 500;
function renderAdWithRetry(attempt = 0) {
if (attempt > MAX_RENDER_ATTEMPTS) {
return;
}
if (window.adsbygoogle && Array.isArray(window.adsbygoogle)) {
try {
window.adsbygoogle.push({});
} catch (error) {
console.warn("Adsense rendering error", error);
}
return;
}
setTimeout(() => renderAdWithRetry(attempt + 1), RETRY_DELAY);
}
function createAdElement(container) {
const adElement = document.createElement("ins");
adElement.className = "adsbygoogle page-ad";
adElement.style.display = "block";
adElement.setAttribute("data-ad-client", AD_CLIENT);
adElement.setAttribute("data-ad-slot", AD_SLOT);
adElement.setAttribute("data-ad-format", "auto");
adElement.setAttribute("data-full-width-responsive", "true");
const heading = container.querySelector("h1, h2, h3, h4, h5, h6");
if (heading && heading.parentNode) {
if (heading.nextSibling) {
heading.parentNode.insertBefore(adElement, heading.nextSibling);
} else {
heading.parentNode.appendChild(adElement);
}
} else {
container.insertBefore(adElement, container.firstChild);
}
return adElement;
}
function ensureAd() {
const container = document.querySelector("article.md-content__inner");
if (!container) {
return;
}
let adElement = container.querySelector(
`ins.adsbygoogle[data-ad-slot="${AD_SLOT}"]`
);
if (!adElement) {
adElement = createAdElement(container);
}
if (
adElement &&
!adElement.dataset.adsbygoogleStatus &&
!adElement.getAttribute("data-adsbygoogle-status")
) {
renderAdWithRetry();
}
}
function setup() {
ensureAd();
}
if (window.document$ && typeof window.document$.subscribe === "function") {
window.document$.subscribe(setup);
} else {
document.addEventListener("DOMContentLoaded", setup);
}
})();

View File

@ -84,6 +84,8 @@ theme:
repo: fontawesome/brands/github
extra_css:
- stylesheets/code.css
extra_javascript:
- assets/javascripts/ads.js
extra:
analytics:
provider: google