User:Twp/tineye.js

From OpenWetWare
Jump to navigationJump to search

Note: After publishing, 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 / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// 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');
});