User:Gryllida/js/draft-rater.js

//

mw.loader.using(['mediawiki.api'], function {	"use strict";	var api = new mw.Api;	//validProjectsArray = ['Australia', '']	/*if(mw.config.get('wgCanonicalNamespace') != 'Draft'){		return;	}*/	var visitNextDraft = function{		window.open("https://en.wikipedia.org/wiki/Special:RandomInCategory/Pending_AfC_submissions","_self");	};	// if the talk page does not exist, add a textbox for input	if($('#ca-talk').attr('class') && $('#ca-talk').attr('class') == 'new'){		$('#p-namespaces').append($('  ', {id: 'gryllida-draft-rater-div'}));		var $input = $(' ', { type:'text', name: 'gryllida-wikiproject', id: 'gryllida-wikiproject', form: 'gryllida-wpjs-form' });		var $label =$(' ', { for:'gryllida-wikiproject' }).text('WPrjs:');		var $submitButton =$(' ', { type: 'submit', style: 'display: none' });		$input.keyup(function(event) { if ($input.is(":focus") && event.key == "Enter") { // if user pressed enter, add the marker if($input.val.length > 0){ var $span = $(' ', {		        		class: 'gryllida-draft-rater', 		        		text: $input.val		        	}); $span.click(function{		       		$span.remove;		        	}); $span.css('margin-left','10px'); $span.css('margin-right','10px'); $span.css('background-color', 'beige'); // check whether wikiproject name is valid var projname = $input.val; api.get( { 		       		action: 'query',		        		titles: 'Wikipedia:WikiProject '+projname,		        		prop: 'info'}).done( function ( data ) {		        			if(data.query.pages['-1']){		        				return;		        			}		        		}); // XXX extend this // https://en.wikipedia.org/w/api.php? // action=query // titles=Page%20that%20does%20not%20exist // &prop=info&inprop=&format=json /*if(validProjectsArray.indexOf(projname) > -1){ $span.css('background-color', '#ddffdd'); }*/		       	// add to dom $span.insertBefore($input); $input.val(''); // submit the marks } else { $input.prop('disabled', true); var str = ''; var summary = 'added projects '; var TXTsummary = 'added projects '; $('.gryllida-draft-rater').each(function(i){	   				var projN = $($('.gryllida-draft-rater')[i]).text;	    				str = str + '{' + '{WikiProject ' + projN + '}}\r\n';	    				summary = summary + +projN+' ';	    				summary = summary + '(template), ';	    				TXTsummary = TXTsummary + +projN+', ';	    			}); var realsummary = summary + ' (how to join)'; var wgNamespaceNumber=mw.config.get( 'wgNamespaceNumber' ); var wgTitle = mw.config.get('wgTitle'); api.postWithToken("edit", {						action: 'edit',						title: mw.config.get( 'wgFormattedNamespaces' )[wgNamespaceNumber+1] +':'+wgTitle,						text: str,						summary: realsummary					}).done(function (data){						var pageName = mw.config.get( 'wgPageName' ).replace(/\_/g, ' ');						// find who made the article						api.get( { action: 'query', titles: pageName, prop: 'revisions', rvprop: 'user', rvlimit: 1, 'rvdir': 'newer' }).done(function(data){ var u = data.query.pages[Object.keys(data.query.pages)].revisions[0].user; //alert(u); // action=query& // format=json& // prop=revisions& // titles=User%20talk%3AGryllida&callback=&rvlimit=1&rvdir=older api.get( { 				       		action: 'query',				        		titles: 'User talk:'+u,				        		prop: 'revisions',				        		rvprop: 'content',				        		rvlimit: 1,				        		'rvdir': 'older'				        	}).done(function(data){				        		var text = "";				        		if(data.query.pages[Object.keys(data.query.pages)].revisions){			        				text = data.query.pages[Object.keys(data.query.pages)].revisions[0]['*'];				        		}				        		//alert(text);				        		//alert(text.indexOf('welcome') == -1);				        		var msg = "";			        			if(text.indexOf('welcome') == -1) {			        				msg = msg + '--~';			        			}			        			msg = msg + '\r\n' + '== Re: ' + pageName + '==\r\n';			        			msg = msg + 'Hello ' + u + '\r\nI have ' + TXTsummary + ' to your article.';			        			msg = msg + ' You may wish to join them, check their to-do, and meet new'; msg = msg + ' people with interest in these topics. ';			       			msg = msg + '( To reply click "edit" next to this section, and add your reply at the end. ) '; msg = msg + 'Cheers, --~'; api.postWithToken("edit", {									action: 'edit',									title: 'User talk:'+u,									section: 'new',									text: msg								}).done(function (data){					       		api.postWithToken("watch", { action: 'watch', title: 'User talk:'+u }).done(function (data){ $('#gryllida-draft-rater-div').remove; var $span = $(' ', {html: 'Notified '+u+'.'}); $span.insertAfter($('#ca-talk')); $('#ca-talk').attr('class', ''); $('#ca-talk').attr('class', ''); // visit next draft visitNextDraft; });

});			       		});						});			        	});

}	   	}		});		var $form = $(' ', {action: '#', id: 'gryllida-wpjs-form'});		$form.append($label);		$form.append($input);		$form.append($submitButton);		$form.submit(false);		$('#gryllida-draft-rater-div').append($form);		$input.attr('autocomplete');		$form.attr('autocomplete')		$input.focus;	} else{		//visitNextDraft;	} });

//