User:Timdream/Fact.js

(function ($) {

function editSection( sectionId, token, summary, content, callback) { $.ajax({			url: wgScriptPath + '/api.php',			data: {				'action': 'edit',				'title': wgPageName,				'section': sectionId,				'summary': summary,				'text': content,				'format': 'json',				'token': token			},			dataType: 'json',			type: 'POST',			success: function( data ) {				if ( data.edit.result == 'Success' ) {					callback;					window.location.reload; // reload page if edit was successful				} else {					alert( 'Error: Unknown result from API.' );				}			},			error: function( xhr ) {				alert( 'Error: Edit failed.' );			}		}); }

// https://secure.wikimedia.org/wikipedia/mediawiki/wiki/Manual:Edit_token#Retrieving_via_Ajax function getEditToken(callback) { $.getJSON(			wgScriptPath + '/api.php?',			{				action: 'query',				prop: 'info',				intoken: 'edit',				titles: wgPageName,				indexpageids: '',				format: 'json'			},			function( data ) {				if ( data.query.pages && data.query.pageids ) {					var pageid = data.query.pageids[0],					wgEditToken = data.query.pages[pageid].edittoken;					callback(wgEditToken);				}			}		) }	function getSelection { // https://developer.mozilla.org/en/DOM/window.getSelection // TBD: multiple ranges var range = window.getSelection; return { text: range.getRangeAt(0).toString, element: range.anchorNode.parentNode // anchorNode.nodeName == #text };	}	function getSectionID(el) { return (			( $(el).parents('#bodyContent > *').prevAll('h2,h3,h4,h5,h6').find('.editsection > a').get(0) || $(el).prevAll('h2,h3,h4,h5,h6').find('.editsection > a').get(0) || { search: 'section=0' } ).search.match(/section=(\d+)/) ||			['', 0]		)[1]; }

function getSectionContent(id, callback) { $.ajax({			url: wgScriptPath + '/index.php',			data: {				'action': 'raw',				'title' : wgPageName,				'section': id			},			dataType: 'text',			type: 'GET',			success: function ( data ) {				callback(data);			},			error: function ( xhr ) {				alert( 'Error: get raw fail.');			}		}); }

function citationNeeded(selection, callback) { var sectionId = getSectionID(selection.element); getSectionContent(			sectionId,			function (text) {				var replaced = text.replace( new RegExp(selection.text), selection.text + '' );				if (replaced.length === text.length) {					alert('Sorry, I couldn\'t find the place to insert .');					return;				}				getEditToken( function (token) { editSection(							sectionId,							token,							'[citation needed]: ' + selection.text,							replaced,							callback						); }				);			}		);	}	var $btn = $(' ').text().css(		{			position: 'absolute'		}	).bind(		'click',		function {			var selection = getSelection;			if (selection.text !==  && window.confirm('Add [Citation Needed] to following selected text?\n\n' + selection.text)) {				$btn.attr('disabled', true).text('...');				citationNeeded( selection, function { $btn.text('Done!'); }				);			}		}	);	$(document.body).append($btn);

$('#bodyContent').bind(		'mouseup', // FIXME mouseup fires before selection being removed by a single click		function (ev) {			var selection = getSelection;			if (selection.text !== '') {				$btn.css({ top: ev.pageY, left: ev.pageX }).fadeIn(200);			} else {				$btn.fadeOut(200);			}		}	);

})(jQuery);