اطلاعیه: از همه ایران دوستان عزیز دعوت می‌کنیم در ایجاد و به‌روزرسانی صفحات جاویدنامان همیاری نمایند. در حال حاضر میتوانید بدون عضویت در ثبت تاریخ کمک نمایید.

مدیاویکی:Common.js: تفاوت میان نسخه‌ها

از ویکی‌یاد
Adminwki (بحث | مشارکت‌ها)
بدون خلاصۀ ویرایش
Adminwki (بحث | مشارکت‌ها)
بدون خلاصۀ ویرایش
خط ۱: خط ۱:
/**
/**
  * Gallery to background-cover: set img src as background on .thumb div + hide img
  * Gallery to background-cover: set img src as background on .thumb div + hide img
* → ONLY on Category: pages (not articles or other namespaces)
  */
  */
(function () {
(function () {
    'use strict';
    // Early exit if not on a category page
    if (mw.config.get('wgCanonicalNamespace') !== 'Category') {
        return;
    }
     function processGalleries() {
     function processGalleries() {
         document.querySelectorAll('li.gallerybox').forEach(function (box) {
         document.querySelectorAll('li.gallerybox').forEach(function (box) {
خط ۸: خط ۱۶:
             const thumbDiv = box.querySelector('div.thumb');
             const thumbDiv = box.querySelector('div.thumb');
             const img = thumbDiv ? thumbDiv.querySelector('img.mw-file-element') : null;
             const img = thumbDiv ? thumbDiv.querySelector('img.mw-file-element') : null;
           
 
             if (thumbDiv && img && img.src && !thumbDiv.dataset.backgroundApplied) {
             if (thumbDiv && img && img.src && !thumbDiv.dataset.backgroundApplied) {
                 // Apply background to the .thumb div
                 // Apply background to the .thumb div
خط ۱۵: خط ۲۳:
                 thumbDiv.style.backgroundPosition = 'center center';
                 thumbDiv.style.backgroundPosition = 'center center';
                 thumbDiv.style.backgroundRepeat = 'no-repeat';
                 thumbDiv.style.backgroundRepeat = 'no-repeat';
               
 
                 // Hide the original <img> tag
                 // Hide the original <img> tag
                 img.style.display = 'none';
                 img.style.display = 'none';
               
 
                 // Mark as processed
                 // Mark as processed (prevents re-processing the same element)
                 thumbDiv.dataset.backgroundApplied = 'true';
                 thumbDiv.dataset.backgroundApplied = 'true';
               
 
                 console.log('Applied background to thumb div:', img.src);
                 console.log('Applied background to thumb div on category page:', img.src);
             }
             }
         });
         });
خط ۳۰: خط ۳۸:
     document.addEventListener('DOMContentLoaded', processGalleries);
     document.addEventListener('DOMContentLoaded', processGalleries);


     // Watch for dynamic additions (DPL may insert later)
     // Watch for dynamic content additions (very useful for DPL, CategoryTree, transclusions, AJAX-loaded galleries, etc.)
     const observer = new MutationObserver(processGalleries);
     const observer = new MutationObserver(processGalleries);
     observer.observe(document.body, { childList: true, subtree: true });
     observer.observe(document.body, { childList: true, subtree: true });


     // Extra fallback runs
     // Extra fallback runs (in case observer misses something or images load very late)
     setTimeout(processGalleries, 1000);
     setTimeout(processGalleries, 1000);
     setTimeout(processGalleries, 2500);
     setTimeout(processGalleries, 2500);
    setTimeout(processGalleries, 5000);  // one more — harmless and helps on slow connections
})();
})();

نسخهٔ ۲۶ ژانویهٔ ۲۰۲۶، ساعت ۱۷:۰۱

/**
 * Gallery to background-cover: set img src as background on .thumb div + hide img
 * → ONLY on Category: pages (not articles or other namespaces)
 */
(function () {
    'use strict';

    // Early exit if not on a category page
    if (mw.config.get('wgCanonicalNamespace') !== 'Category') {
        return;
    }

    function processGalleries() {
        document.querySelectorAll('li.gallerybox').forEach(function (box) {
            // Find the .thumb div and the img inside it
            const thumbDiv = box.querySelector('div.thumb');
            const img = thumbDiv ? thumbDiv.querySelector('img.mw-file-element') : null;

            if (thumbDiv && img && img.src && !thumbDiv.dataset.backgroundApplied) {
                // Apply background to the .thumb div
                thumbDiv.style.backgroundImage = 'url(' + img.src + ')';
                thumbDiv.style.backgroundSize = 'cover';
                thumbDiv.style.backgroundPosition = 'center center';
                thumbDiv.style.backgroundRepeat = 'no-repeat';

                // Hide the original <img> tag
                img.style.display = 'none';

                // Mark as processed (prevents re-processing the same element)
                thumbDiv.dataset.backgroundApplied = 'true';

                console.log('Applied background to thumb div on category page:', img.src);
            }
        });
    }

    // Run once DOM is ready
    document.addEventListener('DOMContentLoaded', processGalleries);

    // Watch for dynamic content additions (very useful for DPL, CategoryTree, transclusions, AJAX-loaded galleries, etc.)
    const observer = new MutationObserver(processGalleries);
    observer.observe(document.body, { childList: true, subtree: true });

    // Extra fallback runs (in case observer misses something or images load very late)
    setTimeout(processGalleries, 1000);
    setTimeout(processGalleries, 2500);
    setTimeout(processGalleries, 5000);  // one more — harmless and helps on slow connections
})();