Mudanças entre as edições de "Widget:Character.Background"
Ir para navegação
Ir para pesquisar
(Criou página com '<!-- =========================== BACKGROUND SYSTEM =========================== --> <script> (function () { function applyBg(el) { try {...') |
m |
||
| Linha 4: | Linha 4: | ||
<script> | <script> | ||
(function () { | (function () { | ||
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 { | try { | ||
var url = el.getAttribute('data-bg-url'); | var url = el.getAttribute('data-bg-url'); | ||
| Linha 10: | Linha 26: | ||
var f = el.getAttribute('data-bg-file'); | var f = el.getAttribute('data-bg-file'); | ||
if (f) { | if (f) { | ||
url = buildBgURL(f); | |||
el.setAttribute('data-bg-url', url); | el.setAttribute('data-bg-url', url); | ||
} | } | ||
} | } | ||
if (url) { | if (!url) return; | ||
// Se já está no cache, aplicar imediatamente | |||
if (bgPreloadCache.has(url)) { | |||
el.style.backgroundImage = 'url("' + url + '")'; | el.style.backgroundImage = 'url("' + url + '")'; | ||
return; | |||
} | } | ||
} catch (e) { /* no-op */ | |||
} | // 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; | |||
}); | |||
} | } | ||
document.querySelectorAll('[data-bg-url], [data-bg-file]').forEach( | // 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) | ||
new MutationObserver((mutations) | new MutationObserver(function (mutations) { | ||
mutations.forEach((m) | mutations.forEach(function (m) { | ||
if (m.type === 'childList') { | if (m.type === 'childList') { | ||
m.addedNodes.forEach((n) | m.addedNodes.forEach(function (n) { | ||
if (n.nodeType === 1) { | 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) { | } else if (m.type === 'attributes' && m.target) { | ||
preloadAndApplyBg(m.target); | |||
} | } | ||
}); | }); | ||
}).observe(document.body, { | }).observe(document.body, { | ||
attributes: true, | attributes: true, | ||
attributeFilter: ['data-bg-file', 'data-bg-url'], | |||
childList: true, | childList: true, | ||
subtree: true, | subtree: true, | ||
Edição das 22h39min de 24 de novembro de 2025
<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>