Widget:Character.Background

De Wiki Gla
Revisão de 22h39min de 24 de novembro de 2025 por Gurren1 (discussão | contribs)
Ir para navegação Ir para pesquisar

<script>

   (function () {
       // Cache de imagens pré-carregadas
       const bgPreloadCache = new Map();
       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, '/');
           }
       }
       function preloadAndApplyBg(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) return;
               // Se já está no cache, aplicar imediatamente
               if (bgPreloadCache.has(url)) {
                   el.style.backgroundImage = 'url("' + url + '")';
                   return;
               }
               // Pré-carregar a imagem antes de aplicar
               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
       function preloadAllBackgrounds() {
           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)
       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'))) {
                               preloadAndApplyBg(n);
                           }
                           if (n.querySelectorAll) {
                               n.querySelectorAll('[data-bg-file], [data-bg-url]').forEach(preloadAndApplyBg);
                           }
                       }
                   });
               } else if (m.type === 'attributes' && m.target) {
                   preloadAndApplyBg(m.target);
               }
           });
       }).observe(document.body, {
           attributes: true,
           attributeFilter: ['data-bg-file', 'data-bg-url'],
           childList: true,
           subtree: true,
       });
   })();

</script>