MediaWiki:Gadget-Títulos simples.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.
/*  _____________________________________________________________________________
 * |                                                                             |
 * |                    === AVISO: ARQUIVO DE GADGET GLOBAL ===                  |
 * |            Alterações a esta página afetarão vários colaboradores           |
 * |            Por favor discuta alterações na página de discussão ou           |
 * |                    em [[Wikilivros:Gadgets]] antes de editar.               |
 * |_____________________________________________________________________________|
 *
 * Script para simplificar os links exibidos nas categorias dos livros:
 * Em vez de "Nome do livro/Nome do capítulo", exibe "Nome do capítulo"
 * @author: Helder (https://github.com/he7d3r)
 */
/*global mw, $ */
if ( 'Category' === mw.config.get( 'wgCanonicalNamespace' ) ) {
	$( function () {
		var prefix = mw.config.get( 'wgTitle' ).replace( /^Livro\//g, '' ) + '/';
		$( '#mw-pages' ).find( 'a' ).text( function ( i, val ) {
			return val.slice( 0, prefix.length ) === prefix ? val.slice( prefix.length ) : val;
		} );
	} );
}

// Funções para a simplificação dos títulos
// Adaptada da function mod_node() do [[oldwikisource:MediaWiki:Modernisation.js]]
function new_subtitle(node) {
	var i;
	if (node.nodeType === 3) {
		if ( node.data.match(/< /) ) {
			node.data = '';
		} else if ( node.data.match(/ \| /) ) {
			node.data = '/';
		}
	} else {
		for ( i = 0; i < node.childNodes.length; i++ ) {
			new_subtitle(node.childNodes[i]);
		}
	}
}
// Inspirada na "function setupTitle()" do [[:es:MediaWiki:Monobook.js]]
function setupTitle() {
	var base = '';
	$('#contentSub :first')
		.filter(function() {
			// Remove text-nodes
			// Ao exibir diffs ou ao editar os links necessários não estão disponíveis
			return this.nodeType !== 3;
		})
		.each(function () {
			new_subtitle(this);
		})
		.find('a').each(function () {
			base += $(this).text() + '/';
		})
		.end().html(function(i, old) {
			var chapter = $('#firstHeading').html(function(i, old) {
				return old.replace(base, '');
			}).html();
			return old + '/' + chapter;
		});
}
if ( 'view' === mw.config.get( 'wgAction' ) && 0 === mw.config.get( 'wgNamespaceNumber' ) && mw.config.get( 'wgTitle' ).indexOf( '/' ) > -1 ) {
	mw.util.addCSS( 'span.subpages a { margin:0 0.1em; }' );
	$( setupTitle );
}
// [[Categoria:Gadgets|Simplificar categorias.js]]
// [[Categoria:Scripts do Wikilivros|Simplificar categorias.js]]