Jump to content

User:ASammour/test.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
if (mw.config.get('wgArticleId') == 130130){ //Works only in Steward requests/Global
mw.loader.using(["mediawiki.api"]).then(function () {
    var myArray = []; 

    /*Counting the sections in the page*/
    $(".toctext").each (function (index){
        myArray[index] = $(this).text(); // Setting the index to the section title
    });
    
    //A function to get the index of specified section tit
    function getIndex (title){
        var result = 0;
        for (var i=1;i<myArray.length;i++){
            if (myArray[i] == title){
                result = i;
                break;
            }
        }
        return result;
    }
    
    //Append X close button beside each section
    $(".mw-editsection").each (function (){
        $(this).append (" <span class = 'closeButton' style = 'color: white;background-color: #f95a5a;border: 1px white dashed;border-radius: 3px;cursor:pointer;'>X Close</span>");
    });
    
    $(document).on('click', '.closeButton', function() {
        var decision = prompt ("Please answer if you want to mark the request as done (1) or notdone (0)");
        if (decision == "0"){
            decision = "notdone";
        }
        else if (decision == "1"){
            decision = "done";
        }
        else{
        	return false;
        }
    	
        //Get the content of the page
        var api = new mw.Api();
        var content = "";
        api.get({
            "action": "query",
        	"format": "json",
        	"prop": "revisions",
        	"titles": "Steward requests/Global",
        	"utf8": 1,
        	"rvprop": "content",
        	"formatversion": "latest",
        }).done(function (result){
            content = result.query.pages[0].revisions[0].content;
        });
        
        //Get the content of the section
    	var sectionContent = api.get({
            "action": "query",
        	"format": "json",
        	"prop": "revisions",
        	"titles": "Steward requests/Global",
        	"utf8": 1,
        	"rvprop": "content",
        	"formatversion": "latest",
        	"rvsection": parseInt(getIndex($(this).parent().prev().text()))+1
        }).done(function (result){
            var sectionContent = result.query.pages[0].revisions[0].content.toString();
        
            //Replace the status
            var newSectionContent= "";
            newSectionContent = sectionContent.replace ("{{status}}","{{status|"+decision+"}}");
            newSectionContent = newSectionContent.replace ("{{Status}}","{{status|"+decision+"}}");
            newSectionContent = newSectionContent.replace ("{{Status|}}","{{status|"+decision+"}}");
            newSectionContent = newSectionContent.replace ("{{status|}}","{{status|"+decision+"}}");

            //Add {{done}} or {{notdone}}
            newSectionContent = newSectionContent + "\n\n{{"+decision+"}}.-\~\~\~\~";
    
            content = content.replace(sectionContent, newSectionContent);
            
            if (content != ""){
                //Edit goes here
                edit (content, ($(this).parent().prev().text()), decision);
            }
        });
    });
    
    function edit (content,title, decision){
        new mw.Api().edit(
                'Steward requests/Global',
                function () {
                    return {
                        text: content,
                        summary: "Re: "+title+" {{"+decision+"}}",
                        minor: true
                    };
                }
            )
            .then(function () {
                alert ("Done!");
                console.log( 'Done!');
        });
    }
} );
}