User:Anne 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] + "{{subst:" + "Setup auto archiving|small=yes|notice=yes|counter=" + getArchiveCounter(1) + "|talk=" + talkHeader + "|archives=" + archives + age + "}}\n\n" + contents[1])
            .replace(/{{archiveme}}|{{longtalk}}|{{long talk}}|{{Tpcleanup}}/ig, '');
        document.editform.wpSummary.value = "Set up automatic archiving using [[User:Anne drew Andrew and Drew/SetupAutoArchive|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 {{talk header}} on the page? (yes/no)");
    }
    actionrequired = actionrequired === "no";
    if (actionrequired) {
        var talkheader = "";
        while (talkheader.toLowerCase() !== "yes" && talkheader.toLowerCase() !== "no") {
            talkheader = prompt("Prepend standard {{talk header}} to page? (yes/no)");
        }
        talkheader = talkheader === "yes";
        var archives = "";
        if (!talkheader) {
            while (archives.toLowerCase() !== "yes" && archives.toLowerCase() !== "no") {
                archives = prompt("Prepend {{archives}} 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", "");
        }
    }
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.