Jump to content

User:Brighterorange/svg.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.
// This script adds an "SVG" tab when editing image description pages.
// that tab adds the ShouldBeSVG template to the page with a Format
// section and submits. Written by :en:user:brighterorange. Distribute freely.

// TODO: 
// - Detect when the image is actually on Commons (how?) and don't apply then
//    (or go there and ConvertToSVG)
// - Show when just viewing an image; should edit, add message, submit
// - if a JPEG, also BadJPEG template?

// <nowiki><pre>
// maybe this stuff should be in a separate library?
function addlilink(tabs, url, name, id, title, key){
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    if(id)
    {
        if(key && title)
        {
            ta[id] = [key, title];
        }
        else if(key)
        {
            ta[id] = [key, ''];
        }
        else if(title)
        {
            ta[id] = ['', title];
        }
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
}

function addTab(url, name, id, title, key){
    var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
    return addlilink(tabs, url, name, id, title, key)
};

// the appropriate template depends on where we are
function getSVGtemplate() {
  // split up templates so that mediawiki doesn't expand
  // and put the script in the category (!?)
  if (document.title.indexOf("Wikimedia Commons") == -1) return "{{Should" + "BeSVG}}";
  else return "{{Convert" + " to SVG}}";
};

function doShouldbeSVG() {
  var old = document.editform.wpTextbox1.value;
  // don't use toLower or toUpper, since with some letters (German eszet?)
  // that makes single characters multiple, throwing off indices
  var ci = svgFindAnyRight(old, ['[[category:', '[[Category:']);
  if (ci.i != undefined && ci.i >= 0) {
    document.editform.wpTextbox1.value = old.substr(0, ci.i) +
      '\n== Format ==\n' + getSVGtemplate() + '\n\n' +
      old.substring(ci.i);
  } else {
    // no category links; put at end
    document.editform.wpTextbox1.value = old + 
      '\n\n== Format ==\n' + getSVGtemplate() + '\n';
  }
  document.editform.wpSummary.value = 'Should be [[:en:SVG|SVG]]';
  document.editform.wpMinoredit.checked = false;
  document.editform.submit();
};

 function svgFindAnyRight(str, finds) {
    var earliest = undefined;
    var earliesti = undefined;
    for(var i = 0; i < finds.length; i ++) {
       var x = str.indexOf(finds[i]);
       if (earliesti == undefined || x < earliesti) {
        earliest = finds[i];
        earliesti = x;
       }
    }
    return { i : earliesti, s : earliest };
 };

function addSvg() {
  addTab("javascript:doShouldbeSVG()", "svg", "ca-svg", "Should Be SVG", "");
  akeytt();
};

$(function() {
  // only if on the description page of an image

  if (document.title.indexOf("Editing Image:") == -1) return;
  addOnloadHook(addSvg);
});
// </pre></nowiki>