User:Equazcion/OneClickArchiver.js

if ((($('#ca-addsection').length > 0) || (mw.config.get('wgPageName') == 'Wikipedia:Administrators\'_noticeboard/Edit_warring')) && (mw.config.get('wgAction') == 'view')){ $('span.mw-headline').each(function(index, value){		var editSectionUrl = $(this).parent.find('.mw-editsection a:first').attr('href');		var sectionReg = /&section=(.*)/;		var sectionRaw = sectionReg.exec(editSectionUrl);		if ((sectionRaw != null) && (sectionRaw[1].indexOf('T') < 0)){			var section = parseInt(sectionRaw[1]);			if ($(this).parent.prop("tagName") == 'H2'){				$(this).parent('h2').append(' | ' + 'Archive' + ' ');				$(this).parent('h2').find('a.archiverLink').click(function{ var mHeaders = ' Retrieving headers... ';					var mSection = 'retrieving section content...'; var mPosting = ' Content retrieved, performing edits...'; var mPosted = ' Archive appended... ';					var mCleared = ' Section cleared... ';					var mReloading = ' All done! Reloading...'; $('body').append(' '); $('body').prepend(' '); $('.arcProg').append(' ' + mHeaders + ' '); var request4 = { action:"query", titles: mw.config.get('wgPageName'), rvsection: 0, prop: "revisions|info", rvprop: "content", indexpageids: 1, dataType: "xml", format: "xml" };					$.get(mw.config.get("wgScriptPath")+"/api.php", request4, function(response4){						var content = $(response4).find('rev').text;						var regexObject3 = new RegExp('\\|counter ?= ?(\\d+)');						var counter = regexObject3.exec(content);						if ((counter == null) || (typeof counter == 'undefined')){							$('.arcProg').remove;							$('.overlay').remove;							alert("No archive counter was detected on this page, so archiving was aborted. See User:Equazcion/OneClickArchiver for details.");						} else {							var archiveNum = counter[1];							var archiveNameReg = /(\| *archive *= *.*\%\(counter\)d.*?) *(-->)?/;							var archiveNameRegMatch = archiveNameReg.exec(content);							if ((archiveNameRegMatch == null) || (typeof archiveNameRegMatch == 'undefined')){								$('.arcProg').remove;								$('.overlay').remove;								alert("No archive name was detected on this page, so archiving was aborted. See User:Equazcion/OneClickArchiver for details.");							} else {								var monthNames = ["january", "february", "march", "april", "may", "june", 									"july", "august", "september", "october", "november", "december"];								var shortMonthNames = ["jan", "feb", "mar", "apr", "may", "jun", 									"jul", "aug", "sep", "oct", "nov", "dec"];								var year = new Date.getFullYear;								var month = new Date.getMonth;								var archiveName = archiveNameRegMatch[1]									.replace(/\|archive ?= ?/, '')									.replace(/\%\(year\)d/g, year)									.replace(/\%\(month\)d/g, month)									.replace(/\%\(monthname\)s/g, monthNames[month])									.replace(/\%\(monthnameshort\)s/g, shortMonthNames[month])									.replace(/\%\(counter\)d/g, archiveNum);								//https://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Albert_Einstein&rvprop=size&format=xml								$('.arcProg').append(' ' + 'Archive name ' + archiveName + ' found, ' + mSection + ' ');								var request5 = {									action:"query", 									titles: mw.config.get('wgPageName'), 									rvsection: section,									prop: "revisions",									rvprop: "content",									indexpageids: 1,									dataType: "xml",									format: "xml"								};								$.get(mw.config.get("wgScriptPath")+"/api.php", request5, function(response5){ $('.arcProg').append(' ' + mPosting + ' '); var contentSection = '\r\r' + $(response5).find('rev').text; var request1 = { action:"edit", title: archiveName, appendtext: contentSection, summary: 'OneClickArchiver adding 1 discussion', token: mw.user.tokens.get("csrfToken") };									var request2 = { action:"edit", section: section, title: mw.config.get('wgPageName'), text: "", summary: 'OneClickArchiver archived 1 discussion to ' + archiveName + '', token: mw.user.tokens.get("csrfToken") };									$.when(										$.post(mw.config.get("wgScriptPath")+"/api.php", request1, function(response1){ $('.arcProg').append(' ' + mPosted + ' '); }),										$.post(mw.config.get("wgScriptPath")+"/api.php", request2, function(response2){ $('.arcProg').append(' ' + mCleared + ' '); })									 ).done(function {									 	$('.arcProg').append(' ' + mReloading + ' ');									 	location.reload;									 }); });							}						}					});				});			}		}	}); }