MediaWiki:Gadget-GoogleImagesTineye.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*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://lens.google.com/uploadbyurl?url=' + encodeURIComponent(imgURL), 'Google Lens', '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';


});