MediaWiki:Common.js: Difference between revisions

From Another Chance Wiki
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
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 $gallery = $(this);
        var $box = $(this);
        var isWholeGallerySpoiler = $gallery.hasClass('spoiler-image');
        var $caption = $box.find('.gallerytext');
        var hasAnySpoiler = isWholeGallerySpoiler;
         var captionText = $caption.text().trim().toLowerCase();
 
        $gallery.find('li.gallerybox').each(function () {
            var $box = $(this);
            var captionText = $box.find('.gallerytext').text().trim().toLowerCase();
            var imgSrc = ($box.find('img').attr('src') || '') + ' ' +
                        ($box.find('img').attr('alt') || '');
 
            var isIndividualSpoiler = captionText.indexOf('spoiler:') === 0;
 
            if (isWholeGallerySpoiler || isIndividualSpoiler) {
                $box.addClass('spoiler-image');
                hasAnySpoiler = true;
 
                if (isIndividualSpoiler) {
                    $box.find('.gallerytext').html(function (i, html) {
                        return html.replace('spoiler: ', '');
                    });
                }
            }
        });
 
         if (hasAnySpoiler) {
            var $btn = $('<button type="button">')
                .text('Reveal all spoilers');
 
            $btn.on('click', function () {
                $gallery.addClass('spoiler-image revealed');
                $gallery.find('.gallerybox.spoiler-image').addClass('revealed');
                $gallery.find('.mw-gallery-slideshow-img-container img')
                        .css('filter', 'none')
                        .removeClass('spoiler-image');
                $btn.remove();
            });


             $gallery.before($btn);
        if (captionText.startsWith('spoiler:')) {
             $box.addClass('spoiler-image thumb');
            $caption.html($caption.html().replace('spoiler: ', ''));
         }
         }
     });
     });


     function applySpoilerToLargeImage($gallery) {
     $content.find('ul.mw-gallery-slideshow').each(function () {
         if ($gallery.hasClass('spoiler-image revealed')) {
         var $gallery = $(this);
            return;
        }


         var $current = $gallery.find('.gallerybox.slideshow-current');
         function updateLargeImage() {
        if (!$current.length) {
            var $current = $gallery.find('.gallerybox.slideshow-current');
            $current = $gallery.find('.gallerybox').first();
            if (!$current.length) {
        }
                $current = $gallery.find('.gallerybox').first();
            }


        var $container = $gallery.find('.mw-gallery-slideshow-img-container');
            var $container = $gallery.find('.mw-gallery-slideshow-img-container');
        var $largeImg = $container.find('img');


        if ($current.hasClass('spoiler-image') && !$current.hasClass('revealed')) {
            if ($current.hasClass('spoiler-image') && !$current.hasClass('revealed')) {
            $largeImg.addClass('spoiler-image')'
                $container.addClass('spoiler-image');
        } else {
            } else {
            $largeImg.removeClass('spoiler-imgage');
                $container.removeClass('spoiler-image revealed');
             $container.removeClass('spoiler-image');
             }
         }
         }
    }
    $content.find('ul.mw-gallery-slideshow').each(function () {
        var $gallery = $(this);


         applySpoilerToLargeImage($gallery);
         updateLargeImage();
        setTimeout(function () { applySpoilerToLargeImage($gallery); }, 150);
        setTimeout(function () { applySpoilerToLargeImage($gallery); }, 500);


         var observer = new MutationObserver(function () {
         var observer = new MutationObserver(function () {
             applySpoilerToLargeImage($gallery);
             updateLargeImage();
         });
         });
         observer.observe($gallery[0], {
         observer.observe($gallery[0], {
             childList: true,
             attributes: true,
            attributeFilter: ['class'],
             subtree: true,
             subtree: true,
             attributes: true,
             childList: true
            attributeFilter: ['class', 'src']
         });
         });
     });
     });


     $content.on('click', '.gallerybox.spoiler-image', function () {
     $content.on('click', '.gallerybox.spoiler', function () {
         $(this).addClass('revealed');
         $(this).addClass('spoiler-image revealed');
        var $gallery = $(this).closest('ul.gallery');
        applySpoilerToLargeImage($gallery);
     });
     });


     $content.on('click', '.mw-gallery-slideshow-img-container.spoiler-image, .mw-gallery-slideshow-img-container', function (e) {
     $content.on('click', '.mw-gallery-slideshow-img-container.spoiler-image', function (e) {
         if ($(e.target).closest('.oo-ui-buttonElement, button').length) {
         if ($(e.target).closest('.oo-ui-buttonElement, button, a.oo-ui-buttonElement-button').length) {
             return;
             return;
         }
         }


         var $container = $(this).closest('.mw-gallery-slideshow-img-container');
         var $container = $(this);
        var $gallery = $container.closest('ul.gallery');
        var $img = $container.find('img');


         if ($container.hasClass('spoiler-image')) {
         if (!$container.hasClass('revealed')) {
             e.preventDefault();
             e.preventDefault();
             e.stopPropagation();
             e.stopPropagation();


            $img.removeClass('spoiler-image').css('filter', 'none');
             $container.addClass('revealed');
             $container.removeClass('spoiler-image');


             $gallery.find('.gallerybox.slideshow-current').addClass('revealed');
             $container.closest('ul.gallery')
 
                      .find('.gallerybox.slideshow-current')
            if ($gallery.hasClass('spoiler-image')) {
                      .addClass('revealed');
                $gallery.addClass('spoiler-image revealed');
                $gallery.find('.gallerybox.spoiler-image').addClass('revealed');
            }
         }
         }
     });
     });
});
});

Revision as of 18:36, 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('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');
        }
    });
});