MediaWiki:Gadget-Busca aprimorada.js

Origem: Wikilivros, livros abertos por um mundo aberto.

Nota: Depois de publicar, poderá ter de contornar a 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)
  • Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
/** Busca Aprimorada
 * Baseado em: [[:w:en:MediaWiki:Gadget-externalsearch.js]] (versão acessada em 26/02/2008)
 * Autor: [[:b:pt:User:Helder.wiki]]
 * Descrição: Acrescenta 3 menus drop-down na página [[Especial:Search]], permitindo escolher
 *            o wikiprojeto, o idioma e o mecanismo de busca utilizado na pesquisa.
 */

if ('Search' === mw.config.get('wgCanonicalSpecialPageName')) {
	$(BuscaAprimorada);
}

var caixa = [{
	'nome': 'Idioma',
	'item': ['pt', 'en', 'es', 'fi', 'fr', 'it', 'la', 'ja', 'pl', 'nl', 'no', 'sv', 'ru', 'zh', 'meta', 'commons', 'species', 'www']
}, {
	'nome': 'Projeto',
	'item': ['wikibooks', 'mediawiki', 'wikimedia', 'wikinews', 'wikipedia', 'wikiquote', 'wikisource', 'wikiversity', 'wiktionary'],
	'Wikiwix': ['book', 'mediawiki', 'commons', 'news', '', 'quote', 'source', 'versity', 'dict']
}, {
	'nome': 'Buscador',
	'item': ['Padrão', 'Google', 'Yahoo', 'Bing', 'Wikiwix', 'Exalead']
}, {
	'nome': 'Param',
	'item': [
		[mw.config.get('wgServer') + mw.config.get('wgScript'), 'search', 'title', 'Special:Search'],
		['//www.google.com/search', 'q', 'sitesearch', mw.config.get('wgServer')],
		['http://search.yahoo.com/search', 'p', 'vs', mw.config.get('wgServer')],
		['http://www.bing.com/search', 'q', 'q1', 'site:' + mw.config.get('wgServer')],
		['http://www.wikiwix.com/index.php', 'action', 'lang', 'pt'],
		['http://www.exalead.com/search/results', 'q', 'language', 'site:' + mw.config.get('wgServer')]
	]
}];



var CaixaUrl = [];

function BuscaAprimorada() {
	var FormBusca = document.forms.search || document.forms.powersearch;
	if (!FormBusca) {
		return false;
	}
	var searchBox = FormBusca.lsearchbox || FormBusca.search;
	if (!searchBox) {
		return false;
	}

	var CriaCaixa = function (nome, itens) {
		NovaCaixa = document.createElement('select');
		NovaCaixa.id = 'cx' + nome;
		for (var i = 0; i < itens.length; i++) {
			var op = document.createElement('option');
			op.appendChild(document.createTextNode(itens[i]));
			NovaCaixa.appendChild(op);
		}
		return NovaCaixa;
	};

	searchBox.style.marginLeft = '0px'; //* Originalmente o valor é '25%'
	var BtnPesquisa = document.getElementById('loadStatus') || FormBusca.title;
	var N = BtnPesquisa.parentNode;
	//Cria e posiciona cada caixa de seleção imediatamente antes do botão de pesquisa
	for (var i = 0; i < 3; i++) {
		CaixaUrl[i] = CriaCaixa(caixa[i].nome, caixa[i].item);
		N.insertBefore(CaixaUrl[i], BtnPesquisa);
	}
	FormBusca.onsubmit = function () {
		var idxIdSel = document.getElementById('cxIdioma').selectedIndex;
		var idxProjSel = document.getElementById('cxProjeto').selectedIndex;
		var idxBuscSel = document.getElementById('cxBuscador').selectedIndex;

		var IdSel = caixa[0].item[idxIdSel];
		var ProjSel = caixa[1].item[idxProjSel];
		var BuscSel = caixa[2].item[idxBuscSel];
		var ParamSel = caixa[3].item[idxBuscSel];

		var u = '//' + IdSel + '.' + ProjSel + '.org';

		switch (BuscSel) {
		case 'Padrão':
			ParamSel[0] = u + mw.config.get('wgScript');
			break;
		case 'Bing':
			u = 'site:' + u;
			break;
		case 'Wikiwix':
			u = IdSel;
			FormBusca.ns0.value = caixa[1].Wikiwix[idxProjSel];
			FormBusca.ns0.name = 'disp';
			if ('wikimedia' === ProjSel && 'commons' === IdSel) {
				FormBusca.ns1.value = 'true';
				FormBusca.ns1.name = 'img';
			}
			break;
		case 'Exalead':
			u = 'site:' + u;
			searchBox.value += ' ' + u;
			break;
		}
		if ('Padrão' !== BuscSel) {
			ParamSel[3] = u;
		}

		FormBusca.action = ParamSel[0];
		searchBox.name = ParamSel[1];
		FormBusca.title.value = ParamSel[3];
		FormBusca.title.name = ParamSel[2];

	};

	CaixaUrl[0].onchange = function () {
		var IdSel = caixa[0].item[document.getElementById('cxIdioma').selectedIndex];
		var ProjSel = caixa[1].item[document.getElementById('cxProjeto').selectedIndex];
		switch (IdSel) {
		case 'meta':
		case 'commons':
		case 'species':
			document.getElementById('cxProjeto').selectedIndex = 2;
			break; //wikimedia
		case 'www':
			document.getElementById('cxProjeto').selectedIndex = 1;
			break; //mediawiki
		default:
			switch (IdSel) {
			case 'wikimedia':
			case 'mediawiki':
				document.getElementById('cxProjeto').selectedIndex = 0;
				break; //wikibooks
			}
		}
	};

	CaixaUrl[1].onchange = function () {
		var IdSel = caixa[0].item[document.getElementById('cxIdioma').selectedIndex];
		var ProjSel = caixa[1].item[document.getElementById('cxProjeto').selectedIndex];
		switch (ProjSel) {
		case 'wikimedia':
			document.getElementById('cxIdioma').selectedIndex = caixa[0].item.length - 3;
			break; //commons
		case 'mediawiki':
			document.getElementById('cxIdioma').selectedIndex = caixa[0].item.length - 1;
			break; //www
		default:
			switch (IdSel) {
			case 'meta':
			case 'commons':
			case 'species':
			case 'www':
				document.getElementById('cxIdioma').selectedIndex = 0;
				break; //pt
			}
		}
	};
}

// [[Categoria:Gadgets|Busca aprimorada.js]]
// [[Categoria:Scripts do Wikilivros|Busca aprimorada.js]]