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

Возврат рабочей версии вк
Метка: ручная отмена
Откат правки 362378 участника Тэйтанка-птекила (обс.)
Метка: отмена
 
(не показано 16 промежуточных версий этого же участника)
Строка 1: Строка 1:
/* Нигилистические фокусы (см. шаблон {{Нигилизм}}) */
/* Нигилистические фокусы (см. шаблон {{tl|Нигилизм}}) */
$(function () {
$(function () {
     var nihilism = $('#nihilism');
     var nihilism = $('#nihilism');
Строка 83: Строка 83:
             width = parseInt(width) || 560;
             width = parseInt(width) || 560;
             height = parseInt(height) || 315;
             height = parseInt(height) || 315;
             if (width > 2000) width = 2000;
             if (width > 1920) width = 1920;
             if (height < 100) height = 100;
             if (height < 100) height = 100;
             if (height > 2000) height = 2000;
             if (height > 1080) height = 1080;
              
              
             if (oid && id && oid !== 'null' && id !== 'null' && /^[-_a-zA-Z0-9]+$/.test(oid) && /^[-_a-zA-Z0-9]+$/.test(id)) {
             if (oid && id && oid !== 'null' && id !== 'null' && /^[a-zA-Z0-9_-]{1,100}$/.test(oid) && /^[-_a-zA-Z0-9]+$/.test(id)) {
                 var iframe = document.createElement('iframe');
                 var iframe = document.createElement('iframe');
                 iframe.src = 'https://vk.com/video_ext.php?oid=' + encodeURIComponent(oid) + '&id=' + encodeURIComponent(id);
                 iframe.src = 'https://vk.com/video_ext.php?oid=' + encodeURIComponent(oid) + '&id=' + encodeURIComponent(id);
Строка 97: Строка 97:
                 iframe.sandbox = 'allow-scripts allow-same-origin allow-presentation allow-fullscreen';
                 iframe.sandbox = 'allow-scripts allow-same-origin allow-presentation allow-fullscreen';
                 iframe.allow = 'fullscreen';
                 iframe.allow = 'fullscreen';
                  
                 iframe.referrerPolicy = 'no-referrer';
                iframe.loading = 'lazy';
                 iframe.style.border = 'none';
                 iframe.style.border = 'none';
                  
                  
Строка 106: Строка 107:
             } else {
             } else {
                 var errorDiv = document.createElement('div');
                 var errorDiv = document.createElement('div');
                 errorDiv.style.cssText = 'color: red; padding: 10px; text-align: center; border: 1px solid red;';
               
                 errorDiv.style.color = 'red';
                errorDiv.style.padding = '10px';
                errorDiv.style.textAlign = 'center';
                errorDiv.style.border = '1px solid';
                errorDiv.style.backgroundColor = '#fff';
                errorDiv.style.fontFamily = 'Arial, sans-serif';
                errorDiv.style.fontSize = '14px';
                errorDiv.style.margin = '5px 0';
 
                 errorDiv.textContent = 'Ошибка: неверные параметры видео';
                 errorDiv.textContent = 'Ошибка: неверные параметры видео';
                  
                  
Строка 121: Строка 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', 2000);
            var height = sanitizeDimension(embed.getAttribute('data-height'), '315', 2000);
           
            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() {
                console.error('Rutube iframe failed to load:', videoId);
                if (embed.parentNode) {
                    embed.innerHTML = '<p style="padding: 20px; text-align: center; color: #666;">Не удалось загрузить видео</p>';
                }
            });
            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);
     }
     }
})();
})();