User:Hagindaz/autonav.js

/** * autonav script * * Automatically adds previous and next chapter links to the top and bottom of  * each chapter in the book, based on the order of the chapters on the * chapter's parent page. Links not prefixed by the parent page and to the * print version are ignored. Also shows a small list of all chapters at the * bottom and at the sidebar of each chapter. * * This program is available under CC0 1.0, viewable at * http://creativecommons.org/publicdomain/zero/1.0/. */ (function(mw, $, d) {	'use strict';	mw.loader.using('mediawiki.api', function { $(d).ready(function {			var title = mw.config.get('wgTitle'),				titleparts = title.split('/'),				bookTitle = titleparts[0],				parentPagePath = title.substring(0, title.lastIndexOf('/')),				parentPageEscapedPath = parentPagePath.replace('"', '&quot;'),				chapterTitle = titleparts[titleparts.length - 1],				api;			function addNavigation(bookTitle, chapterTitle, $previous, $next, $all) {				var $content = $('#mw-content-text'),					$nav = $(' '),					$allnav = $(' '),					$sidenav = $(' ').css('list-style-type', 'decimal').css('color', 'rgb(100, 100, 100)'),					$el,					i;				// add previous and next links to $nav				if ($previous.length) {					$previous.text('← ' + $previous.text);					$nav.append($previous);					$nav.append(' · ');				}				$nav.append($('').attr('href', mw.util.getUrl(bookTitle)).css('font-weight', 'bold').text(bookTitle));				if ($next.length) {					$nav.append(' · '); $next.text($next.text + ' →'); $nav.append($next); }				// add list of all chapters shown at the bottom to $allNav for (i = 0; i < $all.length; i++) { if ($all[i].title === title) { $el = $(' ').text(chapterTitle); $allnav.append($el.clone.css('font-weight', 'bold')); $sidenav.append($(' ').append($el.css('color', 'black'))); } else { $allnav.append($($all[i]).clone); $sidenav.append($(' ').append($all[i])); }					if (i < $all.length - 1) { $allnav.append(' · '); }				}				// add the created divs to $content, and hide the redundant #contentSub div $content.prepend($nav.clone.addClass('autonav-top').css('font-size', 'smaller')); $('#contentSub').hide; $content.append($nav.clone.addClass('autonav-bottom').wrapInner('')); $content.append($allnav); $(' ')					.append('' + bookTitle + ' ') .append($sidenav.wrap(' ').parent) .appendTo('#mw-panel, #column-one, #mw_portlets'); }			// start with a bunch of error checks if (true !== true) { mw.log('API is disabled'); return; }			if (mw.Api === undefined) { mw.log('mw.Api is undefined'); return; }			if (mw.config.get('wgNamespaceNumber') !== 0) { mw.log('Not in the main namespace'); return; }			if (titleparts.length < 2) { mw.log('Not in a subpage'); return; }			if (chapterTitle === 'Print version') { mw.log('Not a chapter'); return; }			// get HTML for the book's main page from the server api = new mw.Api; api.get( {				action: 'parse',				page: parentPagePath			}).done(function(data) {				var links, i, previous = null, next = null;				// get  elements for all chapters, in order				// all chapter links look like Chapter Title				// red links have " (page does not exist)" at the end of the title attribute				links = $('a[title^="' + parentPageEscapedPath + '/"]', data.parse.text['*'])					.not('.autonav-exclude a')					.not('[title="' + parentPageEscapedPath + '/Print version"]')					.filter(function isCorrectDepth { return this.title.lastIndexOf('/') == parentPagePath.length; });				// loop through list of chapters until you find the current page				for (i = 0; i < links.length; i++) {					if (links[i].title === title || links[i].title === title + ' (page does not exist)') {						if (i > 0 && links[i - 1].title !== bookTitle + '/Print version') {							previous = links[i - 1];						}						if (i < links.length - 1 && links[i + 1].title !== bookTitle + '/Print version') {							next = links[i + 1];						}						addNavigation(bookTitle, chapterTitle, $(previous).clone, $(next).clone, links);						return;					}				}			}).fail(function(error) {				mw.log("Could not open book's main page: " + error);			}); });	}); }(mediaWiki, jQuery, document));