User:Magnus Manske/webp.js

$(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'));});      } ) ;

} ) ; } ) ;