Jump to content

User:Epicgenius/hideimages.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.loader.using('mediawiki.util').then(function () {
    var hidden = false;

    function toggleImages() {
        document.querySelectorAll('img').forEach(function (img) {
            img.style.visibility = hidden ? '' : 'hidden';
        });

        hidden = !hidden;

        var link = document.getElementById('toggle-images-link');
        if (link) {
            link.textContent = hidden ? 'Show images' : 'Hide images';
        }
    }

    $(function () {
        var portlet = mw.util.addPortletLink(
            'p-tb',
            '#',
            'Hide images',
            'toggle-images-link',
            'Show or hide all images on this page'
        );

        if (portlet) {
            $(portlet).on('click', function (event) {
                event.preventDefault();
                toggleImages();
            });
        }
    });
});