User:Equazcion/sandbox.js

From Wikipedia, the free encyclopedia
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.
var archiveName = wgPageName + '/Archive' + 110; //+ archiveNum;
$('span.mw-headline').each(function(index, value){
	var section = index;
	if ($(this).parent().prop("tagName") == 'H2'){
		var sectionTitle = $(this).html();
	 
		$(this).parent('h2').append('<a id="' + section + '" href="#archiverLink" class="archiverLink">' + 'Archive' + '</a>');
		$(this).parent('h2').find('a.archiverLink').click(function(){
			
			var request4 = {
				action:"query", 
				titles: wgPageName, 
				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 regexObject2 = new RegExp( '((^|\n)={1,5} ?.*[\\s\\S]*?)\n={1,5}' , 'g');
				var matches = [];
				while (match = regexObject2.exec(content)) {
					matches.push(match[1]);
					regexObject2.lastIndex = regexObject2.lastIndex - 5;
    			}
				if (matches != null){
					var contentMatch = matches[section]; 
					//window.open("data:text/html," + encodeURIComponent(matches[section]), "_blank", "width=200,height=100");
				}
				
				var request1 = {
					action:"edit", 
					title: archiveName, 
					appendtext: contentMatch, 
					summary: 'adding ' + sectionTitle,
					token: mw.user.tokens.get("editToken")
				};

				$.post(mw.config.get("wgScriptPath")+"/api.php", request1, function(response1){
					
					var request2 = {
						action:"edit", 
						section: section + 1, 
						title: wgPageName, 
						text: "",
						summary: 'archived ' + sectionTitle,
						token: mw.user.tokens.get("editToken")
					};
			 
					$.post(mw.config.get("wgScriptPath")+"/api.php", request2, function(response1){
						
						location.reload();	
					});
				});
			});
		});
	}
});