مدیاویکی:Common.js: تفاوت میان نسخهها
از ویکییاد
جزبدون خلاصۀ ویرایش |
بدون خلاصۀ ویرایش |
||
| خط ۳۱: | خط ۳۱: | ||
const thumbDiv = box.querySelector('div.thumb'); | const thumbDiv = box.querySelector('div.thumb'); | ||
if (!thumbDiv) return; | if (!thumbDiv) return; | ||
// Quick skip: if this thumb is a known broken/placeholder structure | |||
if (thumbDiv.querySelector('.mw-broken-media, .mw-file-element:not(img), span[typeof="mw:Error"]')) { | |||
return; // broken file link → leave untouched | |||
} | |||
const img = thumbDiv.querySelector('img.mw-file-element'); | const img = thumbDiv.querySelector('img.mw-file-element'); | ||
if (!img) return; | if (!img) return; | ||
const src = img.src && img.src.trim(); | const src = img.src && img.src.trim(); | ||
if (!src || src === '' || src.includes('brokenimage') || src.includes('noimage')) { | if (!src || src === '' || | ||
src.includes('brokenimage') || | |||
src.includes('noimage') || | |||
src.includes('Special:Redirect/file/') || // sometimes broken thumbs use redirect | |||
img.alt === '' || img.alt === null) { // empty alt often = placeholder | |||
return; | return; | ||
} | } | ||
// | // Skip tiny/unloaded images (common for broken ones) | ||
if (img.naturalWidth <= 4 || img.naturalHeight <= 4 || img.complete === false) { | |||
if (img.naturalWidth <= | |||
return; | return; | ||
} | } | ||
// | // Already processed? | ||
if (thumbDiv.dataset.backgroundApplied) { | if (thumbDiv.dataset.backgroundApplied) { | ||
return; | return; | ||
| خط ۵۸: | خط ۶۵: | ||
thumbDiv.style.backgroundRepeat = 'no-repeat'; | thumbDiv.style.backgroundRepeat = 'no-repeat'; | ||
// Hide original | // Hide original img | ||
img.style.display = 'none'; | img.style.display = 'none'; | ||
// Mark as | // Mark as processed | ||
thumbDiv.dataset.backgroundApplied = 'true'; | thumbDiv.dataset.backgroundApplied = 'true'; | ||
}); | }); | ||
} | } | ||
// | // Run when DOM is ready | ||
function init() { | |||
processGalleries(); | |||
} | |||
if (document.readyState !== 'loading') { | if (document.readyState !== 'loading') { | ||
init(); | |||
} else { | } else { | ||
document.addEventListener('DOMContentLoaded', | document.addEventListener('DOMContentLoaded', init); | ||
} | } | ||
// | // Mutation observer for dynamic additions (DPL, ajax, category tree, etc.) | ||
const observer = new MutationObserver(processGalleries); | const observer = new MutationObserver(processGalleries); | ||
observer.observe(document.body, { | observer.observe(document.body, { childList: true, subtree: true }); | ||
// Extra | // Extra delayed runs for late-loading thumbs / lazy images | ||
setTimeout(processGalleries, | setTimeout(processGalleries, 600); | ||
setTimeout(processGalleries, | setTimeout(processGalleries, 1500); | ||
setTimeout(processGalleries, | setTimeout(processGalleries, 3500); | ||
})(); | })(); | ||
نسخهٔ ۹ فوریهٔ ۲۰۲۶، ساعت ۱۵:۲۳
/**
* Umami Analytics tracking (privacy-focused, self-hosted)
*/
(function() {
// Create the script element
var script = document.createElement('script');
// Set attributes exactly as provided by Umami
script.defer = true;
script.src = 'https://cloud.umami.is/script.js';
script.setAttribute('data-website-id', 'f5f75b33-a7ed-4d71-ac5a-7750a48efaa0');
// Append to head (or body – head is preferred for earlier loading)
document.head.appendChild(script);
})();
/**
* 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) {
const thumbDiv = box.querySelector('div.thumb');
if (!thumbDiv) return;
// Quick skip: if this thumb is a known broken/placeholder structure
if (thumbDiv.querySelector('.mw-broken-media, .mw-file-element:not(img), span[typeof="mw:Error"]')) {
return; // broken file link → leave untouched
}
const img = thumbDiv.querySelector('img.mw-file-element');
if (!img) return;
const src = img.src && img.src.trim();
if (!src || src === '' ||
src.includes('brokenimage') ||
src.includes('noimage') ||
src.includes('Special:Redirect/file/') || // sometimes broken thumbs use redirect
img.alt === '' || img.alt === null) { // empty alt often = placeholder
return;
}
// Skip tiny/unloaded images (common for broken ones)
if (img.naturalWidth <= 4 || img.naturalHeight <= 4 || img.complete === false) {
return;
}
// Already processed?
if (thumbDiv.dataset.backgroundApplied) {
return;
}
// Apply background
thumbDiv.style.backgroundImage = 'url(' + src + ')';
thumbDiv.style.backgroundSize = 'cover';
thumbDiv.style.backgroundPosition = 'center center';
thumbDiv.style.backgroundRepeat = 'no-repeat';
// Hide original img
img.style.display = 'none';
// Mark as processed
thumbDiv.dataset.backgroundApplied = 'true';
});
}
// Run when DOM is ready
function init() {
processGalleries();
}
if (document.readyState !== 'loading') {
init();
} else {
document.addEventListener('DOMContentLoaded', init);
}
// Mutation observer for dynamic additions (DPL, ajax, category tree, etc.)
const observer = new MutationObserver(processGalleries);
observer.observe(document.body, { childList: true, subtree: true });
// Extra delayed runs for late-loading thumbs / lazy images
setTimeout(processGalleries, 600);
setTimeout(processGalleries, 1500);
setTimeout(processGalleries, 3500);
})();
