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.
function NRHPmapbutton() {
    if (mw.config.get('wgPageName')!="Wikipedia:WikiProject_National_Register_of_Historic_Places/Progress"||location.href.indexOf('action')!=-1) {
        return;
    }
    var button=document.createElement("input")
    button.setAttribute("type", "button");
    button.setAttribute("id", "mapbutton");
    button.setAttribute("value", "Generate SVG Output");
    button.setAttribute("onclick", "MapButtonClick()");
    var content=document.getElementById('mw-content-text')

    content.parentNode.insertBefore(button, content)
}

function MapButtonClick() {
    var button = document.getElementById('mapbutton')
    button.disabled = true
    var SVGDiv = document.createElement('div')
    SVGDiv.setAttribute('id', 'SVGDiv')
    SVGDiv.setAttribute("style", "width:500px; border:1px solid black; padding:5px")
    button.parentNode.insertBefore(SVGDiv, button)
    SVGDiv.innerHTML = "Fetching wikitext..."
    
    mw.loader.using( ['mediawiki.util', 'user.options'] ).then( function() {
	    getSVGtext("Wikipedia:WikiProject National Register of Historic Places/Progress/SVG")
    } );
}

function getSVGtext(title) { // get SVG code from /SVG page
    var SVGtext=""
    $.ajax({
        dataType: "json",
        url: mw.util.wikiScript('api'),
        data: {
            format: 'json',
            action: 'query',
            prop: 'revisions',
            rvprop: 'content',
            titles: title,
            indexpageids: true,
            redirects: 'true'
        },
        error: function() {SVGtext="error"},
        success: function(output) {
            for (page in output.query.pages) {
                SVGtext=output.query.pages[page].revisions[0]['*'];
            }
        },
        complete: function() {
            if (SVGtext=="error") {
                var SVGDiv=document.getElementById("SVGDiv")
                SVGDiv.innerHTML+=" Unable to fetch wikitext! Script aborted."
            } else {
                getProgressText(SVGtext,"Wikipedia:WikiProject National Register of Historic Places/Progress")
            }
        }
    })
}

function getProgressText(SVGtext,title) { // get progress page wikitext
    var ProgressText=""
    $.ajax({
        dataType: "json",
        url: mw.util.wikiScript('api'),
        data: {
            format: 'json',
            action: 'query',
            prop: 'revisions',
            rvprop: 'content',
            titles: title,
            indexpageids: true,
            redirects: 'true'
        },
        error: function() {ProgressText="error"},
        success: function(output) {
            for (page in output.query.pages) {
                ProgressText=output.query.pages[page].revisions[0]['*'];
            }
        },
        complete: function() {
            if (ProgressText=="error") {
                var SVGDiv=document.getElementById("SVGDiv")
                SVGDiv.innerHTML+=" Unable to fetch wikitext! Script aborted."
            } else {
                ExtractMapInfo(SVGtext,ProgressText)
            }
        }
    })
}

function ExtractMapInfo(SVGtext,ProgressText) {
    var SVGDiv=document.getElementById("SVGDiv")
    SVGDiv.innerHTML+=" Done!"

    ProgressText=ProgressText.split("==State totals==")[1] // get rid of header

    var StateNames= ["OVERALL STATE PERCENTAGES","ALABAMA","ALASKA","ARIZONA","ARKANSAS","CALIFORNIA","COLORADO","CONNECTICUT","DELAWARE"]
    StateNames.push("D.C.","FLORIDA","GEORGIA","HAWAII","IDAHO","ILLINOIS","INDIANA","IOWA","KANSAS","KENTUCKY","LOUISIANA","MAINE")
    StateNames.push("MARYLAND","MASSACHUSETTS","MICHIGAN","MINNESOTA","MISSISSIPPI","MISSOURI","MONTANA","NEBRASKA","NEVADA")
    StateNames.push("NEW HAMPSHIRE","NEW JERSEY","NEW MEXICO","NEW YORK","NORTH CAROLINA","NORTH DAKOTA","OHIO","OKLAHOMA","OREGON")
    StateNames.push("PENNSYLVANIA","RHODE ISLAND","SOUTH CAROLINA","SOUTH DAKOTA","TENNESSEE","TEXAS","UTAH","VERMONT")
    StateNames.push("VIRGINIA","WASHINGTON","WEST VIRGINIA","WISCONSIN","WYOMING")

    var StateAbbrs= ['XX','AL','AK','AZ','AR','CA','CO','CT','DE','DC','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD']
    StateAbbrs.push('MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD')
    StateAbbrs.push('TN','TX','UT','VT','VA','WA','WV','WI','WY','PR','GU','VI','MP','AS')

    var StatsToCheck=["Illustrated","Articled","Start+","Net Quality"]
    for (var currentStat=0; currentStat<StatsToCheck.length; currentStat++ ) { // loop through each statistic to create SVG code
        var ThisStatText=""
        var OverallStatePercentagesText=""
        var NoListings=[]

        var TableStartIndex=0
        for (var i=0; i<StateNames.length; i++) { // loop through all states, i=0 corresponds to national table
            var StateTable={"100Percent":[], "90Percent":[], "80Percent":[], "70Percent":[], "60Percent":[], "50Percent":[], "40Percent":[], "30Percent":[], "20Percent":[], "10Percent":[], "00Percent":[]}

            TableStartIndex=ProgressText.indexOf("{|",TableStartIndex+1)
            var TableEndIndex=ProgressText.indexOf("|}",TableStartIndex)+2
            var TableText=ProgressText.substr(TableStartIndex,TableEndIndex-TableStartIndex)

            var RowStartIndex=0
            var RowNumber=0
            var DCdone=false
            while (RowStartIndex!=-1) { // loop until no more rows
                RowStartIndex=TableText.indexOf("\n|-",RowStartIndex+1) // find next row in table
                var RowEndIndex=TableText.indexOf("\n|-",RowStartIndex+1)
                if (RowEndIndex==-1) RowEndIndex=TableText.length // last row ends at end of table
                var RowText=TableText.substr(RowStartIndex,RowEndIndex-RowStartIndex)

                RowNumber++
                if (i==0&&RowNumber==StateAbbrs.length) break; // manual break for national table since some rows aren't on the map

                var ID=RowText.match(/(\d{5}|-----|ddddd)/g)
                if (i==0) ID=StateAbbrs[RowNumber]
                if (ID==null||ID[0]=="ddddd"||ID[0]=="-----") continue; // skip duplicate rows, sublists, and state total row
                if (i!=0) ID=ID[0]
                if (ID=="02230"||ID=="02275") continue; // skip special cases in Alaska, taken care of below

                if (ID=="02105") { // special case for Hoonah–Angoon Census Area, Alaska
                    var TempStartIndex=RowStartIndex
                    var nextID="00000"
                    while (nextID!="02230") { // look for Skagway
                        TempStartIndex=TableText.indexOf("\n|-",TempStartIndex+1)
                        var TempEndIndex=TableText.indexOf("\n|-",TempStartIndex+1)
                        if (TempEndIndex==-1) TempEndIndex=TableText.length
                        var nextRowText=TableText.substr(TempStartIndex,TempEndIndex-TempStartIndex)
                        nextID=nextRowText.match(/(\d{5}|-----|ddddd)/g)
                        if (nextID!=null) nextID=nextID[0]
                    }
                    var HoonahStats=RowText.match(/[^\d](\d{1,3},)*\d{1,3}(?!(%|\.|\d))/g)
                    var SkagwayStats=nextRowText.match(/[^\d](\d{1,3},)*\d{1,3}(?!(%|\.|\d))/g)

                    var Percentage=0
                    switch(currentStat) {
                        case 0: // Illustrated
                            var total=parseFloat(HoonahStats[0])+parseFloat(SkagwayStats[0])
                            var illustrated=parseFloat(HoonahStats[1])+parseFloat(SkagwayStats[1])
                            Percentage=Math.round(illustrated/total*1000)/10
                            break
                        case 1: // Articled
                            var total=parseFloat(HoonahStats[0])+parseFloat(SkagwayStats[0])
                            var articled=parseFloat(HoonahStats[2])+parseFloat(SkagwayStats[2])
                            Percentage=Math.round(articled/total*1000)/10
                            break
                        case 2: // Start+
                            var total=parseFloat(HoonahStats[0])+parseFloat(SkagwayStats[0])
                            var startPlus=parseFloat(HoonahStats[5])+parseFloat(SkagwayStats[5])
                            Percentage=Math.round(startPlus/total*1000)/10
                            break
                        case 3: // Net Quality
                            var total=parseFloat(HoonahStats[0])+parseFloat(SkagwayStats[0])
                            var illustrated=parseFloat(HoonahStats[1])+parseFloat(SkagwayStats[1])
                            var stubs=parseFloat(HoonahStats[3])+parseFloat(SkagwayStats[3])
                            var NRISonly=parseFloat(HoonahStats[4])+parseFloat(SkagwayStats[4])
                            var startPlus=parseFloat(HoonahStats[5])+parseFloat(SkagwayStats[5])
                            var unassessed=parseFloat(HoonahStats[6])+parseFloat(SkagwayStats[6])
                            var untagged=parseFloat(HoonahStats[7])+parseFloat(SkagwayStats[7])

                            Percentage=startPlus+0.5*stubs+0.5*unassessed-0.5*untagged-0.75*NRISonly
                            Percentage=Math.round((0.75*Percentage/total+0.25*illustrated/total)*1000)/10
                            if (Percentage<0) Percentage = 0
                            break
                    }
                    ID="02232" // set special ID for map
                } else if (ID=="02280") { // special case for Petersburg Census Area, Alaska
                    var TempStartIndex=RowStartIndex
                    var nextID="00000"
                    while (nextID!="02275") { // look for Wrangell
                        TempStartIndex=TableText.indexOf("\n|-",TempStartIndex+1)
                        var TempEndIndex=TableText.indexOf("\n|-",TempStartIndex+1)
                        if (TempEndIndex==-1) TempEndIndex=TableText.length
                        var nextRowText=TableText.substr(TempStartIndex,TempEndIndex-TempStartIndex)
                        nextID=nextRowText.match(/(\d{5}|-----|ddddd)/g)
                        if (nextID!=null) nextID=nextID[0]
                    }
                    var PetersburgStats=RowText.match(/[^\d](\d{1,3},)*\d{1,3}(?!(%|\.|\d))/g)
                    var WrangellStats=nextRowText.match(/[^\d](\d{1,3},)*\d{1,3}(?!(%|\.|\d))/g)

                    var Percentage=0
                    switch(currentStat) {
                        case 0: // Illustrated
                            var total=parseFloat(PetersburgStats[0])+parseFloat(WrangellStats[0])
                            var illustrated=parseFloat(PetersburgStats[1])+parseFloat(WrangellStats[1])
                            Percentage=Math.round(illustrated/total*1000)/10
                            break
                        case 1: // Articled
                            var total=parseFloat(PetersburgStats[0])+parseFloat(WrangellStats[0])
                            var articled=parseFloat(PetersburgStats[2])+parseFloat(WrangellStats[2])
                            Percentage=Math.round(articled/total*1000)/10
                            break
                        case 2: // Start+
                            var total=parseFloat(PetersburgStats[0])+parseFloat(WrangellStats[0])
                            var startPlus=parseFloat(PetersburgStats[5])+parseFloat(WrangellStats[5])
                            Percentage=Math.round(startPlus/total*1000)/10
                            break
                        case 3: // Net Quality
                            var total=parseFloat(PetersburgStats[0])+parseFloat(WrangellStats[0])
                            var illustrated=parseFloat(PetersburgStats[1])+parseFloat(WrangellStats[1])
                            var stubs=parseFloat(PetersburgStats[3])+parseFloat(WrangellStats[3])
                            var NRISonly=parseFloat(PetersburgStats[4])+parseFloat(WrangellStats[4])
                            var startPlus=parseFloat(PetersburgStats[5])+parseFloat(WrangellStats[5])
                            var unassessed=parseFloat(PetersburgStats[6])+parseFloat(WrangellStats[6])
                            var untagged=parseFloat(PetersburgStats[7])+parseFloat(WrangellStats[7])

                            Percentage=startPlus+0.5*stubs+0.5*unassessed-0.5*untagged-0.75*NRISonly
                            Percentage=Math.round((0.75*Percentage/total+0.25*illustrated/total)*1000)/10
                            if (Percentage<0) Percentage = 0
                            break
                    }
                } else if (ID=="11001") { // special case for DC
                    if (DCdone) continue; // only record DC once
                    DCdone=true
                    var TempStartIndex=RowStartIndex
                    var matched=false
                    while (!matched) { // look for total row
                        TempStartIndex=TableText.indexOf("\n|-",TempStartIndex+1)
                        var TempEndIndex=TableText.indexOf("\n|-",TempStartIndex+1)
                        if (TempEndIndex==-1) TempEndIndex=TableText.length
                        var nextRowText=TableText.substr(TempStartIndex,TempEndIndex-TempStartIndex)
                        var test=nextRowText.match("State Total")
                        if (test!=null) matched=true
                    }
                    var Percentage=nextRowText.match(/(\d|\.)+(?=%)/g)
                    Percentage=parseFloat(Percentage[currentStat])
                } else {
                    var Percentage=RowText.match(/(\d|\.)+(?=%)/g)           // extract percentages from row text
                    if (Percentage==null) {NoListings.push(ID); continue}    // if no percentages, must be no listings
                    Percentage=parseFloat(Percentage[currentStat])           // get relevant percentage for this stat
                }

                if (Percentage==100) {StateTable["100Percent"].push(ID)}
                else if (Percentage>=90) {StateTable["90Percent"].push(ID)}
                else if (Percentage>=80) {StateTable["80Percent"].push(ID)}
                else if (Percentage>=70) {StateTable["70Percent"].push(ID)}
                else if (Percentage>=60) {StateTable["60Percent"].push(ID)}
                else if (Percentage>=50) {StateTable["50Percent"].push(ID)}
                else if (Percentage>=40) {StateTable["40Percent"].push(ID)}
                else if (Percentage>=30) {StateTable["30Percent"].push(ID)}
                else if (Percentage>=20) {StateTable["20Percent"].push(ID)}
                else if (Percentage>=10) {StateTable["10Percent"].push(ID)}
                else {StateTable["00Percent"].push(ID)}
            }
            ThisStatText+="/* *******************************************************************\n"
            ThisStatText+="                              " + StateNames[i] + "\n"
            ThisStatText+="******************************************************************** */\n"

            var colors=["313695","4575B4","74ADD1","ABD9E9","E0F3F8","FFFFBF","FEE090","FDAE61","F46D43","D73027","A50026"]
            for (var j=10; j>-1; j--) { // loop through each percentage
                ThisStatText+="/* "
                if (j!=0) ThisStatText+=j
                ThisStatText+="0% "+StatsToCheck[currentStat]+" */\n      "
                for (var k=0; k<StateTable[j+"0Percent"].length; k++) {
                    if (i==0) {
                        ThisStatText+="."+StateTable[j+"0Percent"][k]+", "
                    } else {
                        ThisStatText+=".c"+StateTable[j+"0Percent"][k]+", "
                    }
                }
                if (StateTable[j+"0Percent"].length>0) ThisStatText=ThisStatText.substr(0,ThisStatText.length-2)+" "
                ThisStatText+="{fill: #"+colors[j]+"; }\n"
            }
            ThisStatText+="\n"
            if (i==0) { // wait to add state totals to end of list
                OverallStatePercentagesText=ThisStatText.substr(0,ThisStatText.length-2) // remove final line breaks
                ThisStatText=""
            }
        }
        // now add no listings text
        ThisStatText+="/* *******************************************************************\n"
        ThisStatText+="                        NO LISTINGS IN COUNTY\n"
        ThisStatText+="******************************************************************** */\n      "
        for (var k=0; k<NoListings.length; k++) {
            ThisStatText+=".c"+NoListings[k]+", "
        }
        if (NoListings.length>0) ThisStatText=ThisStatText.substr(0,ThisStatText.length-2)+" "
        ThisStatText+="{fill: #000000; }\n\n"

        // finally add state totals text
        ThisStatText+=OverallStatePercentagesText

        // now update SVG text
        var StartIndex = 0
        var InsertLocations = [];
        var SearchStr = 'overflow:scroll">'
        for (var i=0; i<StatsToCheck.length; i++) {
            InsertLocations[i] = SVGtext.indexOf(SearchStr, StartIndex) + SearchStr.length + 1
            StartIndex = InsertLocations[i]
        }

        var StartIndex = 0
        var EndInsertLocations = [];
        var SearchStr = '</pre>'
        for (var i=0; i<StatsToCheck.length; i++) {
            EndInsertLocations[i] = SVGtext.indexOf(SearchStr, StartIndex) - 1
            StartIndex = EndInsertLocations[i] + SearchStr.length + 1
        }
        var oldStatText=SVGtext.substr(InsertLocations[currentStat],EndInsertLocations[currentStat]-InsertLocations[currentStat])
        SVGtext=SVGtext.replace(oldStatText,ThisStatText)
    }
    // now edit page with updated SVG text
    var d=new Date();
    var months = ['January','February','March','April','May','June','July','August','September','October'];
    months.push('November','December')
    var year=d.getYear();
    if (year < 1000) year += 1900
    var DateStr = months[d.getMonth()]+" "+d.getDate()+", "+year

    var summary="Updating SVG output as of "+DateStr+" using [[User:Dudemanfellabra/NRHPmap.js|script]]."

    SVGDiv.innerHTML+="<br>Editing page..."
    editMapPage("Wikipedia:WikiProject National Register of Historic Places/Progress/SVG",SVGtext,summary);
}

function editMapPage(title,text,summary) { // edit page when done
    $.ajax({
        dataType: 'json',
        url: mw.util.wikiScript( 'api' ),
        type: 'POST',
        data: {
            format: 'json',
            action: 'edit',
            title: title,
            text: text,
            summary: summary,
            token: mw.user.tokens.get( 'csrfToken' )
        },
        error: function(ajaxResponse,status,errorThrown) {ajaxResponse.errorThrown=errorThrown},
        complete: function(ajaxResponse,status) {MapPageEdited(ajaxResponse,status)}
    })
}

function MapPageEdited(ajaxResponse,status) {
    var SVGDiv=document.getElementById("SVGDiv")
    if (status!="success") {
        SCGDiv.innerHTML+="Edit failed!"
        return;
    }
    var responseText=JSON.parse(ajaxResponse.responseText)
    var diff=responseText.edit.newrevid
    var linkStr="//en.wikipedia.org/w/index.php?diff="+diff
    SVGDiv.innerHTML+="Page edited! Click <a href='"+linkStr+"'>here</a> for diff."
}

function MakePreCopyable() {
    if (mw.config.get('wgPageName') != "Wikipedia:WikiProject_National_Register_of_Historic_Places/Progress/SVG") return;
    var pres = document.getElementsByTagName('pre');
    while (pres.length>0) {
        var textarea = document.createElement('textarea');
        textarea.innerHTML = pres[0].innerHTML;
        pres[0].parentNode.insertBefore(textarea,pres[0].nextSibling);
        pres[0].parentNode.removeChild(pres[0]);
        textarea.setAttribute('style', 'width: 580px; height: 300px')
        textarea.setAttribute('onclick', 'this.focus();this.select()')
    }
}

$(window).on('load', NRHPmapbutton);
$(window).on('load', MakePreCopyable);