User:Anne drew Andrew and Drew/SetupAutoArchive.js

function getArchiveCounter(current) { var url = "https://en.wikipedia.org/w/api.php?action=query&titles=" + mw.config.get('wgPageName') + "/Archive_" + current + "&format=json&formatversion=2" var cont = true $.ajax({	    async: false,	     type: 'GET',	     url: url,	     success: function(data) {	            if (data.query.pages["0"].missing) {	                cont = false	            }	        }	}); if(cont){ return getArchiveCounter(current+1) }   return current>1 ? current-1 : current }

function addAutoArchive(talkheader, archives, age) { age = age ? "|age=" + age : ""; talkHeader = document.editform.wpTextbox1.value.toLowerCase.indexOf("talk header") === -1 && talkheader === "true" ? "yes" : "no"; archives = talkHeader === "no" && archives === "true" ? "yes" : "no"; if ((document.editform.wpTextbox1.value.indexOf("User:MiszaBot/config") === -1 && document.editform.wpTextbox1.value.indexOf("User:ClueBot III/ArchiveThis") === -1) ||       confirm("It looks like this page is already being automatically archived. Are you sure you want to continue?")) { var content = document.editform.wpTextbox1.value; contents = [content.substr(0, content.indexOf('==')), content.substr(content.indexOf('=='))]; document.editform.wpTextbox1.value = (contents[0] + "\n\n" + contents[1]) .replace(/|||/ig, ''); document.editform.wpSummary.value = "Set up automatic archiving using SetupAutoArchive.js"; document.getElementById('wpSave').click; } else window.history.back; }

function showModal { var actionrequired = ""; while (actionrequired.toLowerCase !== "yes" && actionrequired.toLowerCase !== "no") { actionrequired = prompt("Is there already a on the page? (yes/no)"); }   actionrequired = actionrequired === "no"; if (actionrequired) { var talkheader = ""; while (talkheader.toLowerCase !== "yes" && talkheader.toLowerCase !== "no") { talkheader = prompt("Prepend standard to page? (yes/no)"); }       talkheader = talkheader === "yes"; var archives = ""; if (!talkheader) { while (archives.toLowerCase !== "yes" && archives.toLowerCase !== "no") { archives = prompt("Prepend index template to page instead? (yes/no)"); }           archives = archives === "yes"; } else archives = false; } else { talkheader = false; archives = false; }   var age; while (age === undefined || (!(/[0-9]*/.test(age)) && age !== "")) { age = prompt("How many days should a thread be inactive before archiving?") }   age = age ? "&age=" + age : ""; var editLink = window.location.protocol + "//" + window.location.host + "/w/index.php?title=" + encodeURIComponent(mw.config.get('wgPageName')) + "&action=edit&setupArchive=" + Date.now + "&talkheader=" + talkheader + "&archives=" + archives + age; window.location.href = editLink; }

jQuery(document).ready(function($) { if (["talk", "wikipedia"].some(function (str) {    return mw.config.get('wgCanonicalNamespace').toLowerCase.indexOf(str) > -1  })) {    if (document.editform) {            var params = new URLSearchParams(window.location.search);            var shouldContinue = params.get("setupArchive");            if (shouldContinue) {                shouldContinue = Date.now - shouldContinue < 10000;                if (shouldContinue) {                    var talkheader = params.get("talkheader");                    var archives = params.get("archives");                    var age = params.get("age");                    addAutoArchive(talkheader, archives, age);                } else {                    throw "Operation timed out. [SetupAutoArchive.js]";                }            }        } else {            mw.util.addPortletLink("p-cactions", "javascript:showModal", "Setup Archive", "autoArchive", "Add auto archiving", ""); }   } });