MediaWiki:Common.js
Nota: Após publicar, você pode ter que limpar o "cache" do seu navegador para ver as alterações.
- Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
- Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
- Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5.
- Opera: Pressione Ctrl-F5.
/* Códigos JavaScript aqui colocados serão carregados por todos aqueles que acessarem alguma página deste wiki */
/* Ocultar links para páginas especiais de usuários não registrados */
$(function () {
// Variável para controlar se já escondeu
let searchHidden = false;
// Função principal para ocultar elementos
const hideElementsForAnonymous = function() {
// Se já está logado, não fazer nada
if (mw.config.get('wgUserName') !== null) {
return;
}
// Se já escondeu, verificar se elementos reapareceram
if (searchHidden) {
// Verificar se algum elemento de busca está visível
const searchVisible = $('#p-search').is(':visible') ||
$('.vector-search-box').is(':visible') ||
$('#searchInput').is(':visible');
if (!searchVisible) {
return; // Já está oculto, não precisa fazer nada
}
}
// 1. Ocultar páginas especiais da navegação
$('#n-specialpages').hide();
// 2. Ocultar TODAS as variações possíveis de busca
const searchSelectors = [
'#p-search',
'#searchform',
'#searchInput',
'#simpleSearch',
'.mw-search',
'.vector-search-box',
'.vector-search-box-input',
'.vector-search-box-form',
'.vector-search-box-vue',
'.cdx-search-input',
'.cdx-text-input__input',
'[id*="search"]',
'[class*="search"]',
'input[name="search"]',
'.vector-header-end .vector-search-box',
'.vector-header-start .vector-search-box'
];
// Aplicar a cada seletor
searchSelectors.forEach(selector => {
try {
$(selector).each(function() {
$(this).hide();
$(this).css({
'display': 'none !important',
'visibility': 'hidden !important'
});
});
} catch (e) {
console.log('Erro com seletor:', selector, e);
}
});
// 3. Marcar como escondido
searchHidden = true;
// 4. Adicionar classe ao body para controle via CSS
$('body').addClass('anonymous-user');
};
// Executar imediatamente
hideElementsForAnonymous();
// Executar novamente após carregamento completo
$(window).on('load', hideElementsForAnonymous);
// Executar em intervalos curtos inicialmente
let intervalCount = 0;
const initialInterval = setInterval(() => {
hideElementsForAnonymous();
intervalCount++;
if (intervalCount > 10) { // Parar após 10 execuções (5 segundos)
clearInterval(initialInterval);
}
}, 500);
// Observar mudanças no DOM de forma mais agressiva
const observer = new MutationObserver(function(mutations) {
let shouldHide = false;
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length > 0) {
// Verificar se algum nó adicionado contém elementos de busca
$(mutation.addedNodes).each(function() {
if ($(this).find(searchSelectors.join(',')).length > 0 ||
$(this).is(searchSelectors.join(','))) {
shouldHide = true;
}
});
}
});
if (shouldHide) {
hideElementsForAnonymous();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['style', 'class', 'id']
});
// Monitorar cliques em qualquer link, especialmente "ENTRAR"
$(document).on('click', 'a', function(e) {
const href = $(this).attr('href') || '';
if (href.includes('Special:UserLogin') ||
href.includes('title=Special:UserLogin') ||
$(this).text().toLowerCase().includes('entrar') ||
$(this).attr('id') === 'pt-login') {
// Antes do redirecionamento, garantir que elementos estejam ocultos
setTimeout(hideElementsForAnonymous, 100);
setTimeout(hideElementsForAnonymous, 500);
}
});
// Monitorar mudanças na URL (para quando clicar em ENTRAR)
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
if (url.includes('Special:UserLogin')) {
setTimeout(hideElementsForAnonymous, 100);
setTimeout(hideElementsForAnonymous, 500);
}
}
}).observe(document, { subtree: true, childList: true });
});