MediaWiki:Common.js: различия между версиями

м для исключения из категорий
Удалено
Строка 131: Строка 131:
     } else {
     } else {
         initVKVideo();
         initVKVideo();
    }
})();
/* Вставка Rutube-видео на страницу для шаблона {{Rutube}} */
(function() {
    'use strict';
    function sanitizeVideoId(videoId) {
        if (typeof videoId !== 'string') return null;
        return videoId.match(/^[a-zA-Z0-9_-]+$/) ? videoId : null;
    }
   
    function sanitizeDimension(dim, defaultValue, maxValue) {
        if (typeof dim !== 'string') return defaultValue;
        const cleanDim = dim.match(/^(100|[1-9]\d?)%$|^\d+$/) ? dim : defaultValue;
   
        if (cleanDim.match(/^\d+$/)) {
        const numValue = parseInt(cleanDim, 10);
        return (numValue >= 100 && numValue <= maxValue) ? cleanDim : defaultValue;
    }
   
    return cleanDim;
}
   
    function initRutubeEmbeds() {
        var embeds = document.querySelectorAll('.rutube-video-embed:not([data-processed])');
       
        embeds.forEach(function(embed) {
            var rawVideoId = embed.getAttribute('data-video-id');
            var videoId = sanitizeVideoId(rawVideoId);
           
        if (!videoId) {
            console.warn('Invalid Rutube video ID format');
            embed.setAttribute('data-processed', 'true');
            return;
        }
           
            var width = sanitizeDimension(embed.getAttribute('data-width'), '560', 1920);
            var height = sanitizeDimension(embed.getAttribute('data-height'), '315', 1080);
           
            var iframe = document.createElement('iframe');
           
            iframe.src = 'https://rutube.ru/play/embed/' + encodeURIComponent(videoId);
            iframe.width = width;
            iframe.height = height;
            iframe.frameBorder = '0';
            iframe.allow = 'autoplay; fullscreen; picture-in-picture';
            iframe.allowFullscreen = true;
            iframe.style.border = 'none';
            iframe.style.display = 'block';
            iframe.style.maxWidth = '100%';
           
            iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin');
            iframe.setAttribute('referrerpolicy', 'strict-origin-when-cross-origin');
            iframe.setAttribute('loading', 'lazy');
            iframe.setAttribute('importance', 'low');
            iframe.addEventListener('error', function() {
                clearTimeout(loadTimeout);
                console.error('Rutube iframe failed to load:', videoId);
                if (embed.parentNode && iframe.parentNode === embed) {
                    const errorMsg = document.createElement('p');
                    errorMsg.textContent = 'Не удалось загрузить видео';
                    errorMsg.style.padding = '20px';
                    errorMsg.style.textAlign = 'center';
                    errorMsg.style.color = '#666';
                    embed.replaceChild(errorMsg, iframe);
                }
            });
            while (embed.firstChild) {
                embed.removeChild(embed.firstChild);
            }
           
            embed.appendChild(iframe);
            embed.setAttribute('data-processed', 'true');
        });
    }
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initRutubeEmbeds);
    } else {
        initRutubeEmbeds();
    }
   
    if (typeof mw !== 'undefined' && mw.hook) {
        mw.hook('wikipage.content').add(initRutubeEmbeds);
     }
     }
})();
})();