Difference between revisions of "MediaWiki:Common.js"
Jump to navigation
Jump to search
Thearchivist (talk | contribs) Tag: Reverted |
Thearchivist (talk | contribs) Tag: Manual revert |
||
| Line 66: | Line 66: | ||
} | } | ||
} | } | ||
}); | }); | ||
Latest revision as of 14:06, 30 August 2025
/* Any JavaScript here will be loaded for all users on every page load. */
/* === AdSense global loader (loads once) === */
(function() {
var PUBLISHER_ID = "pub-6349390782750569"; // <-- replace
if (!window._adsenseLoaded) {
window._adsenseLoaded = true;
// Load the AdSense library
var s = document.createElement('script');
s.async = true;
s.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=" + encodeURIComponent(PUBLISHER_ID);
s.crossOrigin = "anonymous";
document.head.appendChild(s);
}
// Utility: create a responsive ad block
window.createAdsenseBlock = function(slotId) {
var cont = document.createElement('div');
cont.style.margin = "12px 0";
var ins = document.createElement('ins');
ins.className = "adsbygoogle";
ins.style.display = "block";
ins.style.textAlign = "center";
ins.setAttribute("data-ad-client", PUBLISHER_ID);
ins.setAttribute("data-ad-slot", slotId); // <-- pass the slot ID
ins.setAttribute("data-ad-format", "auto");
ins.setAttribute("data-full-width-responsive", "true");
cont.appendChild(ins);
// Kick AdSense to render
(window.adsbygoogle = window.adsbygoogle || []).push({});
return cont;
};
})();
/* === Choose where to place ads === */
mw.loader.using('mediawiki.util', function () {
// Avoid showing ads to logged-in users (optional)
if (mw.config.get('wgUserName')) return;
// Only show on content pages (skip Special:, Edit, etc.)
var ns = mw.config.get('wgNamespaceNumber');
var action = mw.config.get('wgAction');
if (ns !== 0 || action !== 'view') return;
// Replace these with your slot IDs:
var SLOT_BELOW_TITLE = "1234567890";
var SLOT_AFTER_PARA1 = "2345678901";
// Insert below the page title
var titleEl = document.getElementById('firstHeading');
if (titleEl && window.createAdsenseBlock) {
var ad1 = createAdsenseBlock(SLOT_BELOW_TITLE);
titleEl.insertAdjacentElement('afterend', ad1);
}
// Insert after first paragraph of content
var content = document.getElementById('mw-content-text');
if (content && window.createAdsenseBlock) {
var firstP = content.querySelector('.mw-parser-output > p');
if (firstP) {
var ad2 = createAdsenseBlock(SLOT_AFTER_PARA1);
firstP.insertAdjacentElement('afterend', ad2);
}
}
});