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

De Wiki Gla
Ir para navegação Ir para pesquisar
m
m
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 () {
    /**
    * 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, "/");
      }
    }
     /**
     /**
     * @typedef {Object} MediaWiki
     * Sistema de cache persistente usando IndexedDB
    * @property {Object} util
    * @property {Function} util.wikiScript
    * @property {Function} util.wikiUrlencode
    * @property {Object} config
    * @property {Function} config.get
     */
     */
     // @ts-ignore - MediaWiki global
     var BgCache = (function () {
    var mw = window.mw || {};
      var DB_NAME = "character-bg-cache";
      var DB_VERSION = 1;
      var STORE_NAME = "backgrounds";
      var db = null;


    (function () {
      /**
        /**
      * Inicializa o banco de dados IndexedDB
        * Builds a MediaWiki file URL from a filename
      */
        * @param {string} fileName - The filename (with or without "Arquivo:" prefix)
      function initDB() {
        * @returns {string} The full URL to the file
        return new Promise(function (resolve, reject) {
        */
          if (db) {
        function buildBgURL(fileName) {
            resolve(db);
            if (!fileName) return '';
             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, '/');
            }
        }


        /**
          if (!window.indexedDB) {
        * Sistema de cache persistente usando IndexedDB
             reject(new Error("IndexedDB não disponível"));
        */
             return;
        var BgCache = (function () {
          }
             var DB_NAME = 'character-bg-cache';
             var DB_VERSION = 1;
            var STORE_NAME = 'backgrounds';
            var db = null;


            /**
          var request = indexedDB.open(DB_NAME, DB_VERSION);
            * Inicializa o banco de dados IndexedDB
            */
            function initDB() {
                return new Promise(function (resolve, reject) {
                    if (db) {
                        resolve(db);
                        return;
                    }


                    if (!window.indexedDB) {
          request.onerror = function () {
                        reject(new Error('IndexedDB não disponível'));
            reject(request.error);
                        return;
          };
                    }


                    var request = indexedDB.open(DB_NAME, DB_VERSION);
          request.onsuccess = function () {
            db = request.result;
            resolve(db);
          };


                    request.onerror = function () {
          request.onupgradeneeded = function (event) {
                        reject(request.error);
            var database = event.target.result;
                    };
            if (!database.objectStoreNames.contains(STORE_NAME)) {
              database.createObjectStore(STORE_NAME);
            }
          };
        });
      }


                    request.onsuccess = function () {
      /**
                        db = request.result;
      * Obtém imagem do cache
                        resolve(db);
      * @param {string} url - URL da imagem
                    };
      * @returns {Promise<string|null>} URL do blob ou null se não estiver em cache
      */
      function get(url) {
        return initDB()
          .then(function (database) {
            return new Promise(function (resolve, reject) {
              var transaction = database.transaction([STORE_NAME], "readonly");
              var store = transaction.objectStore(STORE_NAME);
              var request = store.get(url);


                    request.onupgradeneeded = function (event) {
              request.onsuccess = function () {
                        var database = event.target.result;
                var blob = request.result;
                        if (!database.objectStoreNames.contains(STORE_NAME)) {
                if (blob && blob instanceof Blob) {
                            database.createObjectStore(STORE_NAME);
                  var blobUrl = URL.createObjectURL(blob);
                        }
                  resolve(blobUrl);
                    };
                } else {
                 });
                  resolve(null);
            }
                 }
              };


            /**
              request.onerror = function () {
            * Obtém imagem do cache
                reject(request.error);
            * @param {string} url - URL da imagem
              };
            * @returns {Promise<string|null>} URL do blob ou null se não estiver em cache
            });
            */
          })
            function get(url) {
          .catch(function () {
                return initDB().then(function (database) {
            return Promise.resolve(null);
                    return new Promise(function (resolve, reject) {
          });
                        var transaction = database.transaction([STORE_NAME], 'readonly');
      }
                        var store = transaction.objectStore(STORE_NAME);
                        var request = store.get(url);


                        request.onsuccess = function () {
      /**
                            var blob = request.result;
      * Salva imagem no cache
                            if (blob && blob instanceof Blob) {
      * @param {string} url - URL da imagem
                                var blobUrl = URL.createObjectURL(blob);
      * @param {Blob} blob - Blob da imagem
                                resolve(blobUrl);
      */
                            } else {
      function set(url, blob) {
                                resolve(null);
        initDB()
                            }
          .then(function (database) {
                        };
            var transaction = database.transaction([STORE_NAME], "readwrite");
            var store = transaction.objectStore(STORE_NAME);
            store.put(blob, url);
          })
          .catch(function () {
            // Falha silenciosa - cache opcional
          });
      }


                        request.onerror = function () {
      /**
                            reject(request.error);
      * Carrega imagem e aplica cache
                        };
      * @param {string} url - URL da imagem
                    });
      * @returns {Promise<string>} URL do blob (do cache ou recém-carregada)
                }).catch(function () {
      */
                    return Promise.resolve(null);
      function loadImage(url) {
                });
        return get(url)
          .then(function (cachedUrl) {
            if (cachedUrl) {
              return Promise.resolve(cachedUrl);
             }
             }


             /**
             // Carrega a imagem e salva no cache
            * Salva imagem no cache
            return fetch(url, { mode: "cors" })
            * @param {string} url - URL da imagem
              .then(function (response) {
            * @param {Blob} blob - Blob da imagem
                if (!response.ok) {
            */
                  throw new Error(
            function set(url, blob) {
                    "Falha ao carregar imagem: " + response.status
                 initDB().then(function (database) {
                  );
                    var transaction = database.transaction([STORE_NAME], 'readwrite');
                }
                    var store = transaction.objectStore(STORE_NAME);
                 return response.blob();
                    store.put(blob, url);
              })
                }).catch(function () {
              .then(function (blob) {
                    // Falha silenciosa - cache opcional
                set(url, blob);
                });
                return URL.createObjectURL(blob);
            }
              });
          })
          .catch(function () {
            // Em caso de erro, retorna a URL original
            return Promise.resolve(url);
          });
      }


            /**
      return {
            * Carrega imagem e aplica cache
        loadImage: loadImage,
            * @param {string} url - URL da imagem
      };
            * @returns {Promise<string>} URL do blob (do cache ou recém-carregada)
    })();
            */
            function loadImage(url) {
                return get(url).then(function (cachedUrl) {
                    if (cachedUrl) {
                        return Promise.resolve(cachedUrl);
                    }


                    // Carrega a imagem e salva no cache
    /**
                    return fetch(url, { mode: 'cors' }).then(function (response) {
    * Applies background image to an element (com cache persistente)
                        if (!response.ok) {
    * @param {HTMLElement} el - The element to apply background to
                            throw new Error('Falha ao carregar imagem: ' + response.status);
    */
                        }
    function applyBg(el) {
                        return response.blob();
      try {
                    }).then(function (blob) {
        var url = el.getAttribute("data-bg-url");
                        set(url, blob);
        if (!url) {
                        return URL.createObjectURL(blob);
          var f = el.getAttribute("data-bg-file");
                    });
          if (f) {
                }).catch(function () {
            url = buildBgURL(f);
                    // Em caso de erro, retorna a URL original
            el.setAttribute("data-bg-url", url);
                    return Promise.resolve(url);
          }
                });
        }
            }


            return {
        if (url) {
                loadImage: loadImage
          // Tenta carregar do cache primeiro
             };
          BgCache.loadImage(url)
        })();
             .then(function (blobUrl) {
              el.style.backgroundImage = 'url("' + blobUrl + '")';
              // Remove o atributo temporário se existir
              el.removeAttribute("data-bg-loading");
            })
            .catch(function () {
              // Fallback para URL original em caso de erro
              el.style.backgroundImage = 'url("' + url + '")';
            });


        /**
          // Aplica URL original imediatamente enquanto carrega do cache (progressive enhancement)
        * Applies background image to an element (com cache persistente)
          if (!el.hasAttribute("data-bg-loading")) {
        * @param {HTMLElement} el - The element to apply background to
            el.setAttribute("data-bg-loading", "1");
        */
            el.style.backgroundImage = 'url("' + url + '")';
        function applyBg(el) {
          }
            try {
        }
                var url = el.getAttribute('data-bg-url');
      } catch (e) {
                if (!url) {
        /* no-op */
                    var f = el.getAttribute('data-bg-file');
      }
                    if (f) {
    }
                        url = buildBgURL(f);
                        el.setAttribute('data-bg-url', url);
                    }
                }


                if (url) {
    // Aplicar backgrounds imediatamente
                    // Tenta carregar do cache primeiro
    document.querySelectorAll("[data-bg-url], [data-bg-file]").forEach(applyBg);
                    BgCache.loadImage(url).then(function (blobUrl) {
                        el.style.backgroundImage = 'url("' + blobUrl + '")';
                        // Remove o atributo temporário se existir
                        el.removeAttribute('data-bg-loading');
                    }).catch(function () {
                        // Fallback para URL original em caso de erro
                        el.style.backgroundImage = 'url("' + url + '")';
                    });


                    // Aplica URL original imediatamente enquanto carrega do cache (progressive enhancement)
    // Apply to future elements (AJAX)
                    if (!el.hasAttribute('data-bg-loading')) {
    new MutationObserver(function (mutations) {
                        el.setAttribute('data-bg-loading', '1');
      mutations.forEach(function (m) {
                        el.style.backgroundImage = 'url("' + url + '")';
        if (m.type === "childList") {
                    }
          m.addedNodes.forEach(function (n) {
                 }
            if (n.nodeType === 1) {
            } catch (e) {
              if (
                 /* no-op */
                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);
         }
         }
 
      });
        // Aplicar backgrounds imediatamente
    }).observe(document.body, {
        document.querySelectorAll('[data-bg-url], [data-bg-file]').forEach(applyBg);
      attributes: true,
 
      attributeFilter: ["data-bg-file", "data-bg-url"],
        // Apply to future elements (AJAX)
      childList: true,
        new MutationObserver(function (mutations) {
      subtree: true,
            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>
</script>

Edição das 14h17min de 22 de janeiro de 2026

<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, "/");
     }
   }
   /**
    * Sistema de cache persistente usando IndexedDB
    */
   var BgCache = (function () {
     var DB_NAME = "character-bg-cache";
     var DB_VERSION = 1;
     var STORE_NAME = "backgrounds";
     var db = null;
     /**
      * Inicializa o banco de dados IndexedDB
      */
     function initDB() {
       return new Promise(function (resolve, reject) {
         if (db) {
           resolve(db);
           return;
         }
         if (!window.indexedDB) {
           reject(new Error("IndexedDB não disponível"));
           return;
         }
         var request = indexedDB.open(DB_NAME, DB_VERSION);
         request.onerror = function () {
           reject(request.error);
         };
         request.onsuccess = function () {
           db = request.result;
           resolve(db);
         };
         request.onupgradeneeded = function (event) {
           var database = event.target.result;
           if (!database.objectStoreNames.contains(STORE_NAME)) {
             database.createObjectStore(STORE_NAME);
           }
         };
       });
     }
     /**
      * Obtém imagem do cache
      * @param {string} url - URL da imagem
      * @returns {Promise<string|null>} URL do blob ou null se não estiver em cache
      */
     function get(url) {
       return initDB()
         .then(function (database) {
           return new Promise(function (resolve, reject) {
             var transaction = database.transaction([STORE_NAME], "readonly");
             var store = transaction.objectStore(STORE_NAME);
             var request = store.get(url);
             request.onsuccess = function () {
               var blob = request.result;
               if (blob && blob instanceof Blob) {
                 var blobUrl = URL.createObjectURL(blob);
                 resolve(blobUrl);
               } else {
                 resolve(null);
               }
             };
             request.onerror = function () {
               reject(request.error);
             };
           });
         })
         .catch(function () {
           return Promise.resolve(null);
         });
     }
     /**
      * Salva imagem no cache
      * @param {string} url - URL da imagem
      * @param {Blob} blob - Blob da imagem
      */
     function set(url, blob) {
       initDB()
         .then(function (database) {
           var transaction = database.transaction([STORE_NAME], "readwrite");
           var store = transaction.objectStore(STORE_NAME);
           store.put(blob, url);
         })
         .catch(function () {
           // Falha silenciosa - cache opcional
         });
     }
     /**
      * Carrega imagem e aplica cache
      * @param {string} url - URL da imagem
      * @returns {Promise<string>} URL do blob (do cache ou recém-carregada)
      */
     function loadImage(url) {
       return get(url)
         .then(function (cachedUrl) {
           if (cachedUrl) {
             return Promise.resolve(cachedUrl);
           }
           // Carrega a imagem e salva no cache
           return fetch(url, { mode: "cors" })
             .then(function (response) {
               if (!response.ok) {
                 throw new Error(
                   "Falha ao carregar imagem: " + response.status
                 );
               }
               return response.blob();
             })
             .then(function (blob) {
               set(url, blob);
               return URL.createObjectURL(blob);
             });
         })
         .catch(function () {
           // Em caso de erro, retorna a URL original
           return Promise.resolve(url);
         });
     }
     return {
       loadImage: loadImage,
     };
   })();
   /**
    * Applies background image to an element (com cache persistente)
    * @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) {
         // Tenta carregar do cache primeiro
         BgCache.loadImage(url)
           .then(function (blobUrl) {
             el.style.backgroundImage = 'url("' + blobUrl + '")';
             // Remove o atributo temporário se existir
             el.removeAttribute("data-bg-loading");
           })
           .catch(function () {
             // Fallback para URL original em caso de erro
             el.style.backgroundImage = 'url("' + url + '")';
           });
         // Aplica URL original imediatamente enquanto carrega do cache (progressive enhancement)
         if (!el.hasAttribute("data-bg-loading")) {
           el.setAttribute("data-bg-loading", "1");
           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>