User:Uziel302/typos.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

if ( mw.config.get('wgPageName') === 'User:Uziel302/Typos' ) {
	$(function(){
	function reloadNotify(msg) {
		mw.notify(msg).done(function(){ window.location.reload(); });
	}

	function removeSection(section, basetimestamp, starttimestamp, summ) {
		var api = new mw.Api(), pagename = this.curPage;
		api.postWithToken('csrf', {
			action: 'edit',
			title: 'User:Uziel302/Typos',
			summary: summ,
			minor: 1,
			basetimestamp: basetimestamp,
			starttimestamp: starttimestamp,
			text: '',
			section: section
		}).done(function(d) {
			if (d && d.edit && d.edit.result == 'Success') {
				mw.notify('Removed');
			}
		});
	}
	function removeSectionAfterCorrection(section, basetimestamp, starttimestamp, summ) {
		var api = new mw.Api(), pagename = this.curPage;
		api.postWithToken('csrf', {
			action: 'edit',
			title: 'User:Uziel302/Typos',
			summary: summ,
			minor: 1,
			basetimestamp: basetimestamp,
			starttimestamp: starttimestamp,
			text: '',
			section: section
		}).done(function(d) {
			if (d && d.edit && d.edit.result == 'Success') {
			mw.notify('Probably fixed');
			}
		});
	}

	function archiveToTalk(articleName, sectionTitle, content) {
		var talkPage = new mw.Title(articleName).getTalkPage(),
		newContent =  content.replace(/== .+? ==\n+/, '');
		return new mw.Api().post({action: 'edit', minor: 1, title: talkPage.getPrefixedText(), summary:'removed', token: mw.user.tokens.get('editToken'), section: 'new', sectiontitle: sectionTitle, text: 'from [[User:Uziel302/Typos]]\n' + newContent }).then(function(d){ mw.notify($('<a>', {href: talkPage.getUrl(), target: '_blank'}).text('Successfully archived')); });	
	}
	function saveChange1(articleName, content,suspect,correction) {
		return new mw.Api().post({
			action: 'edit', 
			minor: 1, 
			title: articleName, 
			summary: suspect+'->'+correction+' - [[User:Uziel302/Typos|Fix a typo in one click]]',
			token: mw.user.tokens.get('editToken'), 
			text: content
			}).done(function(d){ //mw.notify('Successfully fixed');
			 });	
	}
	function saveChange(e) {
			var articleName = $(this).parents('h2').find('a:first').prop('title'),
			sectionNum = $(this).parent().find('a').attr('href').match(/section=(.*)/)[1],
			api = new mw.Api();
						
			api.get( {action:'query',prop:'revisions',titles:mw.config.get('wgPageName'), rvprop:'content|timestamp',rvsection:sectionNum, curtimestamp: 1}).done(function(data){
				for(var rv in data.query.pages)
				{
					var sectionText = data.query.pages[rv].revisions[0]['*'],
					basetimestamp = data.query.pages[rv].revisions[0].timestamp;
					if ((sectionText.indexOf('== [['+articleName+']] ==') !== 0) && (sectionText.indexOf('== [[:'+articleName+']] ==') !== 0)) {
						reloadNotify('passage unavailable');
					} else {
						if (sectionText.indexOf('{{#lst:')!=-1) {
							removeSection(sectionNum, basetimestamp ,data.curtimestamp, '/* '+ articleName +' */ ');
						} else {
							var	articleText='undefined',newArticleText='undefined',suspect='undefined',correction='wow',oldcontext='wwowo',newcontext='undefined';
							suspect=sectionText.replace(/.*\n.*?(.*)?\->.*\n.*\n.*/, "$1")+' ';	
							correction=sectionText.replace(/.*\n.*?>(.*)?\?.*\n.*\n.*/, "$1")+' ';	
							var	api = new mw.Api();
							api.get({action: 'query',prop:'revisions',titles: articleName,rvprop:'content',curtimestamp:1
							}).done(function(d) {for(var rv in d.query.pages)
							{articleText=d.query.pages[rv].revisions[0]["*"];
							newArticleText=articleText.replace(suspect,correction);
							if(newArticleText===articleText)
								{mw.notify('Could not find suspect word in article');
								removeSection(sectionNum, basetimestamp ,data.curtimestamp,suspect+' was not found in [['+articleName+']]');
								return;}
							saveChange1(articleName,newArticleText,suspect,correction)
							if(newArticleText!=articleText)removeSectionAfterCorrection(sectionNum, basetimestamp ,data.curtimestamp,suspect+'->'+correction+' fixed!');
							if(newArticleText===articleText)removeSectionAfterCorrection(sectionNum, basetimestamp ,data.curtimestamp,suspect+'->'+correction+' was not fixed!');
							}})
							}
					}

				}
			});
			e.preventDefault();
	}
	
	function saveChange2(e) {
			var articleName = $(this).parents('h2').find('a:first').prop('title'),
			sectionNum = $(this).parent().find('a').attr('href').match(/section=(.*)/)[1],
			api = new mw.Api();
			api.get( {action:'query',prop:'revisions',titles:mw.config.get('wgPageName'), rvprop:'content|timestamp',rvsection:sectionNum, curtimestamp: 1}).done(function(data){
				for(var rv in data.query.pages){
					var sectionText = data.query.pages[rv].revisions[0]['*'],
					basetimestamp = data.query.pages[rv].revisions[0].timestamp;
					if ((sectionText.indexOf('== [['+articleName+']] ==') !== 0) && (sectionText.indexOf('== [[:'+articleName+']] ==') !== 0)) {
						reloadNotify('passage unavailable');
					} else {
						if (sectionText.indexOf('{{#lst:')!=-1) {
							removeSection(sectionNum, basetimestamp ,data.curtimestamp, '/* '+ articleName +' */ ');
						} else {
							var	articleText='undefined',newArticleText='undefined',suspect='undefined',correction='wow',approvedCorrection='wow',oldcontext='wwowo',newcontext='undefined';
							suspect=sectionText.replace(/.*\n.*?(.*)?\->.*\n.*\n.*/, "$1");	
							correction=sectionText.replace(/.*\n.*?>(.*)?\?.*\n.*\n.*/, "$1");	
							approvedCorrection=prompt(suspect+' will be replaced by: ',correction);
							if(approvedCorrection===null||approvedCorrection===suspect)
							{removeSection(sectionNum, basetimestamp ,data.curtimestamp,suspect+' was not fixed');return;}
							var	api = new mw.Api();
							api.get({action: 'query',prop:'revisions',titles: articleName,rvprop:'content',curtimestamp:1
							}).done(function(d) {for(var rv in d.query.pages)
							{articleText=d.query.pages[rv].revisions[0]["*"];
							newArticleText=articleText.replace(suspect,approvedCorrection);
							if(newArticleText===articleText){mw.notify('Could not find suspect word in article');return;}
							saveChange1(articleName,newArticleText,suspect,approvedCorrection)
							if(newArticleText!=articleText)removeSectionAfterCorrection(sectionNum, basetimestamp ,data.curtimestamp,suspect+'->'+approvedCorrection+' fixed!');
							if(newArticleText===articleText)removeSectionAfterCorrection(sectionNum, basetimestamp ,data.curtimestamp,suspect+'->'+approvedCorrection+' was not fixed!');
							}})
							}
					}

				}
			});
			e.preventDefault();
	}
	function archiveReport(e) {
			var summ = 'Suspect typo';
			var articleName = $(this).parents('h2').find('a:first').prop('title').replace(' (page unavailable)', ''),
			sectionNum = $(this).parent().find('a').attr('href').match(/section=(.*)/)[1],
			api = new mw.Api();
			
			api.get( {action:'query',prop:'revisions',titles:mw.config.get('wgPageName'), rvprop:'content|timestamp',rvsection:sectionNum, curtimestamp: 1}).done(function(data){
				for(var rv in data.query.pages)
				{
					var sectionText = data.query.pages[rv].revisions[0]['*'],
					basetimestamp = data.query.pages[rv].revisions[0].timestamp;
					if ((sectionText.indexOf('== [['+articleName+']] ==') !== 0) && (sectionText.indexOf('== [[:'+articleName+']] ==') !== 0)) {
						reloadNotify('passage unavailable');
					} else {
						if (sectionText.indexOf('{{#lst:')!=-1) {
							removeSection(sectionNum, basetimestamp ,data.curtimestamp, '/* '+ articleName +' */');
						} else {
							archiveToTalk(articleName, summ, sectionText).done(function(){removeSection(sectionNum, basetimestamp ,data.curtimestamp, '/* '+ articleName +' */ '); });
						}
					}

				}
			});
			e.preventDefault();
	}

	function removeReport(e) {
			var summ ='Passage removal';
			//if(summ === null) return;
			var articleName = $(this).parents('h2').find('a:first').prop('title').replace(' (Page unavailable)', ''),
			sectionNum = $(this).parent().find('a').attr('href').match(/section=(.*)/)[1],
			api = new mw.Api();
			api.get( {action:'query',prop:'revisions',titles:mw.config.get('wgPageName'), rvprop:'content|timestamp',rvsection:sectionNum, curtimestamp: 1}).done(function(data){
				for(var rv in data.query.pages)
				{
					var sectionText = data.query.pages[rv].revisions[0]['*'],
					basetimestamp = data.query.pages[rv].revisions[0].timestamp;
					if ((sectionText.indexOf('== [['+articleName+']] ==') !== 0) && (sectionText.indexOf('== [[:'+articleName+']] ==') !== 0)) {
						reloadNotify('Paragraph is missing');
					} else {
						removeSection(sectionNum, basetimestamp ,data.curtimestamp, summ);
					}
				}
			});
			e.preventDefault();
	}


		$('h2').each(function()
		{
			var saveChangeBtn = $('<a>Replace</a>').attr({href:'#'}).css({'font-size':'large', 'margin-right':'20px'}).click(saveChange);
			    saveChangeBtn2 = $('<a>Type</a>').attr({href:'#'}).css({'font-size':'large', 'margin-right':'20px'}).click(saveChange2);
				archive = $('<a>Move to talk</a>').attr({href:'#'}).css({'font-size':'large', 'margin-right':'20px'}).click(archiveReport),
			    removeReportBtn = $('<a>Remove</a>').attr({href:'#'}).css({'font-size':'large', 'margin-right':'20px'}).click(removeReport);
			$(this).find('.mw-editsection').append(['<br><br>',saveChangeBtn,removeReportBtn,saveChangeBtn2,archive]);
		});
	});
}