User:Ilmari Karonen/fixconverttosvg.js

From Wikipedia, the free encyclopedia
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.
// SORT CONVERT-TO-SVG TAGS:
$(function () {
    var editForm = document.forms.editform;
    if (!editForm || wgNamespaceNumber != 6 || wgAction != 'edit') return;
    var type = /[?&]fixconverttosvg=([^&]*)/.exec(window.location.search);
    if (!type) return;
    type = decodeURIComponent(type[1]);
    editForm.wpTextbox1.value = editForm.wpTextbox1.value.replace(/\{\{\s*((Convert[_ ]?to[_ ]?)?SVG)(\s*\|[^|}]+)?[|\s]*\}\}/ig, "{"+"{$1|"+type+"}}");
    editForm.wpSummary.value = "Javascript-assisted sorting of {"+"{Convert to SVG}} tags";
    editForm.wpMinoredit.checked = true;
    editForm.wpSave.click();  // I'm feeling lucky!
});

$(function () {
    if (wgPageName != 'Category:Images_that_should_use_vector_graphics') return;

    // allow overriding of default type list
    var types = window.convertToSVGTypes || "alphabet, art, biology, chemical, chemistry, circuit, coat of arms, diagram, emblem, flag, graph, icon, logo, map, math, military insignia, musical notation, physical, signature, symbol, text";
    if (typeof(types) == 'string') types = types.split(/,\s*/);

    var links = document.getElementById('content').getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        if (!links[i].title || !links[i].title.match(/^File:/) ||
            links[i].parentNode.className != 'gallerytext') continue;
        var img = encodeURIComponent(links[i].title.replace(/^File:/, "").replace(/ /g, "_"));
        var frag = document.createDocumentFragment();
        frag.appendChild(document.createElement('br'));
        for (var type in types) {
            var fixLink = document.createElement('a');
            fixLink.href =
                wgScript + "?title=File:" + img +
                "&action=edit&fixconverttosvg=" + encodeURIComponent(types[type]);
            fixLink.appendChild(document.createTextNode(types[type]));
            frag.appendChild(document.createTextNode(" ("));
            frag.appendChild(fixLink);
            frag.appendChild(document.createTextNode(") "));
        }
        links[i].parentNode.insertBefore(frag, links[i].nextSibling);
    }
});