As an anonymous user, you can only add new data. If you would like to also modify existing data, please create an account and indicate your languages on your user page.

MediaWiki:Common.js/SideIcons.js

From OmegaWiki
Jump to: navigation, search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences
/* Any JavaScript here will be loaded for all users on every page load. */
 
addOnloadHook(displayIconsRight);
 
function displayIconsRight() {
 
  if ( ! (( wgAction == 'view' ) || ( wgAction == 'purge' )) ) return;
  if ( ! (( wgCanonicalNamespace == 'Expression' ) || ( wgCanonicalNamespace == 'DefinedMeaning' )) ) return ;
 
  // determines sense of reading
  var ltr=1;
  var htmltag = document.getElementsByTagName( 'html' );
  if ( htmltag[0].getAttribute('dir') == 'rtl' ) {
    ltr=0;
  }
 
  // Adds icons to the right for links to Wikipedia and Commons
  var definitions = new Array();
  if ( wgCanonicalNamespace == 'Expression' ) {
    definitions = $(".expand-exact-record,.expand-record");
  }
  if ( wgCanonicalNamespace == 'DefinedMeaning' ) definitions = $("#mw-content-text");
 
  var logosize = 50;
 
  for (var def = 0; def < definitions.length ; def++) {    
    var hrefCommons = '';
    var hrefImg = '';
    var hrefWikidata = '';
    var hrefWikiquote = '';
    var hrefWikisource = '';
    var regWikidata=/\(1434066\)/g ; // Wikidata ID DM
    var regWikiquote=/\(1443393\)/g ; // Wikiquote DM
    var regWikisource=/\(1443425\)/g ; // Wikisource DM
    var regCommons=/\(983569\)/g ; // Commons cat link DM
    var regImg=/\(1281905\)/g ; // Commons image link DM
 
    var ahrefs = $(definitions[def]).find("a");
 
    for (var i = 0; i < ahrefs.length ; i++) {    
      var Wikiarticle = ahrefs[i].getAttribute('href');
 
      if ( Wikiarticle == null ) continue ;
 
      if ( Wikiarticle.match(regWikidata) ) {
          hrefWikidata = ahrefs[i] ;
          continue ;
      } // if Wikiarticle match regWikidata
 
      if ( Wikiarticle.match(regCommons) ) {
          hrefCommons = ahrefs[i] ;
          continue ;
      }
      if ( Wikiarticle.match(regWikisource) ) {
          hrefWikisource = ahrefs[i] ;
          continue ;
      }
      if ( Wikiarticle.match(regWikiquote) ) {
          hrefWikiquote = ahrefs[i] ;
          continue ;
      }
 
      if ( Wikiarticle.match(regImg) ) {
          hrefImg = ahrefs[i] ;
          continue ;
      } // if Wikiarticle match regImg
    } // for href
 
    var ICONdiv = document.createElement('div');
    var whereto = '' ;
    if ( ltr == 0 ) {
      $(ICONdiv).css('float','left');
    } else {
      $(ICONdiv).css('float','right');
    }
    $(ICONdiv).css('text-align', 'center').width(200);
 
    // DISPLAYING IMAGES
    if ( hrefImg != '' ) {
      var Imgdiv = document.createElement('div');
      var Imga = document.createElement('a');
      var Imgimg = document.createElement('img');
      var theBR = document.createElement('br');
 
      var fullUrlImg = new String(hrefImg.parentNode.nextSibling.firstChild) ;
      Imga.setAttribute('href', fullUrlImg );
 
      var namePos = 0 ;
      if ( fullUrlImg.indexOf ( 'File:' ) > 0 ) namePos = fullUrlImg.indexOf ( 'File:' ) + 5 ;
      if ( fullUrlImg.indexOf ( 'Image:' ) > 0 ) namePos = fullUrlImg.indexOf ( 'Image:' ) + 6 ;
      var nameOfImg = fullUrlImg.substr(namePos) ;
      var checksum = MD5 ( decodeURI( nameOfImg ) ) ;
      var imgRealUrl = "http://upload.wikimedia.org/wikipedia/commons/thumb/" + checksum.substr(0,1) + "/" + checksum.substr(0,2) + "/" + nameOfImg + "/200px-" + nameOfImg ;
      if ( nameOfImg.substr(nameOfImg.length - 3) == "svg" ) imgRealUrl += ".png"
 
      Imgimg.setAttribute('src',imgRealUrl);
      Imgimg.setAttribute('class','dm-img');
      Imgimg.setAttribute('alt','');
      Imgdiv.setAttribute('style','text-align: center;');
 
      Imgdiv.appendChild( theBR ) ;
      Imgdiv.appendChild( Imga ) ;
      Imga.appendChild( Imgimg ) ;
      Imga.innerHTML += '<br />' + hrefImg.innerHTML + '<br />' ;
      Imgdiv.appendChild( theBR ) ;
      ICONdiv.appendChild ( Imgdiv ) ;
 
      whereto = hrefImg.parentNode ;
    }
 
    if ( hrefWikidata != '' ) {
      whereto = hrefWikidata.parentNode ;
 
      // try to get URL from Wikidata in the user language
      var WikidataID = hrefWikidata.parentNode.nextSibling.innerHTML ;
 
      // while we are at it, change it to a link
      hrefWikidata.parentNode.nextSibling.innerHTML = '<a href="http://www.wikidata.org/wiki/' + WikidataID + '">' + WikidataID + '</a>';
 
      // by default, link to Wikidata. Replaced by a link to Wikipedia later if it exists
      WPUrl = 'http://www.wikidata.org/wiki/' + WikidataID;
      WPlogo = "http://www.omegawiki.org/images/b/be/Wikidata-logo.png";
      WPtext = WikidataID;
 
      var WPdiv = document.createElement('div');
      var WPimg = document.createElement('img');
      var theBR = document.createElement('br');
      var WPa = document.createElement('a');
      WPimg.setAttribute('style','text-align: center;');
      $(WPimg).width(logosize).height(logosize);
      WPimg.setAttribute('src', WPlogo);
      // give it an id to be found later when jSon gets its results
      WPdiv.setAttribute('id','Wikidatalink' + WikidataID);
 
      WPa.setAttribute('href', WPUrl );
      WPa.innerHTML = WPtext;
 
      WPdiv.appendChild( theBR );
      WPdiv.appendChild( WPimg );
      WPdiv.appendChild( theBR );
      WPdiv.appendChild( WPa );
      ICONdiv.appendChild ( WPdiv );
 
      $.getJSON("http://www.wikidata.org/w/api.php?action=wbgetentities&format=json&callback=?",
      {ids: WikidataID, props: "labels|sitelinks", languages: wgUserLanguage},
      (function(localID) {
        return function(data) {
 
        WikidataIDString = localID.toUpperCase();
          // nice but doesn't always work
          // WParticleTitle = data.entities[WikidataIDString].labels[wgUserLanguage].value;
        wikiname = wgUserLanguage + "wiki";
        if ( data.entities[WikidataIDString].sitelinks[wikiname] ) {
          WParticleTitle = data.entities[WikidataIDString].sitelinks[wikiname].title;
        } else {
          WParticleTitle = "";
        }
        if ( WParticleTitle != "" ) {
          // a Wikipedia link was found in the user language
          var link = "http://" + wgUserLanguage + ".wikipedia.org/wiki/" + WParticleTitle;
          $('#Wikidatalink' + localID).find('a').attr("href",link).html(WParticleTitle);
          $('#Wikidatalink' + localID).find('img').attr("src", "http://www.omegawiki.org/images/f/fb/Wikipedia-logo-200px.png");
        }
 
        }; // function(data)
      }(WikidataID)) // function(localID)
 
      ); // if getJSON returns data
    }
    if ( hrefCommons != '' ) {
      var COdiv = document.createElement('div');
      var COimg = document.createElement('img');
      var theBR = document.createElement('br');
      var COa = document.createElement('a');
 
      $(COimg).width(logosize).height(logosize);
      COdiv.setAttribute('style','text-align: center;');
      COimg.setAttribute('src','http://www.omegawiki.org/images/3/39/Commons-logo.png');
 
      COa.setAttribute('href', hrefCommons.parentNode.nextSibling.firstChild );
      // writes "link to Wikipedia <br /> article_name"
      COa.innerHTML = hrefCommons.innerHTML + '<br />' + hrefCommons.parentNode.nextSibling.innerHTML ;
 
      COdiv.appendChild( theBR ) ;
      COdiv.appendChild( COimg ) ;
      COdiv.appendChild( theBR ) ;
      COdiv.appendChild( COa ) ;
      ICONdiv.appendChild ( COdiv ) ;
 
      whereto = hrefCommons.parentNode ;
    }
    if ( hrefWikisource != '' ) {
      var COdiv = document.createElement('div');
      var COimg = document.createElement('img');
      var theBR = document.createElement('br');
      var COa = document.createElement('a');
 
      COimg.setAttribute('src','http://www.omegawiki.org/images/f/fb/Wikisource-logo.png');
      $(COimg).width(logosize).height(logosize);
      COdiv.setAttribute('style','text-align: center;');
 
      COa.setAttribute('href', hrefWikisource.parentNode.nextSibling.firstChild );
      COa.innerHTML = hrefWikisource.innerHTML + '<br />' + hrefWikisource.parentNode.nextSibling.innerHTML ;
 
      COdiv.appendChild( theBR ) ;
      COdiv.appendChild( COimg ) ;
      COdiv.appendChild( theBR ) ;
      COdiv.appendChild( COa ) ;
      ICONdiv.appendChild ( COdiv ) ;
 
      whereto = hrefWikisource.parentNode ;
    }
    if ( hrefWikiquote != '' ) {
      var COdiv = document.createElement('div');
      var COimg = document.createElement('img');
      var theBR = document.createElement('br');
      var COa = document.createElement('a');
 
      $(COimg).width(logosize).height(logosize);
      COdiv.setAttribute('style','text-align: center;');
      COimg.setAttribute('src','http://www.omegawiki.org/images/3/32/Wikiquote-logo.png');
 
      COa.setAttribute('href', hrefWikiquote.parentNode.nextSibling.firstChild );
      COa.innerHTML = hrefWikiquote.innerHTML + '<br />' + hrefWikiquote.parentNode.nextSibling.innerHTML ;
 
      COdiv.appendChild( theBR ) ;
      COdiv.appendChild( COimg ) ;
      COdiv.appendChild( theBR ) ;
      COdiv.appendChild( COa ) ;
      ICONdiv.appendChild ( COdiv ) ;
 
      whereto = hrefWikiquote.parentNode ;
    }
 
    if ( whereto != '' ) {
      while ( ( whereto.className != 'expand-syntAtt' )
        && ( whereto.className != 'expand-dmAtt' )
        && ( whereto.className != 'expand-syntrans' ) ) {
        whereto = whereto.parentNode ;
      }
      // found : Annotation (DM). Need to get "definition", i.e. the previous one.
      whereto = whereto.parentNode.parentNode.firstChild ;
 
      whereto.parentNode.insertBefore(ICONdiv, whereto );
    }
  } // for def  
} // function