User:Teemu08/NRHPbutton.js

function RenumberButton { if (wgPageName.search("National_Register_of_Historic_Places_listings_in")==-1) return var button=document.createElement("input") button.setAttribute("type", "button"); button.setAttribute("value", "Renumber list"); button.setAttribute("id", "button"); button.setAttribute("onclick", "RenumberClick"); var content=document.getElementById('mw-content-text') content.parentNode.insertBefore(button, content) } function RenumberClick { var button = document.getElementById('button') button.disabled = true getWikitext(wgPageName) } function getWikitext(title) { $.getJSON(       mw.util.wikiScript('api'),        {            format: 'json',            action: 'query',            prop: 'revisions',            rvprop: 'content',            titles: title,            indexpageids: true        }    ) .done(function (data) {           var page, wikitext;            for (page in data.query.pages) {                wikitext = data.query.pages[page].revisions[0]['*'];                RenumberList(wikitext)            }        }) .fail(function {           alert('Could not get wikitext; failure.');        }); } function RenumberList(wikitext) { var StartIndex = 0 var str = "|pos=" var skip = str.length; var index, RowLocations = []; while ((index = wikitext.indexOf(str, StartIndex)) > -1) { RowLocations.push(index); StartIndex = index + skip; }   var stop=RowLocations.length+1 for (var i=1; i -1) { // find location of each county row in wikitext string RowLocations.push(index); StartIndex = index + skip; }   }    editNRHPPage({        title: wgPageName,        text: wikitext,        summary: 'Semi-automated renumbering of list items after addition/deletion.'    }); } function editNRHPPage(info) { $.ajax({       url: mw.util.wikiScript( 'api' ),        type: 'POST',        dataType: 'json',        async: false,        data: {            format: 'json',            action: 'edit',            title: info.title,            text: info.text,            summary: info.summary,            token: mw.user.tokens.get( 'editToken' )        }    }) .done (function( data ) {           if (data && data.edit && data.edit.result && data.edit.result == 'Success') {                alert("Success! Rows renumbered!")            } else {                alert("Error! Rows not renumbered! Refresh the page to try again")            }        }) .fail (function {           alert('Ajax failure.');        }); } $(RenumberButton);