User:Twp/tineye.js

From OpenWetWare
Jump to: navigation, search
// adapted from TinEye gadget for Commons
// To use, add this to your user skin preferences file (probably User:YourUsername/monobook.js):
//
// importScript('User:Twp/tineye.js');

addOnloadHook( function() {
 if ((wgNamespaceNumber != 6) || (wgAction != "view") || !document.getElementById('file')) return;
 var links = document.getElementById('file').getElementsByTagName('a');
 if (!links.length) return;
 var imageurl = links[links.length-1].href; //imageboxes adds links _before_, so we get the last link
 
 if (document.getElementById('file').getElementsByTagName('img')[0].width <= 300) {
   imageurl = document.getElementById('file').getElementsByTagName('img')[0].src; //Image smaller than 300px width
 } else { //Get thumb url
   imageurl = imageurl.replace("/en/",      "/en/thumb/");
   imageurl = imageurl.replace("/commons/", "/commons/thumb/");
 
   n = imageurl.lastIndexOf('/') + 1;
   imageurl = imageurl + "/300px-" + imageurl.substring(n); 
 }
 
 addPortletLink('p-cactions', 'http://tineye.com/search?url=' + encodeURIComponent(imageurl), 'Tineye', 'ca-tineye');
});