Mudanças entre as edições de "Widget:Character.Background"

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
 
(Uma revisão intermediária pelo mesmo usuário não está sendo mostrada)
Linha 3: Linha 3:
     =========================== -->
     =========================== -->
<script>
<script>
    /**
    * @typedef {Object} MediaWiki
    * @property {Object} util
    * @property {Function} util.wikiScript
    * @property {Function} util.wikiUrlencode
    * @property {Object} config
    * @property {Function} config.get
    */
    // @ts-ignore - MediaWiki global
    var mw = window.mw || {};
     (function () {
     (function () {
         // Cache de imagens pré-carregadas
         /**
        const bgPreloadCache = new Map();
        * Builds a MediaWiki file URL from a filename
 
        * @param {string} fileName - The filename (with or without "Arquivo:" prefix)
        * @returns {string} The full URL to the file
        */
         function buildBgURL(fileName) {
         function buildBgURL(fileName) {
             if (!fileName) return '';
             if (!fileName) return '';
Linha 20: Linha 33:
         }
         }


         function preloadAndApplyBg(el) {
        /**
        * Applies background image to an element
        * @param {HTMLElement} el - The element to apply background to
        */
         function applyBg(el) {
             try {
             try {
                 var url = el.getAttribute('data-bg-url');
                 var url = el.getAttribute('data-bg-url');
Linha 30: Linha 47:
                     }
                     }
                 }
                 }
                 if (!url) return;
                 if (url) {
 
                // Se já está no cache, aplicar imediatamente
                if (bgPreloadCache.has(url)) {
                     el.style.backgroundImage = 'url("' + url + '")';
                     el.style.backgroundImage = 'url("' + url + '")';
                    return;
                 }
                 }
 
             } catch (e) {
                // Pré-carregar a imagem antes de aplicar
                /* no-op */
                const img = new Image();
            }
                img.onload = function () {
                    bgPreloadCache.set(url, true);
                    el.style.backgroundImage = 'url("' + url + '")';
                };
                img.onerror = function () {
                    // Mesmo com erro, tenta aplicar
                    el.style.backgroundImage = 'url("' + url + '")';
                };
                img.src = url;
             } catch (e) { /* no-op */ }
         }
         }


         // Pré-carregar backgrounds imediatamente ao encontrar elementos
         // Aplicar backgrounds imediatamente
        function preloadAllBackgrounds() {
         document.querySelectorAll('[data-bg-url], [data-bg-file]').forEach(applyBg);
            document.querySelectorAll('[data-bg-file]').forEach(function (el) {
                var f = el.getAttribute('data-bg-file');
                if (!f) return;
                var url = buildBgURL(f);
                if (bgPreloadCache.has(url)) return;
 
                // Inicia o download imediatamente
                var img = new Image();
                img.onload = function () {
                    bgPreloadCache.set(url, true);
                };
                img.src = url;
            });
        }
 
        // Executar preload imediatamente
        preloadAllBackgrounds();
 
        // Aplicar backgrounds
         document.querySelectorAll('[data-bg-url], [data-bg-file]').forEach(preloadAndApplyBg);


         // Apply to future elements (AJAX)
         // Apply to future elements (AJAX)
Linha 82: Linha 65:
                         if (n.nodeType === 1) {
                         if (n.nodeType === 1) {
                             if (n.hasAttribute && (n.hasAttribute('data-bg-file') || n.hasAttribute('data-bg-url'))) {
                             if (n.hasAttribute && (n.hasAttribute('data-bg-file') || n.hasAttribute('data-bg-url'))) {
                                 preloadAndApplyBg(n);
                                 applyBg(n);
                             }
                             }
                             if (n.querySelectorAll) {
                             if (n.querySelectorAll) {
                                 n.querySelectorAll('[data-bg-file], [data-bg-url]').forEach(preloadAndApplyBg);
                                 n.querySelectorAll('[data-bg-file], [data-bg-url]').forEach(applyBg);
                             }
                             }
                         }
                         }
                     });
                     });
                 } else if (m.type === 'attributes' && m.target) {
                 } else if (m.type === 'attributes' && m.target) {
                     preloadAndApplyBg(m.target);
                     applyBg(m.target);
                 }
                 }
             });
             });

Edição atual tal como às 11h56min de 4 de dezembro de 2025

<script>

   /**
    * @typedef {Object} MediaWiki
    * @property {Object} util
    * @property {Function} util.wikiScript
    * @property {Function} util.wikiUrlencode
    * @property {Object} config
    * @property {Function} config.get
    */
   // @ts-ignore - MediaWiki global
   var mw = window.mw || {};
   (function () {
       /**
        * Builds a MediaWiki file URL from a filename
        * @param {string} fileName - The filename (with or without "Arquivo:" prefix)
        * @returns {string} The full URL to the file
        */
       function buildBgURL(fileName) {
           if (!fileName) return ;
           var base = (window.mw && mw.util && typeof mw.util.wikiScript === 'function')
               ? mw.util.wikiScript()
               : (window.mw && mw.config ? (mw.config.get('wgScript') || '/index.php') : '/index.php');
           var path = 'Especial:FilePath/' + fileName.replace(/^Arquivo:|^File:/, );
           if (window.mw && mw.util && typeof mw.util.wikiUrlencode === 'function') {
               return base + '?title=' + mw.util.wikiUrlencode(path);
           } else {
               return base + '?title=' + encodeURIComponent(path).replace(/%2F/g, '/');
           }
       }
       /**
        * Applies background image to an element
        * @param {HTMLElement} el - The element to apply background to
        */
       function applyBg(el) {
           try {
               var url = el.getAttribute('data-bg-url');
               if (!url) {
                   var f = el.getAttribute('data-bg-file');
                   if (f) {
                       url = buildBgURL(f);
                       el.setAttribute('data-bg-url', url);
                   }
               }
               if (url) {
                   el.style.backgroundImage = 'url("' + url + '")';
               }
           } catch (e) {
               /* no-op */
           }
       }
       // Aplicar backgrounds imediatamente
       document.querySelectorAll('[data-bg-url], [data-bg-file]').forEach(applyBg);
       // Apply to future elements (AJAX)
       new MutationObserver(function (mutations) {
           mutations.forEach(function (m) {
               if (m.type === 'childList') {
                   m.addedNodes.forEach(function (n) {
                       if (n.nodeType === 1) {
                           if (n.hasAttribute && (n.hasAttribute('data-bg-file') || n.hasAttribute('data-bg-url'))) {
                               applyBg(n);
                           }
                           if (n.querySelectorAll) {
                               n.querySelectorAll('[data-bg-file], [data-bg-url]').forEach(applyBg);
                           }
                       }
                   });
               } else if (m.type === 'attributes' && m.target) {
                   applyBg(m.target);
               }
           });
       }).observe(document.body, {
           attributes: true,
           attributeFilter: ['data-bg-file', 'data-bg-url'],
           childList: true,
           subtree: true,
       });
   })();

</script>