Jump to content

User:Ilmari Karonen/fixarxivlinks.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ilmari Karonen (talk | contribs) at 06:36, 23 June 2008 (testing...). 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.
// FIX LINKS TO TEMPORARY arxiv.org/PS_cache URLS TO PERMANENT ONES:

// To use, go to Special:Linksearch, search for "arxiv.org/PS_cache" (or same with "www." prefix) and click the "FIX" links.

var arXivPSCacheRegex = "http://([Ww]{3}\\.|)[Aa][Rr][Xx][Ii][Vv]\\.[Oo][Rr][Gg]/PS_cache/([\\-.\\w]+)/(pdf|ps)/(\\d{4})/\\4(\\.?\\d+(v\\d+|))\\.\\3";

if (wgCanonicalNamespace == "Special" && wgCanonicalSpecialPageName == "Linksearch") addOnloadHook(function () {
    // add quick edit links to Special:Linksearch
    var ol = document.getElementsByTagName("ol")[0];
    if (!ol) return;

    var re = new RegExp("^" + arXivPSCacheRegex + "$");

    for (var li = ol.firstChild; li; li = li.nextSibling) {
        if (li.nodeType != 1 || li.tagName.toLowerCase() != "li") continue;

        var links = li.getElementsByTagName("a");
        if (links.length != 2 || !re.test(links[0].href) || !/^\//.test(links[1].href)) continue;

        var title = links[1].title;

        var editlink = document.createElement("a");
        editlink.href = wgScript + "?title=" + encodeURIComponent(title) + "&action=edit&fixarxivlinks=1";
        editlink.title = "Fix arXiv/PS_cache links in " + title;
        editlink.appendChild(document.createTextNode("FIX"));

        li.insertBefore(editlink, li.firstChild); 
        li.insertBefore(document.createTextNode(" ("), editlink); 
        li.insertBefore(document.createTextNode(") "), editlink.nextSibling); 
    }
});

if (wgAction == "edit" && /[?&]fixarxivlinks=/.test(window.location.search)) addOnloadHook(function () {
    var editForm = document.forms.editform;
    if (!editform) return;
    var re = new RegExp("\\b" + arXivPSCacheRegex + "\\b", "g");
    editForm.wpTextbox1.value = editForm.wpTextbox1.value.replace(re, "http://arxiv.org/$3/$2/$4$5");
    editForm.wpTextbox1.value = editForm.wpTextbox1.value.replace(/^\bhttp:\/\/arxiv\.org\/(ps|pdf)\/arxiv\//g, "http://arxiv.org/$1/");
    editForm.wpSummary.value = "Fixing temporary arxiv.org/PS_cache URLs per http://arxiv.org/help/arxiv_identifier_for_services with [[User:Ilmari Karonen/fixarxivlinks.js|script assistance]]";
    editForm.wpMinoredit.checked = true;
    editForm.wpDiff.click();  // Not feeling lucky yet...
});