User:Remember the dot/monobook.js

This is an old revision of this page, as edited by Remember the dot (talk | contribs) at 17:51, 15 September 2009 (revert). 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.
importScript("User:Lightmouse/monobook.js/script.js")
importScript("User:Remember the dot/ISO date format unifier.js")
importScript("User:Remember the dot/Syntax highlighter.js")

/* Three clicks and it's on the Commons */

if (wgCanonicalNamespace == "File")
{
    addOnloadHook(function()
    {
        document.getElementById("p-tb").getElementsByTagName("div")[0].getElementsByTagName("ul")[0].innerHTML += "<li><a href='javascript:secondClickToCommons()'>Move image to the Wikimedia Commons</a></li>"
    })
}

function secondClickToCommons()
{
    var commonsHelper = document.createElement("object")
    commonsHelper.data = "http://tools.wikimedia.de/~magnus/commonshelper.php?language=en&image=" + encodeURIComponent(wgPageName.substr(6)) + "&project=wikipedia&commonsense=1&reallydirectupload=1#du"
    commonsHelper.type = "application/xhtml+xml"
    commonsHelper.style.width = "100%"
    commonsHelper.style.height = "44em"
    
    var explanation = document.createElement("div")
    explanation.innerHTML = "<h3>Instructions</h3> Click \"Get text\", <b>wait for the image to finish uploading</b>, then click <input type='button' value='done' onclick='thirdClickToCommons()'/>"
    explanation.style.paddingLeft = "1ex"
    document.getElementById("content").appendChild(commonsHelper)
    document.getElementById("content").appendChild(explanation)
}

function thirdClickToCommons()
{
    var request = new XMLHttpRequest()
    var parameters
    
    if (wgUserGroups.indexOf("sysop") == -1)
    {
    }
    else
    {
        var apiRequest = new XMLHttpRequest()
        apiRequest.open("GET", wgServer + wgScriptPath + "/api.php?action=query&format=xml&prop=info&intoken=delete&titles=" + encodeURIComponent(wgPageName), false)
        apiRequest.send(null)
        parameters = "title=" + encodeURIComponent(wgPageName) + "&action=delete&wpReason=Speedily%20deleted%20per%20%5B%5BWP%3ACSD%23I8%7CCSD%20I8%5D%5D%2C%20was%20an%20image%20available%20as%20a%20bit-for-bit%20identical%20copy%20on%20the%20Wikimedia%20Commons.&wpEditToken=" + encodeURIComponent(apiRequest.responseXML.evaluate("/api/query/pages/page/@deletetoken", apiRequest.responseXML, null, XPathResult.STRING_TYPE, null).stringValue)
        
        request.open("POST", wgServer + wgScript, false)
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
        request.setRequestHeader("Content-length", parameters.length)
        request.setRequestHeader("Connection", "close")
        request.send(parameters)
    }
    
    window.___location.reload()
}

addOnloadHook(function() {
    document.getElementById("column-one").insertBefore(document.getElementById("p-search"), document.getElementById("p-cactions"))
})


/* format citation templates in a clean, consistent manner (experimental) */

if (wgAction == "edit" || wgAction == "submit")
{
    addOnloadHook(function()
    {
        document.getElementById("p-tb").getElementsByTagName("div")[0].getElementsByTagName("ul")[0].innerHTML += "<li><a href='javascript:formatCitationTemplates()'>Format citation templates</a></li>"
    })
}

function trimRight(string)
{
    return string.replace(/\s+$/,"");
}

function formatCitationTemplates()
{
    var wpTextbox1 = document.getElementById("wpTextbox1") 
    var text = ""
    
    var templateExp = /{{\s*([Cc]ite[^\|\}]*)([\s\S]*?)}}/
    var remainderOfText = wpTextbox1.value
    for (var templateMatch = templateExp.exec(remainderOfText); templateMatch != null; templateMatch = templateExp.exec(remainderOfText))
    {
        var result = "{{" + trimRight(templateMatch[1]) + "\n"
        var rawTemplateParameters = templateMatch[2]
        var parameterExp = /\|\s*([^=\s]*)\s*=\s*([^\|\}]*)/g
        for (var parameterMatch = parameterExp.exec(rawTemplateParameters); parameterMatch != null; parameterMatch = parameterExp.exec(rawTemplateParameters))
        {
            if (parameterMatch[2] != "") //if the parameter is not blank
            {
                result += "|" + parameterMatch[1] + "=" + trimRight(parameterMatch[2]) + "\n"
            }
        }
        result += "}}"
        
        text += remainderOfText.substring(0, templateMatch.index) + result
        remainderOfText = remainderOfText.substring(templateMatch.index + templateMatch[0].length)
    }
    wpTextbox1.value = text + remainderOfText
}


if (wgNamespaceNumber == 0 || wgNamespaceNumber == 1)
{
    addOnloadHook(function()
    {
        document.getElementById("ca-nstab-main").firstChild.firstChild.nodeValue = "Entry"
    })
}

//automated XHTML compliance fixer
addOnloadHook(function()
{
    if (document.getElementById("wpTextbox1"))
    {
        var wpTextbox1 = document.getElementById("wpTextbox1")
        var refRegex = /<ref\s+name\s*=\s*([^\/"\s]+?)\s*(\/|)\s*>/
        var text = ""
        var remainderOfText = wpTextbox1.value
        
        for (var templateMatch = refRegex.exec(remainderOfText); templateMatch != null; templateMatch = refRegex.exec(remainderOfText))
        {
            text += remainderOfText.substring(0, templateMatch.index) + "<ref name=\"" + templateMatch[1] + "\"" + templateMatch[2] + ">"
            remainderOfText = remainderOfText.substring(templateMatch.index + templateMatch[0].length)
        }
        wpTextbox1.value = text + remainderOfText
        
        //wpTextbox1.value = wpTextbox1.value.replace(/<br\/>/g, "<br/>")
    }
})