MediaWiki:Common.js: Difference between revisions
Appearance
mNo edit summary |
mNo edit summary |
||
| Line 1: | Line 1: | ||
mw.loader.using(['mediawiki.util'], function () { | mw.loader.using(['mediawiki.util'], function () { | ||
$(function () { | $(function () { | ||
var isMainPage = | var isMainPage = | ||
mw.config.get('wgIsMainPage') || | mw.config.get('wgIsMainPage') || | ||
| Line 9: | Line 7: | ||
window.megabonkNitro = window.megabonkNitro || {}; | window.megabonkNitro = window.megabonkNitro || {}; | ||
var flags = window.megabonkNitro; | var flags = window.megabonkNitro; | ||
function logNitroError(name, err) { | |||
if (window.console && console.warn) { | |||
console.warn('[Megabonk Nitro] ' + name + ' failed:', err); | |||
} | |||
} | |||
function createNitroAd(name, adId, options) { | |||
if (!window.nitroAds || typeof window.nitroAds.createAd !== 'function') { | |||
return false; | |||
} | |||
if (!options || typeof options !== 'object') { | |||
logNitroError(name, 'Invalid options object'); | |||
return false; | |||
} | |||
try { | |||
window.nitroAds.createAd(adId, options); | |||
return true; | |||
} catch (err) { | |||
logNitroError(name, err); | |||
return false; | |||
} | |||
} | |||
function runWhenNitroReady(callback, attemptsLeft) { | |||
attemptsLeft = attemptsLeft || 40; | |||
if (window.nitroAds && typeof window.nitroAds.createAd === 'function') { | |||
callback(); | |||
return; | |||
} | |||
if (attemptsLeft <= 0) { | |||
logNitroError('loader', 'nitroAds.createAd was not available'); | |||
return; | |||
} | |||
window.setTimeout(function () { | |||
runWhenNitroReady(callback, attemptsLeft - 1); | |||
}, 250); | |||
} | |||
function runIdle(callback, timeout) { | |||
if ('requestIdleCallback' in window) { | |||
window.requestIdleCallback(callback, { timeout: timeout || 2500 }); | |||
} else { | |||
window.setTimeout(callback, timeout || 800); | |||
} | |||
} | |||
function initTopBanner() { | function initTopBanner() { | ||
if (isMainPage | if (isMainPage || flags.topBannerInit) return; | ||
flags.topBannerInit = true; | flags.topBannerInit = true; | ||
| Line 28: | Line 76: | ||
textAlign: 'center' | textAlign: 'center' | ||
}); | }); | ||
$bodyContent.prepend($banner); | $bodyContent.prepend($banner); | ||
} | } | ||
createNitroAd('top banner', bannerId, { | |||
sizes: [ | sizes: [ | ||
[728, 90], | [728, 90], | ||
| Line 38: | Line 87: | ||
[320, 50] | [320, 50] | ||
], | ], | ||
geoDeny: [ | geoDeny: ['RU'] | ||
}); | }); | ||
} | } | ||
| Line 63: | Line 112: | ||
} | } | ||
createNitroAd('sidebar', adId, { | |||
sizes: [ | sizes: [ | ||
[300, 350], | [300, 350], | ||
| Line 70: | Line 119: | ||
[160, 600] | [160, 600] | ||
], | ], | ||
geoDeny: [ | geoDeny: ['RU'] | ||
}); | }); | ||
} | } | ||
| Line 78: | Line 127: | ||
flags.anchorInit = true; | flags.anchorInit = true; | ||
createNitroAd('anchor', 'megabonk_anchor', { | |||
format: | format: 'anchor-v2', | ||
anchor: | anchor: 'bottom', | ||
anchorBgColor: | anchorBgColor: 'transparent', | ||
anchorClose: false, | anchorClose: false, | ||
anchorPersistClose: false, | anchorPersistClose: false, | ||
anchorStickyOffset: 0, | anchorStickyOffset: 0, | ||
mediaQuery: | mediaQuery: '(min-width: 0px)', | ||
geoDeny: [ | geoDeny: ['RU'] | ||
}); | }); | ||
} | } | ||
| Line 94: | Line 143: | ||
flags.videoInit = true; | flags.videoInit = true; | ||
createNitroAd('floating video', 'megabonk_video', { | |||
format: | format: 'floating', | ||
geoDeny: [ | geoDeny: ['RU'] | ||
}); | }); | ||
} | } | ||
| Line 104: | Line 153: | ||
flags.stickyInit = true; | flags.stickyInit = true; | ||
createNitroAd('sticky rail', 'megabonk_sticky', { | |||
format: | format: 'rail', | ||
rail: | rail: 'right', | ||
railOffsetTop: 0, | railOffsetTop: 0, | ||
railOffsetBottom: 0, | railOffsetBottom: 0, | ||
railCollisionWhitelist: [], | railCollisionWhitelist: [], | ||
railCloseColor: | railCloseColor: '#666666', | ||
railSpacing: 5, | railSpacing: 5, | ||
railStack: true, | railStack: true, | ||
railStickyTop: 0, | railStickyTop: 0, | ||
railVerticalAlign: | railVerticalAlign: 'center', | ||
sizes: [ | sizes: [ | ||
[160, 600], | [160, 600], | ||
| Line 120: | Line 169: | ||
[300, 600] | [300, 600] | ||
], | ], | ||
mediaQuery: | mediaQuery: '(min-width: 1025px)', | ||
geoDeny: [ | geoDeny: ['RU'] | ||
}); | }); | ||
} | } | ||
initTopBanner(); | runWhenNitroReady(function () { | ||
initTopBanner(); | |||
initSidebarAd(); | |||
runIdle(function () { | |||
initSticky(); | |||
}, 1200); | |||
runIdle(function () { | |||
initAnchor(); | |||
}, 2200); | |||
runIdle(function () { | |||
initVideo(); | |||
}, 4000); | |||
}); | |||
}); | }); | ||
}); | }); | ||
Revision as of 12:03, 22 May 2026
mw.loader.using(['mediawiki.util'], function () {
$(function () {
var isMainPage =
mw.config.get('wgIsMainPage') ||
mw.config.get('wgPageName') === mw.config.get('wgMainPageTitle');
window.megabonkNitro = window.megabonkNitro || {};
var flags = window.megabonkNitro;
function logNitroError(name, err) {
if (window.console && console.warn) {
console.warn('[Megabonk Nitro] ' + name + ' failed:', err);
}
}
function createNitroAd(name, adId, options) {
if (!window.nitroAds || typeof window.nitroAds.createAd !== 'function') {
return false;
}
if (!options || typeof options !== 'object') {
logNitroError(name, 'Invalid options object');
return false;
}
try {
window.nitroAds.createAd(adId, options);
return true;
} catch (err) {
logNitroError(name, err);
return false;
}
}
function runWhenNitroReady(callback, attemptsLeft) {
attemptsLeft = attemptsLeft || 40;
if (window.nitroAds && typeof window.nitroAds.createAd === 'function') {
callback();
return;
}
if (attemptsLeft <= 0) {
logNitroError('loader', 'nitroAds.createAd was not available');
return;
}
window.setTimeout(function () {
runWhenNitroReady(callback, attemptsLeft - 1);
}, 250);
}
function runIdle(callback, timeout) {
if ('requestIdleCallback' in window) {
window.requestIdleCallback(callback, { timeout: timeout || 2500 });
} else {
window.setTimeout(callback, timeout || 800);
}
}
function initTopBanner() {
if (isMainPage || flags.topBannerInit) return;
flags.topBannerInit = true;
var bannerId = 'megabonk_top_banner';
var $bodyContent = $('#bodyContent');
if (!$bodyContent.length) return;
var $banner = $('#' + bannerId);
if (!$banner.length) {
$banner = $('<div>', {
id: bannerId,
'class': 'nitro-top-banner'
}).css({
margin: '10px 0 20px',
textAlign: 'center'
});
$bodyContent.prepend($banner);
}
createNitroAd('top banner', bannerId, {
sizes: [
[728, 90],
[970, 90],
[320, 100],
[320, 50]
],
geoDeny: ['RU']
});
}
function initSidebarAd() {
if (flags.sidebarInit) return;
flags.sidebarInit = true;
var adId = 'megabonk_sidebar';
var $menuContainer = $('.vector-main-menu-container');
if (!$menuContainer.length) return;
var $adDiv = $('#' + adId);
if (!$adDiv.length) {
$adDiv = $('<div>', {
id: adId,
'class': 'nitro-sidebar-ad'
}).css({
margin: '20px 0',
textAlign: 'center'
});
$menuContainer.after($adDiv);
}
createNitroAd('sidebar', adId, {
sizes: [
[300, 350],
[300, 250],
[300, 600],
[160, 600]
],
geoDeny: ['RU']
});
}
function initAnchor() {
if (flags.anchorInit) return;
flags.anchorInit = true;
createNitroAd('anchor', 'megabonk_anchor', {
format: 'anchor-v2',
anchor: 'bottom',
anchorBgColor: 'transparent',
anchorClose: false,
anchorPersistClose: false,
anchorStickyOffset: 0,
mediaQuery: '(min-width: 0px)',
geoDeny: ['RU']
});
}
function initVideo() {
if (flags.videoInit) return;
flags.videoInit = true;
createNitroAd('floating video', 'megabonk_video', {
format: 'floating',
geoDeny: ['RU']
});
}
function initSticky() {
if (flags.stickyInit) return;
flags.stickyInit = true;
createNitroAd('sticky rail', 'megabonk_sticky', {
format: 'rail',
rail: 'right',
railOffsetTop: 0,
railOffsetBottom: 0,
railCollisionWhitelist: [],
railCloseColor: '#666666',
railSpacing: 5,
railStack: true,
railStickyTop: 0,
railVerticalAlign: 'center',
sizes: [
[160, 600],
[300, 250],
[300, 600]
],
mediaQuery: '(min-width: 1025px)',
geoDeny: ['RU']
});
}
runWhenNitroReady(function () {
initTopBanner();
initSidebarAd();
runIdle(function () {
initSticky();
}, 1200);
runIdle(function () {
initAnchor();
}, 2200);
runIdle(function () {
initVideo();
}, 4000);
});
});
});