Jump to content

User:Magnus Manske/webp.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.
$(document).ready ( function () {
  $('a.image img').each ( function () {
      var img = $(this) ;
      var src = img.attr('src') ;
      if ( null == src.match ( /\.jpe?g$/i ) ) return ;
      
      var width = img.attr('width');
      var filepage = img.parent('a').attr('href') ;
      var filename = filepage.split('/File:')[1] ;

      $.getJSON ( '/w/api.php?action=query&prop=imageinfo&format=json&titles=File:'+filename , function ( data ) {
        var repository ;
        $.each ( data.query.pages , function ( k , v ) {
            repository = v.imagerepository == 'local' ? 'en.wikipedia' : 'commons.wikipedia' ;
        } ) ;
        
        var nsrc = "//toolserver.org/~magnus/cgi-bin/webp.pl?q=50&sharpness=0&px="+width+"&file="+filename+"&wp="+repository  ;
        img.attr('origsrc',img.attr('src')) ;
        img.attr('webpsrc',nsrc) ;
        img.attr('src',nsrc) ;
        img.mouseover(function(){img.attr('src',img.attr('origsrc'));});
        img.mouseout(function(){img.attr('src',img.attr('webpsrc'));});
  
      } ) ;

} ) ;
} ) ;