MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
Awsome2464 (talk | contribs) No edit summary Tag: Manual revert |
Awsome2464 (talk | contribs) No edit summary |
||
| Line 67: | Line 67: | ||
.addClass('revealed'); | .addClass('revealed'); | ||
} | } | ||
}); | |||
}); | |||
/* Displays random page on the home page */ | |||
$(function () { | |||
if (!mw.config.get('wgIsMainPage')) return; | |||
var $container = $('#random-page-content'); | |||
if (!$container.length) return; | |||
$container.html('<p>Loading random page…</p>'); | |||
new mw.Api().get({ | |||
action: 'query', | |||
list: 'random', | |||
rnnamespace: 0, | |||
rnfilterredir: 'nonredirects', | |||
rnlimit: 1 | |||
}).done(function (data) { | |||
var title = data.query.random[0].title; | |||
new mw.Api().get({ | |||
action: 'parse', | |||
page: title, | |||
prop: 'text', | |||
disablelimitreport: true, | |||
disableeditsection: true | |||
}).done(function (parseData) { | |||
$container.html( | |||
'<h2><a href="' + mw.util.getUrl(title) + '">' + title + '</a></h2>' + | |||
parseData.parse.text['*'] | |||
); | |||
}); | |||
}); | }); | ||
}); | }); | ||
Revision as of 19:27, 1 September 2026
/* Any JavaScript here will be loaded for all users on every page load. */
/* Allows images within galleries to be spoiled if "spoiler:" is put at the start of its caption*/
mw.hook('wikipage.content').add(function ($content) {
$content.find('ul.gallery li.gallerybox').each(function () {
var $box = $(this);
var $caption = $box.find('.gallerytext');
var captionText = $caption.text().trim().toLowerCase();
if (captionText.startsWith('spoiler:')) {
$box.addClass('spoiler-image thumb');
$caption.html($caption.html().replace('spoiler: ', ''));
}
});
$content.find('ul.mw-gallery-slideshow').each(function () {
var $gallery = $(this);
function updateLargeImage() {
var $current = $gallery.find('.gallerybox.slideshow-current');
if (!$current.length) {
$current = $gallery.find('.gallerybox').first();
}
var $container = $gallery.find('.mw-gallery-slideshow-img-container');
if ($current.hasClass('spoiler-image') && !$current.hasClass('revealed')) {
$container.addClass('spoiler-image');
} else {
$container.removeClass('spoiler-image revealed');
}
}
updateLargeImage();
var observer = new MutationObserver(function () {
updateLargeImage();
});
observer.observe($gallery[0], {
attributes: true,
attributeFilter: ['class'],
subtree: true,
childList: true
});
});
$content.on('click', '.gallerybox.spoiler', function () {
$(this).addClass('spoiler-image revealed');
});
$content.on('click', '.mw-gallery-slideshow-img-container.spoiler-image', function (e) {
if ($(e.target).closest('.oo-ui-buttonElement, button, a.oo-ui-buttonElement-button').length) {
return;
}
var $container = $(this);
if (!$container.hasClass('revealed')) {
e.preventDefault();
e.stopPropagation();
$container.addClass('revealed');
$container.closest('ul.gallery')
.find('.gallerybox.slideshow-current')
.addClass('revealed');
}
});
});
/* Displays random page on the home page */
$(function () {
if (!mw.config.get('wgIsMainPage')) return;
var $container = $('#random-page-content');
if (!$container.length) return;
$container.html('<p>Loading random page…</p>');
new mw.Api().get({
action: 'query',
list: 'random',
rnnamespace: 0,
rnfilterredir: 'nonredirects',
rnlimit: 1
}).done(function (data) {
var title = data.query.random[0].title;
new mw.Api().get({
action: 'parse',
page: title,
prop: 'text',
disablelimitreport: true,
disableeditsection: true
}).done(function (parseData) {
$container.html(
'<h2><a href="' + mw.util.getUrl(title) + '">' + title + '</a></h2>' +
parseData.parse.text['*']
);
});
});
});