User:Brighterorange/svg.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Brighterorange/svg. |
// 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>