User:Tommy Kronkvist/common.js - Wikispecies


Article Images

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**  SCRIPT 1 ______________________________________________________________________________________
*    Automatically set all TOCs to collapsible
*    Author:  Not applicable (standard wiki code)
*    License: Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
*    Bug:     Collapses the TOC, but clicking the 'Show' link doesn't expand it again **/ 

function hideToc() { 
  var toc = document.getElementById('toc').getElementsByTagName('ul')[0]; 
  var toggleLink = document.getElementById('togglelink'); 
  toc.style.display = 'none'; 
} 
hideToc();

/**  SCRIPT 2 ______________________________________________________________________________________
*    Creates redirect pages for synonyms
*    Author:  Rillke
*    License: GPL v.3 **/ 

importScript('User:Rillke/createRedirects.js')

/**  SCRIPT 3 ______________________________________________________________________________________
*    Real-Time Recent Changes
*    Author: Krinkle **/ 

mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Krinkle/RTRC.js&action=raw&ctype=text/javascript');

/**  SCRIPT 4 ______________________________________________________________________________________
*    JavaWikiBrowser, a web version of AutoWikiBrowser
*    Author: Joeytje50 

importScriptURI('//en.wikipedia.org/w/index.php?title=User:Joeytje50/JWB.js/load.js&action=raw&ctype=text/javascript'); **/

/**  SCRIPT 5 ______________________________________________________________________________________
*    Wikipedia XTools gadget
*    Author: Hedonil
*    Currently not loaded: mw.loader.load('//meta.wikimedia.org/w/index.php?

title=User:Hedonil/XTools/XTools.js&action=raw&ctype=text/javascript'); **/

/**  SCRIPT 6 ______________________________________________________________________________________
* Create redirects to a given page.
* First click on the toolbox link.
* This highlights possible redirects.
* A second click creates them.
*
* It requires a h3 section which is exactly named "Synonyms" or "{{int:Synonyms}}"
* or "Synonymy" or "{{int:Synonymy}}".
* then a ul/li structure and italic names of the redirects to create.
*
* Tested at [[Epipactis helleborine subsp. helleborine]]
*
* @author Rillke
* @license GPL v.3
**/ 
 
(function ($, mw) {

	mw.util.addCSS(
		'.syn-x { background:#ddd; cursor:pointer; }' +
		'.syn-to-create { background:#ffb; outline:dotted 1px #999; }' +
		'.syn-to-create:before { content: \" to be created - \"; }' +
		'.syn-created { background:#bfb; }' +
		'.syn-create-err { background:#fbb; }');
	var p = mw.util.addPortletLink('p-tb', '#createRedirs', 'Create Redirects',
		't-syn-redir', 'Create redirects using AJAX in batch');

	if (!p) return;
	var $p = $(p),
		$syns = $(),
		$count = $('<span>'),
		$txt = $p.find('a');

	$txt = $txt.length ? $txt : $p;

	var __updateCount = function () {
		$count.text('(' + $syns.filter('.syn-to-create').length + ')');
	};

	var __create = function () {
		var mwa = new mw.Api();
		// At the time of writing this mw.Api.edit was broken 
		// (no callback because it did not return the promise object 
		// nor did it forward the callback functions properly)

		var _post = function () {
			if (!$syns.filter('.syn-to-create').length) return $txt.text("Done!");
			var $el = $syns.filter('.syn-to-create').eq(0),
				title = $el.text();

			__updateCount();
			mwa.post({
				action: 'edit',
				createonly: true,
				token: mw.user.tokens.get('csrfToken'),
				text: '#REDIRECT [[' + mw.config.get('wgPageName').replace(/_/g, ' ') + ']]',
				title: title,
				watchlist: 'nochange'
			}).done(function () {
				$el.removeClass('syn-to-create').addClass('syn-created');
				_post();
			}).fail(function () {
				$el.removeClass('syn-to-create').addClass('syn-create-err');
				_post();
			});
		};
		_post();
	};
	var __highlight = function () {
		var _textsWeHave = {};
		var _highlightLi = function (i, el) {
			var $el = $(el),
				$i = $el.find('i:first'),
				t = $i.text();

			if (t in _textsWeHave) return;
			_textsWeHave[t] = true;
			$syns = $syns.add($i.addClass('syn-to-create syn-x'));
		};

		$('h3').find('#Synonyms,#Synonymy,#{{int:Synonyms}},#{{int:Synonymy}}').parents('h3').nextUntil('h3,h2,hr').find('li').each(_highlightLi);

		$syns.click(function () {
			$(this).toggleClass('syn-to-create');
			__updateCount();
		});
		__updateCount();
		$txt.text("Create Redirs: ").append($count);
		$p.unbind('click').click(function (e) {
			e.preventDefault();
			$p.unbind('click').addClass('ui-state-disabled');
			$.createSpinner().prependTo($txt);
			__create($syns);
		});
	};

	$p.click(function (e) {
		e.preventDefault();
		$p.unbind('click');
		mw.loader.using(['jquery.spinner', 'mediawiki.util', 'mediawiki.api', 'mediawiki.user'], function () {
			__highlight();
		});
	});
}(jQuery, mediaWiki));