ഉള്ളടക്കത്തിലേക്ക് പോവുക

മീഡിയവിക്കി:Common.js

Schoolwiki സംരംഭത്തിൽ നിന്ന്
06:16, 12 ജനുവരി 2026-നു ഉണ്ടായിരുന്ന രൂപം സൃഷ്ടിച്ചത്:- Ranjithsiji (സംവാദം | സംഭാവനകൾ)

ശ്രദ്ധിക്കുക: സേവ് ചെയ്തശേഷം മാറ്റങ്ങൾ കാണാനായി താങ്കൾക്ക് ബ്രൗസറിന്റെ കാഷെ ഒഴിവാക്കേണ്ടി വന്നേക്കാം.

  • ഫയർഫോക്സ് / സഫാരി: Reload ബട്ടൺ അമർത്തുമ്പോൾ Shift കീ അമർത്തി പിടിക്കുകയോ, Ctrl-F5 അല്ലെങ്കിൽ Ctrl-R (മാക്കിന്റോഷിൽ ⌘-R ) എന്ന് ഒരുമിച്ച് അമർത്തുകയോ ചെയ്യുക
  • ഗൂഗിൾ ക്രോം: Ctrl-Shift-R (മാക്കിന്റോഷിൽ ⌘-Shift-R ) അമർത്തുക
  • ഇന്റർനെറ്റ് എക്സ്പ്ലോറർ: Refresh ബട്ടൺ അമർത്തുമ്പോൾ Ctrl കീ അമർത്തിപിടിക്കുക. അല്ലെങ്കിൽ Ctrl-F5 അമർത്തുക
  • ഓപ്പറ: Menu → Settings എടുക്കുക (മാക്കിൽ Opera → Preferences) എന്നിട്ട് Privacy & security → Clear browsing data → Cached images and files ചെയ്യുക.
/* ============================================================
   OpenStreetMap marker URL fix
   ============================================================ */
mw.hook('wikipage.content').add(function () {
    var images = document.querySelectorAll('img');
    for (var i = 0; i < images.length; i++) {
        var img = images[i];
        if (
            img.src &&
            img.src.indexOf('https://tile.openstreetmap.org/v4/marker/') === 0
        ) {
            img.src = img.src.replace(
                'https://tile.openstreetmap.org/v4/marker/',
                'https://maps.wikimedia.org/v4/marker/'
            );
        }
    }
});

/* ============================================================
   Wikimedia Commons Gallery Loader (Template-based, MULTILINE SAFE)
   ============================================================ */
mw.hook('wikipage.content').add(function ($content) {

    $content.find('.commons-gallery').each(function () {
        var container = this;

        var sources = container.querySelectorAll('.commons-gallery-source');
        if (!sources.length) return;

        var width = 300;

        var grid = document.createElement('div');
        grid.className = 'commons-gallery-grid';
        grid.style.display = 'grid';
        grid.style.gridTemplateColumns =
            'repeat(auto-fill, minmax(' + width + 'px, 1fr))';
        grid.style.gap = '8px';

        container.appendChild(grid);

        sources.forEach(function (srcEl) {
            var filename = srcEl.dataset.file;
            var captionText = srcEl.dataset.caption || '';

            if (!filename) return;

            var apiUrl =
                'https://commons.wikimedia.org/w/api.php' +
                '?action=query' +
                '&format=json' +
                '&prop=imageinfo' +
                '&iiprop=url|extmetadata' +
                '&iiurlwidth=' + width +
                '&titles=File:' + encodeURIComponent(filename) +
                '&origin=*';

            fetch(apiUrl)
                .then(function (r) {
                    if (!r.ok) throw new Error('HTTP ' + r.status);
                    return r.json();
                })
                .then(function (data) {
                    if (!data || !data.query || !data.query.pages) return;

                    var page =
                        data.query.pages[Object.keys(data.query.pages)[0]];
                    if (!page || !page.imageinfo) return;

                    var info = page.imageinfo[0];
                    var imgUrl = info.thumburl || info.url;

                    var artist = 'Unknown';
                    var license = '';
                    var licenseUrl = '';

                    if (info.extmetadata) {
                        if (info.extmetadata.Artist) {
                            artist = info.extmetadata.Artist.value;
                        }
                        if (info.extmetadata.LicenseShortName) {
                            license = info.extmetadata.LicenseShortName.value;
                        }
                        if (info.extmetadata.LicenseUrl) {
                            licenseUrl = info.extmetadata.LicenseUrl.value;
                        }
                    }

                    var figure = document.createElement('figure');
                    figure.className = 'commons-gallery-item';

                    var img = document.createElement('img');
                    img.src = imgUrl;
                    img.alt = captionText || filename;
                    img.loading = 'lazy';
                    img.style.width = '100%';

                    /* Lightbox data (use responsive size, not original) */
                    var lightboxSrc = info.url;
                    if (info.responsiveUrls) {
                        if (window.devicePixelRatio > 1 && info.responsiveUrls['2']) {
                            lightboxSrc = info.responsiveUrls['2'];
                        } else if (info.responsiveUrls['1.5']) {
                            lightboxSrc = info.responsiveUrls['1.5'];
                        }
                    }

                    img.dataset.fullsrc = lightboxSrc;
                    img.dataset.caption =
                        (captionText ? captionText + '<br>' : '') +
                        '© ' + artist +
                        (licenseUrl
                            ? ' – <a href="' + licenseUrl +
                              '" target="_blank">' + license + '</a>'
                            : '');

                    figure.appendChild(img);

                    var figcaption = document.createElement('figcaption');
                    figcaption.style.fontSize = '0.75em';
                    figcaption.style.textAlign = 'center';
                    figcaption.innerHTML = img.dataset.caption;

                    figure.appendChild(figcaption);
                    grid.appendChild(figure);
                })
                .catch(function (err) {
                    console.warn(
                        'Commons gallery image failed:',
                        filename,
                        err.message
                    );
                });
        });

        /* Hide placeholder spans */
        sources.forEach(function (el) {
            el.style.display = 'none';
        });
    });
});

/* ============================================================
   Commons Gallery Lightbox Viewer (with navigation)
   ============================================================ */
(function () {

    var galleryImages = [];
    var currentIndex = -1;

    function collectGalleryImages() {
        galleryImages = Array.prototype.slice.call(
            document.querySelectorAll('.commons-gallery-item img')
        );
    }

    function createLightbox() {
        if (document.getElementById('commons-lightbox')) return;

        var overlay = document.createElement('div');
        overlay.id = 'commons-lightbox';

        overlay.innerHTML =
            '<div class="clb-backdrop"></div>' +
            '<div class="clb-content">' +
                '<button class="clb-prev" aria-label="Previous">‹</button>' +
                '<button class="clb-next" aria-label="Next">›</button>' +
                '<button class="clb-close" aria-label="Close">×</button>' +
                '<img class="clb-image" alt="">' +
                '<div class="clb-caption"></div>' +
            '</div>';

        document.body.appendChild(overlay);

        overlay.querySelector('.clb-backdrop').onclick = closeLightbox;
        overlay.querySelector('.clb-close').onclick = closeLightbox;
        overlay.querySelector('.clb-prev').onclick = showPrev;
        overlay.querySelector('.clb-next').onclick = showNext;

        document.addEventListener('keydown', function (e) {
            if (!overlay.classList.contains('active')) return;
            if (e.key === 'Escape') closeLightbox();
            if (e.key === 'ArrowLeft') showPrev();
            if (e.key === 'ArrowRight') showNext();
        });
    }

    function openLightbox(img) {
        collectGalleryImages();
        currentIndex = galleryImages.indexOf(img);
        if (currentIndex === -1) return;

        createLightbox();
        showImage(currentIndex);

        document.getElementById('commons-lightbox').classList.add('active');
        document.body.style.overflow = 'hidden';
    }

    function showImage(index) {
        var overlay = document.getElementById('commons-lightbox');
        var lbImg = overlay.querySelector('.clb-image');
        var caption = overlay.querySelector('.clb-caption');

        var img = galleryImages[index];
        if (!img) return;

        lbImg.src = img.dataset.fullsrc || img.src;
        lbImg.alt = img.alt || '';
        caption.innerHTML = img.dataset.caption || '';

        currentIndex = index;
    }

    function showPrev() {
        if (currentIndex > 0) showImage(currentIndex - 1);
    }

    function showNext() {
        if (currentIndex < galleryImages.length - 1) {
            showImage(currentIndex + 1);
        }
    }

    function closeLightbox() {
        var overlay = document.getElementById('commons-lightbox');
        if (!overlay) return;
        overlay.classList.remove('active');
        document.body.style.overflow = '';
    }

    /* Event delegation (single, correct) */
    document.addEventListener('click', function (e) {
        var img = e.target;
        if (img && img.matches('.commons-gallery-item img')) {
            openLightbox(img);
        }
    });

})();
"https://schoolwiki.in/index.php?title=മീഡിയവിക്കി:Common.js&oldid=2928508" എന്ന താളിൽനിന്ന് ശേഖരിച്ചത്