Jump to content

User:Makyen/OneClickArchiver.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
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(' <div style="font-size:.6em;font-weight:bold;float:right"> | <a id="' + section + 
					'" href="#archiverLink" class="archiverLink">' + 'Archive' + '</a></div>');
 
				$(this).parent('h2').find('a.archiverLink').click(function(){
 
					var mHeaders = '<span style="color:#454545;">Retrieving headers...</span>';
					var mSection = 'retreiving section content...';
					var mPosting = '<span style="color:#004000">Content retrieved,</span> performing edits...';
					var mPosted = '<span style="color:#008000">Archive appended...</span>';
					var mCleared = '<span style="color:#008000">Section cleared...</span>';
					var mReloading = '<span style="color:#00008C">All done! </span>Reloading...';
 
					$('body').append('<div class="overlay" style="background-color:#000;opacity:.4;position:fixed;' + 
						'top:0;left:0;width:100%;height:100%;z-index:500;"></div>');					
 
					$('body').prepend('<div class="arcProg" style="font-weight:bold;box-shadow: 7px 7px 5px #000;font-size:0.9em;line-height:1.5em;' + 
						'z-index:501;opacity:1;position:fixed;width:50%;left:25%;top:30%;background:#F7F7F7;border:#222 ridge 1px;padding:20px;"></div>');
 
					$('.arcProg').append('<div>' + mHeaders + '</div>');
 
					var request4 = {
						action:"query", 
						titles: wgPageName, 
						rvsection: 0,
						prop: "revisions|info", 
						intoken: "edit", 
						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('\\|\\s*counter\\s*=\\s*(\\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 = /(\|\s*archive\s*=\s*.*\%\(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('<div>' + 'Archive name <span style="font-weight:normal;color:#003366;">' + archiveName + 
									'</span> <span style="darkgreen">found</span>, ' + mSection + '</div>');
 
								var request5 = {
									action:"query", 
									titles: wgPageName, 
									rvsection: section,
									prop: "revisions|info", 
									intoken: "edit", 
									rvprop: "content",
									indexpageids: 1,
									dataType: "xml",
									format: "xml"
								};
 
								$.get(mw.config.get("wgScriptPath")+"/api.php", request5, function(response5){
 
									$('.arcProg').append('<div>' + mPosting + '</div>');
 
									var contentSection = '\r\r' + $(response5).find('rev').text();
 
									var request1 = {
										action:"edit", 
										title: archiveName,
										appendtext: contentSection, 
										summary: '[[User:Equazcion/OneClickArchiver|OneClickArchiver]] adding 1 discussion',
										token: mw.user.tokens.get("csrfToken")
									};
 
									var request2 = {
										action:"edit", 
										section: section, 
										title: wgPageName, 
										text: "",
										summary: '[[User:Equazcion/OneClickArchiver|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('<div class="archiverPosted">' + mPosted + '</div>'); }),
										$.post(mw.config.get("wgScriptPath")+"/api.php", request2, function(response2){	
											$('.arcProg').append('<div class="archiverCleared">' + mCleared + '</div>'); })
									 ).done(function() {
									 	$('.arcProg').append('<div>' + mReloading + '</div>');
									 	location.reload();
									 });
								});
							}
						}
					});
				});
			}
		}
	});
}