User:Ilmari Karonen/delinkerloglink.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * Adds a link to the CommonsDelinker log to deletion log entries.
 */
addOnloadHook(function () {
    // guard against multiple inclusion
    if (window.commonsDelinkerLogLinksAdded) return;
    window.commonsDelinkerLogLinksAdded = true;

    var content = document.getElementById("bodyContent") ||       // monobook & vector skins
                  document.getElementById("mw_contentholder") ||  // modern skin
                  document.getElementById("article");             // classic skins
    if (!content) return;

    var deletions = getElementsByClassName(content, "li", "mw-logline-delete");
    if (!deletions || !deletions.length) return;

    // create the links in advance so we can cloneNode() them quickly in the loop
    var guLink = document.createElement("a");
    guLink.className = "delinker-log-globalusage";
    guLink.appendChild(document.createTextNode("global usage"));

    var cdLink = document.createElement("a");
    cdLink.className = "delinker-log-link extiw";
    cdLink.appendChild(document.createTextNode("delinker log"));
            
    var span = document.createElement("span");
    span.className = "delinker-log-links";
    span.appendChild(document.createTextNode(" ("));
    span.appendChild(guLink);
    span.appendChild(document.createTextNode("; "));
    span.appendChild(cdLink);
    span.appendChild(document.createTextNode(")"));

    for (var i = 0; i < deletions.length; i++) {
        var match = null;
        for (var elem = deletions[i].firstChild; elem; elem = elem.nextSibling) {
            if (!elem.tagName || elem.tagName.toLowerCase() != 'a') continue;
            if (/mw-userlink/.test(elem.className)) continue;
            match = /^File:(.*)/.exec(getInnerText(elem));
            if (match) break;
        }
        if (match) {
            var filename = encodeURIComponent(match[1].replace(/ /g, "_"));
            guLink.href = wgScript + "?title=Special:GlobalUsage&target=" + filename;
            guLink.title = "Current usage of " + match[1] + " on all Wikimedia projects";
            cdLink.href = "http://toolserver.org/~delinker/index.php?image=" + filename;
            cdLink.title = "CommonsDelinker log for " + match[1];
            deletions[i].appendChild(span.cloneNode(true));
        }
    }
});