MediaWiki:Gadget-addprefix.js

From Wikimedia Incubator

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * The AddPrefix gadget
 *
 * This is a gadget that allows prefixes to be automatically
 * added to wikilinks before a page in a test wiki is saved.
 * It simplifies the need of adding prefixes to pages when
 * that page is being saved, so as to save time in adding the
 * prefixes and more time to do more meaningful contributions
 * to a specific test wiki.
 *
 * This gadget will detect if the page is part of a test wiki,
 * else it would just skip automatically adding prefixes to 
 * wikilinks.
 * 
 * @author Slomox
 * @todo Enable support for templates and categories
 */

function rep() {
	var prefix = mw.config.get('wgTitle').substr(0, mw.config.get('wgTitle').lastIndexOf("/"));
	if ((prefix.indexOf("/") == 2) && (mw.config.get('wgTitle').lastIndexOf("/") != 2) && (mw.config.get('wgAction') == 'edit')) {
		var x = document.getElementById("wpTextbox1").value;
		x = x.replace(/\[\[([^\]:]+)\]\]/g, "[["+"|$1]]");
		document.getElementById("wpTextbox1").value = x;

		document.getElementById("editform").onsubmit = function(){
			x = document.getElementById("wpTextbox1").value;
			var prefix = mw.config.get('wgTitle').substr(0, mw.config.get('wgTitle').lastIndexOf("/"));
			x = x.replace(/\[\[([^\/\|\]:]+(\|[^\/\]\|:]+))\]\]/g, "[["+prefix+"/$1]]");
			x = x.replace(/\[\[([^\/\|\]:]+)\]\]/g, "[["+prefix+"/$1|$1]]");
			x = x.replace(/\[\[\|([^\]:]+)\]\]/g, "[[$1]]");
			document.getElementById("wpTextbox1").value = x;
		};
	}
}

$(rep);