Jump to content

User:Dudemanfellabra/AddCommonsCatLinks.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dudemanfellabra (talk | contribs) at 17:40, 27 September 2014 (fix for when all matches have thumbnails that can't be displayed). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/*
   The following script places a button at the top of lists of historic sites on many registers around the world.
   When the button is clicked, the script looks on Commons to find images and categories linked to each site's unique ID
   and allows the user to choose to add them to the entries on the list.
*/

// global vars
var thiswikitext=""
var Rows=[];
var HTMLRows=[];
var Matches=[];
var FileMatches=[];
var FileSuggestions=[];
var refnum=[];
var currentRow=0
var MatchesFound=false

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Dudemanfellabra/AddCommonsCatLinks/SupportedRegisters.js&action=raw&ctype=text/javascript', 'text/javascript' );

function FindCommonsCatButton() {
    if (location.href.indexOf('action')!=-1||location.href.indexOf('.js')!=-1||wgNamespaceNumber==10||wgNamespaceNumber==11) return
    if (!RegisterTable[wgContentLanguage]) return

    thiswikitext=getThisWikitext(wgPageName)
    if (thiswikitext=="error"||thiswikitext=="") return
    for (var r in RegisterTable[wgContentLanguage]) {
        var regex = new RegExp("{{[ ]*"+RegisterTable[wgContentLanguage][r].RowTemplate, "g")
        if (thiswikitext.search(regex)!=-1) ThisRegisterTable = RegisterTable[wgContentLanguage][r]
    }
    if (!ThisRegisterTable.TopCatName) return

    if (!GUIMessages[wgUserLanguage]) {           // default to contentLanguage GUI if userLanguage is not yet supported
        if (!GUIMessages[wgContentLanguage]) {    // English if both not yet supported
            ThisGUIMessages = GUIMessages["en"]
        } else {
            ThisGUIMessages = GUIMessages[wgContentLanguage]
        }
    } else {
        ThisGUIMessages = GUIMessages[wgUserLanguage]
    }

    var button=document.createElement("input")
    button.setAttribute("type", "button");
    button.setAttribute("value", ThisGUIMessages.ButtonText);
    button.setAttribute("id", "commonsbutton");
    button.setAttribute("onclick", "CommonsClick()");
    var content=document.getElementById('mw-content-text')

    content.parentNode.insertBefore(button, content)
}

function CommonsClick() {
    var button = document.getElementById('commonsbutton')
    button.disabled = true

    var ProgressDiv = document.createElement("div")
    ProgressDiv.setAttribute("id", "ProgressDiv")
    ProgressDiv.setAttribute("style", "width:500px; border:5px solid black; padding:5px; position:fixed; background:#ffffff; z-index:100")
    ProgressDiv.style.left = "200px"
    ProgressDiv.style.top = "5px"
    commonsbutton.parentNode.insertBefore(ProgressDiv, commonsbutton)

    if (!GUIMessages[wgUserLanguage]&&!GUIMessages[wgContentLanguage]) {
        ProgressDiv.innerHTML="Preferred language ("+LanguageTable[wgUserLanguage].name+"/"+LanguageTable[wgUserLanguage].nativeName+") unavailable. Defaulting to English. <small>(If you can help translate, <a href='//en.wikipedia.org/w/index.php?title=User_talk:Dudemanfellabra&action=edit&section=new&preloadtitle=I+can+help+translate+the+CommonsCat+script+into+"+LanguageTable[wgUserLanguage].name+"!'>let me know!</a>)</small><br>"+ThisGUIMessages.CheckingMatches+" "
    } else {
        ProgressDiv.innerHTML=ThisGUIMessages.CheckingMatches+" "
    }
    window.onbeforeunload = OnBeforeUnLoad;
    FindRows()
}

function FindRows() {
    var AllHTMLRows=document.getElementsByTagName("tr")
    for (var i=0; i<AllHTMLRows.length; i++){
        if (AllHTMLRows[i].className.indexOf("vcard")!=-1||AllHTMLRows[i].className.search(/with(out)?_image/g)!=-1) HTMLRows.push(AllHTMLRows[i])
    }

    var str = "{{"+ThisRegisterTable.RowTemplate
    var start=0
    var commentstart=0
    while (true) {
        commentstart=thiswikitext.indexOf("<!--",start)
        start=thiswikitext.indexOf(str,start)
        if (start==-1) break
        if (commentstart<start&&commentstart!=-1) {
            start=thiswikitext.indexOf("-->",commentstart)
            start=thiswikitext.indexOf(str,start)
        }
        if (start==-1) break
        var open=1
        var index=start+str.length
        while (open!=0 && index<thiswikitext.length) {
            if (thiswikitext.substr(index,2)=="}}") {
                open--
                index++
            } else if (thiswikitext.substr(index,2)=="{{") {
                open++
                index++
            }
            index++
        }
        var regex = new RegExp("{{"+ThisRegisterTable.RowTemplate+"( |\\n)*\\|", "g") // make sure it's actual template, not subpage
        if (thiswikitext.substr(start,index-start).match(regex)!=null) Rows[Rows.length]=thiswikitext.substr(start,index-start)
        start++
    }
    if (Rows.length!=HTMLRows.length) {
        alert(ThisGUIMessages.RowMismatch.replace("ROWTEMPLATE",ThisRegisterTable.RowTemplate))
        window.onbeforeunload = function() {}
        return
    }
    currentRow=0
    CheckRow()
}

function CheckRow() {
    var ProgressDiv=document.getElementById("ProgressDiv")
    if (!GUIMessages[wgUserLanguage]&&!GUIMessages[wgContentLanguage]) {
        ProgressDiv.innerHTML="Preferred language ("+LanguageTable[wgUserLanguage].name+"/"+LanguageTable[wgUserLanguage].nativeName+") unavailable. Defaulting to English. <small>(If you can help translate, <a href='//en.wikipedia.org/w/index.php?title=User_talk:Dudemanfellabra&action=edit&section=new&preloadtitle=I+can+help+translate+the+CommonsCat+script+into+"+LanguageTable[wgUserLanguage].name+"!'>let me know!</a>)</small><br>"+ThisGUIMessages.CheckingMatches+" "
    } else {
        ProgressDiv.innerHTML=ThisGUIMessages.CheckingMatches+" "
    }
    if (currentRow==Rows.length) {
        if (MatchesFound) {
            if (!GUIMessages[wgContentLanguage]) {               // edit summary defaults to content language if available
                if (!GUIMessages[wgUserLanguage]) {              // if not, it falls back to user preferred language
                    var summary = GUIMessages["en"].EditSummary  // if neither, English
                } else {
                    var summary = GUIMessages[wgUserLanguage].EditSummary
                }
            } else {
                var summary = GUIMessages[wgContentLanguage].EditSummary
            }
            editPageWithCommonsCat({
                title: wgPageName,
                text: thiswikitext,
                summary: summary
            });
        } else {
            ProgressDiv.innerHTML+=ThisGUIMessages.NoneFound
            window.onbeforeunload = function() {}
            setTimeout(function(){ProgressDiv.parentNode.removeChild(ProgressDiv)}, 10000);
        }
        return;
    }
    HTMLRows[currentRow].scrollIntoView()      // highlight current row
    window.scrollBy(0,-10-ProgressDiv.offsetHeight)

    Matches=[]
    FileMatches=[]
    FileSuggestions=[]
    refnum = []
    var HasImage=false
    var HasCommonsCat=false
    if (typeof ThisRegisterTable.IdentifierName=='string') {
        ThisRegisterTable.IdentifierName = [ThisRegisterTable.IdentifierName]
    }
    for (var j=0; j<ThisRegisterTable.IdentifierName.length; j++) {
        var regex=new RegExp("\\|[ ]*?"+ThisRegisterTable.IdentifierName[j]+"[ ]*?=.*?(?=(\\n|\\||}}))", "g")
        var temprefnum=Rows[currentRow].match(regex)
        if (temprefnum==null) {
            if (ThisRegisterTable.IdentifierName[j]=="bygningsnr") { // special case for Denmark listed buildings in Danish where the
                temprefnum = ["|bygningsnr=1"]                       // parameter "bygningsnr" has default value "1" if not specified
            } else {
                continue
            }
        }
        temprefnum=temprefnum[0]
        regex=new RegExp("\\|[ ]*?"+ThisRegisterTable.IdentifierName[j]+"[ ]*?=", "g")
        temprefnum=temprefnum.replace(regex,"").replace(/ /g, "").replace(/\<\!\-\-(.|[\r\n])*?\-\-\>/g,"").split(",")
        for (var k=0; k<temprefnum.length; k++) {
            if (temprefnum[k]!="") refnum.push(temprefnum[k])
        }
    }
    if (ThisRegisterTable.CombineIdentifiers == true) {            // Some countries have multiple fields joined together
        refnum=[refnum.join(ThisRegisterTable.CombineWith)]
    }
    for (var j=0; j<refnum.length; j++) {
        if (refnum[j].length<8) {
            refnum[j]="00000000"+refnum[j]       // identifiers with less than 8 digits are padded left with zeroes
            refnum[j]=refnum[j].substr(refnum[j].length-8,8)
        }
    }

    if (typeof ThisRegisterTable.PropertyNameParam=='string') {
        ThisRegisterTable.PropertyNameParam = [ThisRegisterTable.PropertyNameParam]
    }

    var j=0
    while (j<ThisRegisterTable.PropertyNameParam.length) {
        var regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.PropertyNameParam[j]+"[ ]*?=.*?((\\[\\[.*?]]|\\{\\{(.|\\n)*?}}|<[ ]*?ref(.|\\n)*?(<[ ]*?)?/[ ]*?(ref)?[ ]*?>).*?)*(?=(\\n|\\||}}))", "g")
        var name=Rows[currentRow].match(regex)
        if (name!=null) {
            name = name[0]
            break
        } else {
            j++
        }
    }
    if (name==null) name=""

    regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.PropertyNameParam+"[ ]*?=", "g")
    name=name.replace(regex,"").replace(/(<[ ]*?)?ref(.|\n)*?<[ ]*?\/[ ]*?(ref)?[ ]*?>/g,"").replace(/\[\[.*?\|(.*?)]]/g, "$1").replace(/\<\!\-\-(.|[\r\n])*?\-\-\>/g,"").trim()

    regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.CommonscatParam+"[ ]*?=.*?(?=(\\n|\\||}}))", "g")
    var commonscat=Rows[currentRow].match(regex)
    if (commonscat!=null) {
        regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.CommonscatParam+"[ ]*?=", "g")
        commonscat=commonscat[0].replace(regex,"").replace(/\<\!\-\-(.|[\r\n])*?\-\-\>/g,"").trim()
        if (commonscat!="") {
            HasCommonsCat = true    // only true if commonscat param there and non-blank
        }
    }
    regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.ImageParam+"[ ]*?=.*?(?=(\\n|\\||}}))", "g")
    var image=Rows[currentRow].match(regex)
    if (image!=null) {
        regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.ImageParam+"[ ]*?=", "g")
        image=image[0].replace(regex,"").replace(/\<\!\-\-(.|[\r\n])*?\-\-\>/g,"").trim()
        if (image!="") {
            HasImage = true    // only true if image param there and non-blank
        }
    }
    if (!HasCommonsCat||!HasImage) {
        var temprefnum=[];
        for (var i=0; i<refnum.length; i++) {
            temprefnum[i]=refnum[i]
        }
        CheckForMatches(name,temprefnum,HasCommonsCat,HasImage)
    } else {
        currentRow++
        CheckRow(currentRow)   // if already has both commonscat and image, skip
    }
}

function CheckForMatches(name,temprefnum,HasCommonsCat,HasImage) {
    if (refnum.length==0) {
        if (Matches.length==0&&FileMatches.length==0&&FileSuggestions.length==0) {
            currentRow++
            CheckRow(currentRow)   // skip if no matches found
        } else {
            ChooseCat(name,temprefnum)
        }
        return
    }
    var sortkey=" "+refnum[0].replace(/\?/g,"") // strip question marks used as placeholders.. code breaks without this
    if (!HasCommonsCat) {
        $.ajax({
        url: '//commons.wikimedia.org/w/api.php?action=query&list=categorymembers&format=json&cmtitle='+ThisRegisterTable.TopCatName+'&cmprop=title|sortkeyprefix&cmtype=subcat&cmlimit=max&cmsort=sortkey&cmstartsortkeyprefix='+sortkey+'&cmendsortkeyprefix='+sortkey+'0',
        data: {
            format: 'json'
        },
        dataType: 'jsonp',
        success: function(CommonsCatjsonObject) {GotCats(CommonsCatjsonObject,name,temprefnum,sortkey,HasCommonsCat,HasImage)}
        });
    } else {
        GotCats({"query":{"categorymembers":[]}},name,temprefnum,sortkey,HasCommonsCat,HasImage)
    }
}

function GotCats(CommonsCatjsonObject,name,temprefnum,sortkey,HasCommonsCat,HasImage) {
    for (var c in CommonsCatjsonObject.query.categorymembers) {
        var title=CommonsCatjsonObject.query.categorymembers[c].title.replace("Category:","")
        Matches[Matches.length] = [title,refnum[0]]
    }

    if (!HasImage) {
        $.ajax({
        url: '//commons.wikimedia.org/w/api.php?action=query&list=categorymembers&format=json&cmtitle='+ThisRegisterTable.TopCatName+'&cmprop=title|sortkeyprefix&cmtype=file&cmlimit=max&cmsort=sortkey&cmstartsortkeyprefix='+sortkey+'&cmendsortkeyprefix='+sortkey+'0',
        data: {
            format: 'json'
        },
        dataType: 'jsonp',
        success: function(CommonsCatjsonObject) {GotFiles(CommonsCatjsonObject,name,temprefnum,HasCommonsCat,HasImage)}
        });
    } else {
        GotFiles({"query":{"categorymembers":[]}},name,temprefnum,HasCommonsCat,HasImage)
    }
}

function GotFiles(CommonsCatjsonObject,name,temprefnum,HasCommonsCat,HasImage) {
    for (var c in CommonsCatjsonObject.query.categorymembers) {
        var title=CommonsCatjsonObject.query.categorymembers[c].title.replace("File:","")
        FileMatches[FileMatches.length] = [title,refnum[0]]
    }
    if (!HasImage&&HasCommonsCat&&refnum.length==1) {
        var regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.CommonscatParam+"[ ]*?=.*?(?=(\\n|\\||}}))", "g")
        var commonscat=Rows[currentRow].match(regex)
        regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.CommonscatParam+"[ ]*?=", "g")
        commonscat=commonscat[0].replace(regex,"").replace(/\<\!\-\-(.|[\r\n])*?\-\-\>/g,"").trim()
        commonscat=[commonscat]    // make array to allow compatibility with multiple matched categories below
        $.ajax({
        url: '//commons.wikimedia.org/w/api.php?action=query&list=categorymembers&format=json&cmtitle=Category:'+commonscat[0].replace(/ /g,"_")+'&cmprop=title&cmtype=file&cmlimit=max',
        data: {
            format: 'json'
        },
        dataType: 'jsonp',
        success: function(CommonsCatjsonObject) {GotSuggestedFiles(CommonsCatjsonObject,name,commonscat,temprefnum,HasCommonsCat,HasImage)}
        });
    } else if (!HasImage&&Matches.length!=0&&refnum.length==1) {
        var commonscat=[]
        for (var i=0; i<Matches.length; i++) {
            commonscat.push(Matches[i][0])
        }
        $.ajax({
        url: '//commons.wikimedia.org/w/api.php?action=query&list=categorymembers&format=json&cmtitle=Category:'+commonscat[0].replace(/ /g,"_")+'&cmprop=title&cmtype=file&cmlimit=max',
        data: {
            format: 'json'
        },
        dataType: 'jsonp',
        success: function(CommonsCatjsonObject) {GotSuggestedFiles(CommonsCatjsonObject,name,commonscat,temprefnum,HasCommonsCat,HasImage)}
        });
    } else {
        refnum.splice(0,1)
        CheckForMatches(name,temprefnum,HasCommonsCat,HasImage)  // check next refnum
    }
}

function GotSuggestedFiles(CommonsCatjsonObject,name,commonscat,temprefnum,HasCommonsCat,HasImage) {
    for (var c in CommonsCatjsonObject.query.categorymembers) {
        var title=CommonsCatjsonObject.query.categorymembers[c].title.replace("File:","")
        FileSuggestions[FileSuggestions.length] = [title,commonscat[0]]
    }
    if (commonscat.length!=1) {
        commonscat.splice(0,1)
        $.ajax({
        url: '//commons.wikimedia.org/w/api.php?action=query&list=categorymembers&format=json&cmtitle=Category:'+commonscat[0].replace(/ /g,"_")+'&cmprop=title&cmtype=file&cmlimit=max',
        data: {
            format: 'json'
        },
        dataType: 'jsonp',
        success: function(CommonsCatjsonObject) {GotSuggestedFiles(CommonsCatjsonObject,name,commonscat,temprefnum,HasCommonsCat,HasImage)}
        });
    } else {
        refnum.splice(0,1)
        CheckForMatches(name,temprefnum,HasCommonsCat,HasImage)  // check next refnum
    }
}

var arrows = ""
var scrolled = false // global vars for scrolling mechanism

function isOnScreen(elem) {
    if (elem != null) {
        var height = $(window).height()
        var elemTop = elem.getBoundingClientRect().top
        var elemBot = elemTop + elem.offsetHeight

        if (elemBot<0) {
            return "up"
        } else if (elemTop>height) {
            return "down"
        } else {
            return "onscreen"
        }
    } else {
        return false
    }
}

function ChooseCat(name,temprefnum) {
    var ProgressDiv=document.getElementById("ProgressDiv")
    ProgressDiv.innerHTML+=ThisGUIMessages.Found
    var span=document.createElement("span")
    ProgressDiv.appendChild(span)
    window.onscroll = function () { scrolled = true }
    arrows = setInterval(function() {
        if(scrolled) {
            scrolled = false;
            var direction = isOnScreen(document.getElementById("overlay"))
            if (direction=="up") {
                span.innerHTML = " <a href='#overlay' onclick='setTimeout(function(){window.scrollBy(0,-10-document.getElementById(\"ProgressDiv\").offsetHeight)},50)'>&#8593;</a>" // up arrow
            } else if (direction=="down") {
                span.innerHTML = " <a href='#overlay' onclick='setTimeout(function(){window.scrollBy(0,-10-document.getElementById(\"ProgressDiv\").offsetHeight)},50)'>&#8595;</a>" // down arrow
            } else {
                span.innerHTML = ""
            }
        }
    }, 250);

    if (FileMatches.length>0) {
        var done = "no"
        var loops = 0
        var toQuery=FileMatches.length
        while (done=="no") {
            if (toQuery>20) {
                var TempFileMatches = FileMatches.slice(20*loops,20*(loops+1))
            } else {
                var TempFileMatches = FileMatches.slice(20*loops)
            }
            var FileTitleStr="File:"+TempFileMatches[0][0]
            for (var j=1; j<TempFileMatches.length; j++) {
                FileTitleStr+="|File:"+TempFileMatches[j][0]
            }
            var URLs=JSON.parse(   // get URLs of images to display
                $.ajax({
                    dataType: "json",
                    url: mw.util.wikiScript('api'),
                    data: {
                        format: 'json',
                        action: 'query',
                        prop: 'imageinfo',
                        iiprop: 'url',
                        iiurlwidth: '100',
                        titles: FileTitleStr
                    },
                    async:false
                })
                .responseText
            );
            for (var p in URLs.query.pages) {
                for (var j=0; j<FileMatches.length; j++) {
                    if (URLs.query.pages[p].title.replace(/^.*?:/,"")==FileMatches[j][0]) {
                        for (var k in URLs.query.pages[p].imageinfo) {
                            if (!URLs.query.pages[p].imageinfo[k].thumberror) {
                                FileMatches[j].push(URLs.query.pages[p].imageinfo[k].thumburl)
                            }
                        }
                    }
                }
            }
            if (toQuery>20) {
                toQuery = toQuery-20
                loops++
            } else {
                done = "yes"
            }
        }
        for (var j=0; j<FileMatches.length; j++) {
            if (FileMatches[j].length<3) {FileMatches.splice(j,1); j--} // get rid of files where fetching thumbnails failed
        }
    }
    if (FileSuggestions.length>0) {
        var done = "no"
        var loops = 0
        var toQuery=FileSuggestions.length
        while (done=="no") {
            if (toQuery>20) {
                var TempFileSuggestions = FileSuggestions.slice(20*loops,20*(loops+1))
            } else {
                var TempFileSuggestions = FileSuggestions.slice(20*loops)
            }
            var FileTitleStr="File:"+TempFileSuggestions[0][0]
            for (var j=1; j<TempFileSuggestions.length; j++) {
                FileTitleStr+="|File:"+TempFileSuggestions[j][0]
            }
            var URLs=JSON.parse(   // get URLs of images to display
                $.ajax({
                    dataType: "json",
                    url: mw.util.wikiScript('api'),
                    data: {
                        format: 'json',
                        action: 'query',
                        prop: 'imageinfo',
                        iiprop: 'url',
                        iiurlwidth: '100',
                        titles: FileTitleStr
                    },
                    async:false
                })
                .responseText
            );
            for (var p in URLs.query.pages) {
                for (var j=0; j<FileSuggestions.length; j++) {
                    if (URLs.query.pages[p].title.replace(/^.*?:/,"")==FileSuggestions[j][0]) {
                        for (var k in URLs.query.pages[p].imageinfo) {
                            if (!URLs.query.pages[p].imageinfo[k].thumberror) {
                                FileSuggestions[j].push(URLs.query.pages[p].imageinfo[k].thumburl)
                            }
                        }
                    }
                }
            }
            if (toQuery>20) {
                toQuery = toQuery-20
                loops++
            } else {
                done = "yes"
            }
        }
        for (var j=0; j<FileSuggestions.length; j++) {
            if (FileSuggestions[j].length<3) {FileSuggestions.splice(j,1); j--} // get rid of files where fetching thumbnails failed
        }
    }

    if (Matches.length==0&&FileMatches.length==0&&FileSuggestions.length==0) { // if all matches can't have thumbnails, just skip
        window.clearInterval(arrows)
        currentRow++
        CheckRow(currentRow)
        return
    }

    if (FileMatches.length>0) {
        var fieldsetfiles = document.createElement("fieldset");
        fieldsetfiles.setAttribute("style","margin:0px")
        var legend = document.createElement("legend");
        legend.innerHTML=ThisGUIMessages.FollowingImages.replace("NAME",name+" (#"+temprefnum+")")
        fieldsetfiles.appendChild(legend);
        fieldsetfiles.innerHTML+=ThisGUIMessages.SelectOne+"<br>"

        for (var j=0; j<FileMatches.length; j++) {
            var image = document.createElement("input")
            image.setAttribute("type","radio")
            image.setAttribute("name","imageselect")
            image.setAttribute("id","image"+j)
            if (j==0) image.setAttribute("checked","checked")
            fieldsetfiles.appendChild(image);
            var imagelabel = document.createElement("label")
            imagelabel.setAttribute("for","image"+j)
            imagelabel.innerHTML='<a href="//commons.wikimedia.org/wiki/File:'+encodeURIComponent(FileMatches[j][0])+'" target="_blank"><div style="display:inline-block; vertical-align:middle; width:100px; overflow-x:hidden; text-overflow:ellipsis"><center><img src="'+FileMatches[j][2].replace(/ /g,"_")+'" style="max-height:100%; max-width:100%;"><br>'+FileMatches[j][1]+'<br>'+FileMatches[j][0]+'</center></div></a>'
            fieldsetfiles.appendChild(imagelabel);
            if ((j+1)%5==0) fieldsetfiles.innerHTML+="<br>"
        }
        if (FileSuggestions.length==0) {
            if (FileMatches.length%5!=0) fieldsetfiles.innerHTML+="<br>"
            var skip = document.createElement("input")
            skip.setAttribute("type","radio")
            skip.setAttribute("name","imageselect")
            skip.setAttribute("id","skipimage")
            fieldsetfiles.appendChild(skip);
            var skiplabel = document.createElement("label")
            skiplabel.setAttribute("for","skipimage")
            skiplabel.innerHTML=ThisGUIMessages.DontAddImage
            fieldsetfiles.appendChild(skiplabel);
            fieldsetfiles.innerHTML+="<br>"
        }
        if (Matches.length==0&&FileSuggestions.length==0) {
            var selectbutton=document.createElement("input")
            selectbutton.setAttribute("type", "button")
            selectbutton.setAttribute("value", ThisGUIMessages.SelectButton)
            selectbutton.setAttribute("style","margin-top:7px")
            selectbutton.setAttribute("onclick", "CatChosen()");
            fieldsetfiles.appendChild(selectbutton)
        }
    }
    if (FileSuggestions.length>0) {
        var fieldsetfilesuggestions = document.createElement("fieldset");
        fieldsetfilesuggestions.setAttribute("style","margin:0px")
        var legend = document.createElement("legend");
        if (FileMatches.length!=0) {
            legend.innerHTML=ThisGUIMessages.OtherSuggested.replace("NAME",name+" (#"+temprefnum+")")
        } else {
            legend.innerHTML=ThisGUIMessages.Suggested.replace("NAME",name+" (#"+temprefnum+")")
        }
        fieldsetfilesuggestions.appendChild(legend);
        fieldsetfilesuggestions.innerHTML+=ThisGUIMessages.SelectOne+"<br>"

        for (var j=0; j<FileSuggestions.length; j++) {
            var image = document.createElement("input")
            image.setAttribute("type","radio")
            image.setAttribute("name","imageselect")
            image.setAttribute("id","imagesuggestion"+j)
            if (j==0&&FileMatches.length==0) image.setAttribute("checked","checked")
            fieldsetfilesuggestions.appendChild(image);
            var imagelabel = document.createElement("label")
            imagelabel.setAttribute("for","imagesuggestion"+j)
            imagelabel.innerHTML='<a href="//commons.wikimedia.org/wiki/File:'+encodeURIComponent(FileSuggestions[j][0])+'" target="_blank"><div style="display:inline-block; vertical-align:middle; width:100px; overflow-x:hidden; text-overflow:ellipsis"><center><img src="'+FileSuggestions[j][2].replace(/ /g,"_")+'" style="max-height:100%; max-width:100%;"><br>('+ThisGUIMessages.From.replace('CATNAME','Category:'+FileSuggestions[j][1])+')<br>'+FileSuggestions[j][0]+'</center></div></a>'
            fieldsetfilesuggestions.appendChild(imagelabel);
            if ((j+1)%5==0) fieldsetfilesuggestions.innerHTML+="<br>"
        }
        if (FileSuggestions.length%5!=0) fieldsetfilesuggestions.innerHTML+="<br>"
        var skip = document.createElement("input")
        skip.setAttribute("type","radio")
        skip.setAttribute("name","imageselect")
        skip.setAttribute("id","skipimagesuggestions")
        fieldsetfilesuggestions.appendChild(skip);
        var skiplabel = document.createElement("label")
        skiplabel.setAttribute("for","skipimagesuggestions")
        skiplabel.innerHTML=ThisGUIMessages.DontAddImage
        fieldsetfilesuggestions.appendChild(skiplabel);
        fieldsetfilesuggestions.innerHTML+="<br>"

        if (Matches.length==0) {
            var selectbutton=document.createElement("input")
            selectbutton.setAttribute("type", "button")
            selectbutton.setAttribute("value", ThisGUIMessages.SelectButton)
            selectbutton.setAttribute("style","margin-top:7px")
            selectbutton.setAttribute("onclick", "CatChosen()");
            fieldsetfilesuggestions.appendChild(selectbutton)
        }
    }
    if (Matches.length>0) {
        var fieldsetcats = document.createElement("fieldset");
        fieldsetcats.setAttribute("style","margin:0px")
        var legend = document.createElement("legend");
        legend.innerHTML=ThisGUIMessages.FollowingCats.replace("NAME",name+" (#"+temprefnum+")")
        fieldsetcats.appendChild(legend);
        fieldsetcats.innerHTML+=ThisGUIMessages.SelectOne+"<br>"

        for (var j=0; j<Matches.length; j++) {
            var cat = document.createElement("input")
            cat.setAttribute("type","radio")
            cat.setAttribute("name","catselect")
            cat.setAttribute("id","cat"+j)
            if (j==0) cat.setAttribute("checked","checked")
            fieldsetcats.appendChild(cat);
            var catlabel = document.createElement("label")
            catlabel.setAttribute("for","cat"+j)
            catlabel.innerHTML=Matches[j][1]+' – <a href="//commons.wikimedia.org/wiki/Category:'+encodeURIComponent(Matches[j][0])+'" target="_blank">'+Matches[j][0]+'</a>'
            fieldsetcats.appendChild(catlabel);
            fieldsetcats.innerHTML+="<br>"
        }
        var skip = document.createElement("input")
        skip.setAttribute("type","radio")
        skip.setAttribute("name","catselect")
        skip.setAttribute("id","skip")
        fieldsetcats.appendChild(skip);
        var skiplabel = document.createElement("label")
        skiplabel.setAttribute("for","skip")
        skiplabel.innerHTML=ThisGUIMessages.DontAddCat
        fieldsetcats.appendChild(skiplabel);
        fieldsetcats.innerHTML+="<br>"

        var selectbutton=document.createElement("input")
        selectbutton.setAttribute("type", "button")
        selectbutton.setAttribute("value", ThisGUIMessages.SelectButton)
        selectbutton.setAttribute("style","margin-top:7px")
        selectbutton.setAttribute("onclick", "CatChosen()");
        fieldsetcats.appendChild(selectbutton)
    }
    var overlay = document.createElement("tr")
    var td=document.createElement("td")
    td.setAttribute("colspan",HTMLRows[currentRow].getElementsByTagName("td").length+1)
    overlay.appendChild(td)
    if (FileMatches.length>0) td.appendChild(fieldsetfiles)
    if (FileSuggestions.length>0) td.appendChild(fieldsetfilesuggestions)
    if (Matches.length>0) td.appendChild(fieldsetcats)
    overlay.setAttribute("style", "outline:2px solid red; border-top:0px; background:#ffffff")
    overlay.setAttribute("id","overlay")
    HTMLRows[currentRow].setAttribute("style","border-bottom:0px;")
    HTMLRows[currentRow].parentNode.insertBefore(overlay, HTMLRows[currentRow].nextSibling)
}

function CatChosen() {
    var overlay=document.getElementById("overlay")
    var options=overlay.getElementsByTagName("input")
    var titles=overlay.getElementsByTagName("label")
    for (var i=0; i<options.length-1; i++) {
        if (options[i].checked) {
            if (options[i].name.search("cat")!=-1) {
                var link=titles[i].getElementsByTagName("a")
                if (link.length==0) continue    // if chose not to add cat
                MatchesFound = true             // if chose to add something
                var catname=link[0].innerHTML
                var tempRow = Rows[currentRow]
                var regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.CommonscatParam+"[ ]*?=.*?(?=(\\n|\\||}}))", "g")
                if (tempRow.match(regex)!=null) { // if parameter is already there but blank, put selection there
                    tempRow=tempRow.replace(regex, "|"+ThisRegisterTable.CommonscatParam+"="+catname)
                } else {
                    var EndOfTemplate=tempRow.lastIndexOf("}}")
                    if (tempRow.indexOf("\n")!=-1&&tempRow.indexOf("\n")!=0&&tempRow.indexOf("\n")!=tempRow.length-1) {
                        tempRow = tempRow.substr(0,EndOfTemplate)+"|"+ThisRegisterTable.CommonscatParam+"="+catname+"\n}}"+tempRow.substr(EndOfTemplate+2,tempRow.length-EndOfTemplate-2)
                    } else {
                        tempRow = tempRow.substr(0,EndOfTemplate)+"|"+ThisRegisterTable.CommonscatParam+"="+catname+"}}"+tempRow.substr(EndOfTemplate+2,tempRow.length-EndOfTemplate-2)
                    }
                }
                thiswikitext=thiswikitext.replace(Rows[currentRow],tempRow)
                Rows[currentRow] = tempRow
            } else {
                var link=titles[i].getElementsByTagName("a")
                if (link.length==0) continue    // if chose not to add image
                MatchesFound = true             // if chose to add something
                var imagename=link[0].innerHTML
                var StartIndex=imagename.lastIndexOf("<br>")+4
                imagename=imagename.substr(StartIndex,imagename.indexOf("</center>",StartIndex)-StartIndex)
                var tempRow = Rows[currentRow]
                var regex = new RegExp("\\|[ ]*?"+ThisRegisterTable.ImageParam+"[ ]*?=.*?(?=(\\n|\\||}}))", "g")
                if (tempRow.match(regex)!=null) { // if parameter is already there but blank, put selection there
                    tempRow=tempRow.replace(regex, "|"+ThisRegisterTable.ImageParam+"="+imagename)
                } else {
                    var EndOfTemplate=tempRow.lastIndexOf("}}")
                    if (tempRow.indexOf("\n")!=-1&&tempRow.indexOf("\n")!=0&&tempRow.indexOf("\n")!=tempRow.length-1) {
                        tempRow = tempRow.substr(0,EndOfTemplate)+"|"+ThisRegisterTable.ImageParam+"="+imagename+"\n}}"+tempRow.substr(EndOfTemplate+2,tempRow.length-EndOfTemplate-2)
                    } else {
                        tempRow = tempRow.substr(0,EndOfTemplate)+"|"+ThisRegisterTable.ImageParam+"="+imagename+"}}"+tempRow.substr(EndOfTemplate+2,tempRow.length-EndOfTemplate-2)
                    }
                }
                thiswikitext=thiswikitext.replace(Rows[currentRow],tempRow)
                Rows[currentRow] = tempRow
            }
        }
    }
    overlay.parentNode.removeChild(overlay)
    window.clearInterval(arrows)
    currentRow++
    CheckRow(currentRow)
}

function getThisWikitext(title) {
    try {
        var output=JSON.parse(
            $.ajax({
                dataType: "json",
                url: mw.util.wikiScript('api'),
                data: {
                    format: 'json',
                    action: 'query',
                    prop: 'revisions',
                    rvprop: 'content',
                    titles: title,
                    indexpageids: true,
                    redirects: 'true'
                },
                async:false
            })
            .responseText
        );
        for (var page in output.query.pages) {
            wikitext = output.query.pages[page].revisions[0]['*'];
        }
        return wikitext
    }
    catch(err) {
        return "error"
    }
}

function editPageWithCommonsCat(info,ProgressDiv) {
    var ProgressDiv=document.getElementById("ProgressDiv")
    ProgressDiv.innerHTML+=ThisGUIMessages.Done+" "

    $.ajax({
        url: mw.util.wikiScript( 'api' ),
        type: 'POST',
        dataType: 'json',
        data: {
            format: 'json',
            action: 'edit',
            title: info.title,
            text: info.text,
            summary: info.summary,
            token: mw.user.tokens.get( 'editToken' )
        }
    })
        .done (function( data ) {
            window.onbeforeunload = function() {}
            if (data && data.edit && data.edit.result && data.edit.result == 'Success') {
                ProgressDiv.innerHTML+=ThisGUIMessages.SuccessfulEdit
            } else {
                ProgressDiv.innerHTML+=ThisGUIMessages.FailedEdit
            }
            setTimeout(function(){ProgressDiv.parentNode.removeChild(ProgressDiv)}, 10000);
        })
        .fail (function() {
            alert('Ajax failure.');
        });
}

function OnBeforeUnLoad () {
    return ThisGUIMessages.LeavePage;
}

addOnloadHook(FindCommonsCatButton);