Widget:Character.Background
Ir para navegação
Ir para pesquisar
<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>