User:Tim Pierce/tineye.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.
// 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');

jQuery( function() { mw.loader.using( ['mediawiki.util'], 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); 
 }
 
 mw.util.addPortletLink('p-cactions', 'http://tineye.com/search?url=' + encodeURIComponent(imageurl), 'Tineye', 'ca-tineye');
})});