MediaWiki:Gadget-GoogleImagesTineye.js

/*global mw, $*/ /*jshint curly:false */

$(function { 'use strict'; if (mw.config.get('wgNamespaceNumber') !== 6 || mw.config.get('wgAction') !== "view" || !document.getElementById('file'))	return;

var img = document.getElementById('file').getElementsByTagName('img'); if (!img || !img.length) return; // No preview image, e.g. for large PNGs

img = img[0]; var imgURL = img.parentNode.href; if (!imgURL) return; // This occurs with thumbs of videos/sounds for instance

if (img.width <= 300) imgURL = img.src; // Image smaller than 300px width else { // Get thumb url var n = imgURL.indexOf("/commons/") + 9; imgURL = imgURL.substr(0, n) + "thumb/" + imgURL.substr(n); n = imgURL.lastIndexOf('/') + 1; imgURL += "/300px-" + imgURL.substring(n); if (!imgURL.slice(-3).indexOf("svg")) imgURL += ".png"; }

var portletLink = mw.util.addPortletLink('p-cactions', 'https://www.google.com/searchbyimage?image_url=' + encodeURIComponent(imgURL), 'Google Images', 'ca-googleimages', null); if(portletLink && portletLink.children[0]) portletLink.children[0].target = '_blank';

portletLink = mw.util.addPortletLink('p-cactions', 'https://tineye.com/search?url=' + encodeURIComponent(imgURL) + '&sort=size&order=desc', 'TinEye', 'ca-tineye', null); if(portletLink && portletLink.children[0]) portletLink.children[0].target = '_blank';

});