Jump to content
Wikimedia Commons

User:Menner/svg-button-dev.js

From Wikimedia Commons, the free media repository
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
Documentation for this user script can be added at User:Menner/svg-button-dev.
 /**
  * SVG Button (sb)
  * 
  * SVG is a troublesome file format. To improve usability some assistance is
  * helpful. The SVG Button appears on every file page presenting a SVG file.
  * It gives linkes to support tools, SVG help page and Commons Village pump.
  * 
  * Author: Menner @ Wikimedia Commons
  * 
  */
 (function($,mw){// Making my own namespace!?
 "use strict";
 /**
  * GLOBALS
  */
 varonlyLoggedinUsers=true;// SVG Button only visible to loggedin users

 /**
  * Creates a DOM element of type <p> containing a named link
  */
 functionmakepLink(name,url)
 {
 vartext=document.createTextNode(name);
 vara=document.createElement('a');
 varp=document.createElement('p');
 a.setAttribute('href',url);
 a.appendChild(text);
 p.appendChild(a);
 returnp;
 }

 /**
  * Helper that extracts the fully qualified url of the original file 
  * represented by a file description page
  */
 functionfullFilePath()
 {
 varfullImageLink=$('#file.fullImageLink').get(0);
 returnfullImageLink.firstChild.getAttribute('href');
 }

 /**
  * Helper that checks if current user is listed as uploader of a file
  */
 functionmatchUploader()
 {
 varuser=mw.config.get('wgUserName');
 varret=false;
 // Does it improve performance to filter of 
 // id="mw-imagepage-section-filehistory" before?
 varobjUploaders=$('.mw-userlink');// Filter uploader DOM
 for(vari=0;i<objUploaders.length;i++){
 varobjUploader=objUploaders[i];
 varuploader=objUploader.getAttribute('title');
 uploader=uploader.split(":",2).pop();// remove "User:"
 if(uploader===user){
 ret=true;
 break;
 }
 }

 returnret;
 }

 /**
  * Internationalize label of SVG Button
  */
 functionlabelSvgHelp()
 {
 vari18n={
 en:'SVG help',
 de:'SVG Hilfe',
 es:'SVG ayuda',
 fr:'SVG aide',
 };
 returni18n[mw.config.get('wgUserLanguage')]||i18n.en;
 }
 /**
  * Internationalize link title for SVG help page
  */
 functiontitleSvgHelpPage()
 {
 vari18n={
 en:'SVG help page',
 de:'SVG Hilfeseite',
 es:'SVG página de ayuda',
 fr:'SVG page d\'aide',
 };
 returni18n[mw.config.get('wgUserLanguage')]||i18n.en;
 }

 /**
  * Internationalized wiki links
  */
 functionlinkSvgHelp()
 {
 vari18n={
 en:'Help:SVG',
 de:'Help:SVG/de',
 es:'Help:SVG/es',
 fr:'Help:SVG/fr',
 };
 returni18n[mw.config.get('wgUserLanguage')]||i18n.en;
 }

 /**
  * Add SVG help button to file descrition page
  */
 functionsvgButtonLoader()
 {
 //https://www.mediawiki.org/wiki/Using_OOjs_UI_in_MediaWiki#JavaScript
 varlinks;
 varclear;
 varsvgHelp;
 if(matchUploader()===false){
 return;// Skip button if we arn't one of the uploaders
 }
 try{
 clear=document.createElement('div');
 clear.setAttribute('style','clear: both;');

 try{
 links=newArray(
 makepLink(titleSvgHelpPage(),'/wiki/'+linkSvgHelp()),
 makepLink('W3C validator','//validator.w3.org/check?uri='+fullFilePath()),
 makepLink('Wikimedia SVG check','//tools.wmflabs.org/svgcheck/'),
 makepLink('Graphics village pump','/wiki/'+'Commons:Graphics village pump')
 );// might fail if MediaWiki can't render the SVG
 }catch(err){
 console.log('Error in svg-button.js - creating links failed: '+err.message);
 links=null;
 }

 svgHelp=newOO.ui.PopupButtonWidget({
 label:labelSvgHelp(),
 icon:'menu',
 popup:{
 $content:$(links),
 padded:true,
 align:'left'
 }
 });
 svgHelp.getPopup().width=220;
 $('.fullMedia').append(clear);
 $('.fullMedia').append(svgHelp.$element);
 }catch(err){
 console.log('Error in svg-button.js: '+err.message);
 }
 }

 /**
  * str ends with suffix?
  */
 functionendsWith(str,suffix){
 returnstr.indexOf(suffix,str.length-suffix.length)!==-1;
 }

 /**
  * M..A..I..N
  */
 if(onlyLoggedinUsers===false||mw.user.isAnon()===false){
 if(endsWith(mw.config.get("wgTitle").toLowerCase(),".svg")
 ===true){
 // First wait for oojs-ui then for jQuery
 mw.loader.using('oojs-ui').done(
 function(){$(svgButtonLoader())}
 );
 }
 }

 }(jQuery,mediaWiki));

AltStyle によって変換されたページ (->オリジナル) /