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 11:45, 23 June 2008 (also try to autofix "Preprint on arXiv (PDF)" link texts). 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:
// SEE ALSO: http://arxiv.org/help/arxiv_identifier_for_services

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

var arXivPSCacheRegex = "http://(?:www\\.|)arxiv\\.org/(?:PS_cache|ftp)/(?:arxiv/|([\\-.\\w]*/))(?:pdf|ps|papers)/(\\d{4})/(\\2\\.?\\d+(v\\d+|))\\.(?:ps|pdf)";

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 + "$", "i");

    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) continue;
        if (!re.test(links[0].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(?:\\s*\\|\\s*format\\s*=\\s*(?:PDF|PS|PostScript)\\b|(\\s+Preprint on arXiv)\\s*\\(pdf\\))?", "ig");
    editForm.wpTextbox1.value = editForm.wpTextbox1.value.replace(re, "http://arxiv.org/abs/$1$3$4");
    editForm.wpSummary.value = "Fixing temporary \"arxiv.org/PS_cache\" and obsolete \"arxiv.org/ftp\" URLs to link to abstract page with download links instead (with [[User:Ilmari Karonen/fixarxivlinks.js|script assistance]])";
    editForm.wpMinoredit.checked = true;
    editForm.wpDiff.click();  // Not feeling lucky yet...
});