User:NQ/custom-scripts/diffconverter.js

// User:Scottywong/diffconverter.js - modified to add toolbar button instead of shortcut key using code from [[User:Js/urldecoder.js] var btn = newToolbarBtn; function addDiffDecoderButton{	btn(		'diffDecoder', 		diffRun, 		'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Accept.svg/30px-Accept.svg.png', 		'Diff Converter - Scottywong/diffconverter.js'	); }

mw.loader.using( [ 'user.options', 'jquery.textSelection' ], function {	/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar . . . */	if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {		$(document).ready( function  { if ( mw.user.options.get('usebetatoolbar') ) { mw.loader.using( 'ext.wikiEditor', addDiffDecoderButton ); } 		} );	}	// Add the customizations to LiquidThreads' edit toolbar, if available	mw.hook( 'ext.lqt.textareaCreated' ).add( addDiffDecoderButton ); } ); function newToolbarBtn(bId, bFunc, bIcon, bTitle){ var msg = {}; msg[bId] = bTitle; mw.messages.set(msg) // mw.usability.addMessages(msg) doesn't work $('#wpTextbox1').wikiEditor('addToToolbar', {  section:'main', group:'insert', tools: {    bId:{     type: 'button',     action: {type:'callback', execute: bFunc},     labelMsg: bId,     icon: bIcon  }}})

}

function diffRun {		if (window.getSelection)			//if any text is selected {			var diffstr = window.getSelection.toString; if (diffstr == "") diffstr = document.editform.wpTextbox1.value.substr(document.editform.wpTextbox1.selectionStart, document.editform.wpTextbox1.selectionEnd - document.editform.wpTextbox1.selectionStart); var diffstr2 = diffstr; var article = decodeURIComponent(urlparse("title", diffstr).replace(/_/g, " ")); var diff = urlparse("diff", diffstr); var oldid = urlparse("oldid", diffstr); var label = "Diff of " + article; var oldidlabel = "Previous revision of " + article; var anchor = ""; if (diffstr.indexOf(" ") != -1) {				label = diffstr.slice(diffstr.indexOf(" ") + 1); diffstr2 = diffstr.slice(0, diffstr.indexOf(" ")); if (label[label.length - 1] == "]") {					label = label.slice(0, label.length - 1); }				oldidlabel = label; }			if (diffstr.indexOf("#") != -1) {				anchor = encodeURIComponent(diffstr2.slice(diffstr.indexOf("#")).replace(/_/g, " ")); if (anchor[anchor.length - 1] == "]") {					anchor = anchor.slice(0, anchor.length - 1); }			}			if (article == "") {				alert("Diffconverter.js: Invalid URL. Ensure that you have selected a valid diff URL."); }			else if (article != "" && diff != "" && oldid != "")	//diff URL should have all three parameters {				var start = document.editform.wpTextbox1.selectionStart; var len = diffstr.length; var difftemplate = "{" + "{diff|" + article + anchor + "|" + diff + "|" + oldid + "|" + "}" + "}"; document.editform.wpTextbox1.value = document.editform.wpTextbox1.value.substring(0, start) + difftemplate + document.editform.wpTextbox1.value.substring(start + len); }			else if (article != "" && oldid != "" && diff == "")	//oldid URL doesn't contain a diff parameter {				var start = document.editform.wpTextbox1.selectionStart; var len = diffstr.length; var difftemplate = "{" + "{oldid|" + article + anchor + "|" + oldid + "|" + oldidlabel + "}" + "}"; document.editform.wpTextbox1.value = document.editform.wpTextbox1.value.substring(0, start) + difftemplate + document.editform.wpTextbox1.value.substring(start + len); }			else if (article != "" && oldid == "" && diff == "")	//no diff, no oldid, just create a wikilink out of it			{ var start = document.editform.wpTextbox1.selectionStart; var len = diffstr.length; var wikilink = "" + article + ""; document.editform.wpTextbox1.value = document.editform.wpTextbox1.value.substring(0, start) + wikilink + document.editform.wpTextbox1.value.substring(start + len); }		}		else {			alert("Diffconverter.js: No text selected."); }

}

function urlparse(name, url)	//thanks to http://www.netlobo.com/url_query_string_javascript.html for this function {	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+name+"=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(url); if(results == null) {		return ""; }	else {		if (results[1].indexOf(" ") != -1) results[1] = results[1].slice(0, results[1].indexOf(" ")); if (results[1][results[1].length - 1] == "]") results[1] = results[1].slice(0, results[1].length - 1); return results[1]; } }