MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
Awsome2464 (talk | contribs) No edit summary Tag: Reverted |
Awsome2464 (talk | contribs) No edit summary Tag: Reverted |
||
| Line 3: | Line 3: | ||
/* Allows images within galleries to be spoiled if "spoiler:" is put at the start of its caption*/ | /* Allows images within galleries to be spoiled if "spoiler:" is put at the start of its caption*/ | ||
mw.hook('wikipage.content').add(function ($content) { | mw.hook('wikipage.content').add(function ($content) { | ||
$content.find('ul.gallery').each(function () { | $content.find('ul.gallery li.gallerybox').each(function () { | ||
var $ | var $box = $(this); | ||
var | 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: ', '')); | |||
} | } | ||
}); | }); | ||
| Line 47: | Line 17: | ||
var $gallery = $(this); | var $gallery = $(this); | ||
function | function updateLargeImage() { | ||
var $current = $gallery.find('.gallerybox.slideshow-current'); | var $current = $gallery.find('.gallerybox.slideshow-current'); | ||
if (!$current.length) { | if (!$current.length) { | ||
| Line 59: | Line 25: | ||
var $container = $gallery.find('.mw-gallery-slideshow-img-container'); | var $container = $gallery.find('.mw-gallery-slideshow-img-container'); | ||
if ($current.hasClass('spoiler') && !$current.hasClass('revealed')) { | if ($current.hasClass('spoiler-image') && !$current.hasClass('spoiler-image.revealed')) { | ||
$container.addClass('spoiler- | $container.addClass('spoiler-image'); | ||
} else { | } else { | ||
$container.removeClass('spoiler- | $container.removeClass('spoiler-image revealed'); | ||
} | } | ||
} | } | ||
updateLargeImage(); | |||
var observer = new MutationObserver(function () { | |||
updateLargeImage(); | |||
}); | |||
observer.observe($gallery[0], { | observer.observe($gallery[0], { | ||
attributes: true, | attributes: true, | ||
| Line 81: | Line 47: | ||
$content.on('click', '.gallerybox.spoiler', function () { | $content.on('click', '.gallerybox.spoiler', function () { | ||
$(this).addClass('revealed'); | $(this).addClass('spoiler-image.revealed'); | ||
}); | }); | ||
$content.on('click', '.mw-gallery-slideshow-img-container.spoiler- | $content.on('click', '.mw-gallery-slideshow-img-container.spoiler-main', function (e) { | ||
if ($(e.target).closest('.oo-ui-buttonElement, button, a.oo-ui-buttonElement-button').length) { | if ($(e.target).closest('.oo-ui-buttonElement, button, a.oo-ui-buttonElement-button').length) { | ||
return; | return; | ||
| Line 91: | Line 56: | ||
var $container = $(this); | var $container = $(this); | ||
if (!$container.hasClass('revealed')) { | if (!$container.hasClass('spoiler-image.revealed')) { | ||
e.preventDefault(); | e.preventDefault(); | ||
e.stopPropagation(); | e.stopPropagation(); | ||
$container.addClass(' | $container.addClass('spoiler-image.revealed'); | ||
$container.closest('ul.gallery') | |||
.find('.gallerybox.slideshow-current') | |||
.addClass('spoiler-image.revealed'); | |||
} | } | ||
}); | }); | ||
}); | }); | ||
Revision as of 17:55, 20 August 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('spoiler-image.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-main', function (e) {
if ($(e.target).closest('.oo-ui-buttonElement, button, a.oo-ui-buttonElement-button').length) {
return;
}
var $container = $(this);
if (!$container.hasClass('spoiler-image.revealed')) {
e.preventDefault();
e.stopPropagation();
$container.addClass('spoiler-image.revealed');
$container.closest('ul.gallery')
.find('.gallerybox.slideshow-current')
.addClass('spoiler-image.revealed');
}
});
});