Jump to content

User:Ainali/blur.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ainali (talk | contribs) at 08:16, 15 July 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// Blur eligible images
function blurAllImages() {
    document.querySelectorAll('img').forEach(img => {
        // Skip if already processed
        if (img.dataset.blurProcessed) return;

        // Mark as processed
        img.dataset.blurProcessed = 'true';

        // Skip SVGs
        if (img.src && img.src.toLowerCase().endsWith('.svg')) return;

        // Skip small images (less than 50px in width or height)
        const w = img.naturalWidth || img.width;
        const h = img.naturalHeight || img.height;
        if (w < 50 || h < 50) return;

        // Otherwise, blur it
        img.classList.add('blurred-image');
    });
}

// Add CSS style for blurred images
mw.util.addCSS(`
    .blurred-image {
        filter: blur(20px);
        transition: filter 0.1s ease;
    }
    .blurred-image.unblurred {
        filter: none;
    }
`);

// Blur initially
blurAllImages();

// Use event delegation to handle clicks
document.addEventListener('click', function(event) {
    const img = event.target.closest('img.blurred-image');
    if (img) {
        img.classList.toggle('unblurred');
    }
});

// Handle dynamically loaded content
const observer = new MutationObserver(blurAllImages);
observer.observe(document.body, { childList: true, subtree: true });