User:Equazcion/OneClickArchiverBeta.js

if ((($('#ca-addsection').length > 0) || (wgPageName == 'Wikipedia:Administrators\'_noticeboard/Edit_warring')) && (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 = 'retreiving 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: wgPageName, rvsection: 0, prop: "revisions|info", intoken: "edit", rvprop: "content", indexpageids: 1, 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);						var regexObject4 = /\| *maxarchivesize *= *(\d+)([kK]?)/;						var maxSizeMatch = regexObject4.exec(content);						if ((maxSizeMatch != null) || (typeof counter != 'undefined')){ 							if ((maxSizeMatch[2] == "k") || (maxSizeMatch[2] == "K")){								var maxSize = parseInt(maxSizeMatch[1]) * 1000;							} else {								var maxSize = parseInt(maxSizeMatch[1]);							}						} else {							var maxSize = 0;						}						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; if (maxSize > 0){ archiveNumOrig = archiveNum; archiveNum = archiveNum - 1; var size = 0; function inc{ archiveNum = archiveNum + 1; 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); var request7 = { //async: false, action:"query", titles: archiveName, prop: "revisions", rvprop: "size", format: "xml" };										$.get(mw.config.get("wgScriptPath")+"/api.php", request7, function(response7){											var responseSize = $(response7).find('rev').attr('size');											if (parseInt(responseSize) >= maxSize) inc;										}); }									inc;

newZero = content.replace(/\| *counter *= *(\d+)/, '|counter = ' + archiveNum); if (archiveNum > archiveNumOrig){ $('.arcProg').append(' Incrementing archive counter... '); var request8 = { action:"edit", section: 0, title: wgPageName, text: newZero, summary: 'OneClickArchiver incrementing archive counter', token: mw.user.tokens.get("csrfToken") };											$.post(mw.config.get("wgScriptPath")+"/api.php", request8, function(response8){												$('.archiveInc').append(' done! ');										});									}								}								$('.arcProg').append(' ' + 'Archive name ' + archiveName + 									' found, ' + mSection + ' '); var request5 = { action:"query", titles: wgPageName, rvsection: section + 1, prop: "revisions|info", intoken: "edit", rvprop: "content", indexpageids: 1, 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 + 1, 										title: 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; });								});							}						}					});				});			}		}	}); }