User:Achim55/ImageSearch.js

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.
//adopted from [[MediaWiki:Gadget-GoogleImages.js]]
/*global mw, jQuery*/
/*jshint curly:false */
jQuery(document).ready(function() {
'use strict';
	if (mw.config.get('wgNamespaceNumber') !== 6 || mw.config.get('wgAction') !== "view" || !document.getElementById('file')) return;

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

	var imageurl = imgs[0].parentNode.href;
	if (!imageurl) return; /* This occurs with thumbs of videos for instance */

	// For the case of 'Error creating thumbnail: Invalid thumbnail parameters or PNG file with more than 12.5 million pixels'
	if (document.getElementById('file').getElementsByTagName('img').length <= 0) return; 

	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
		var n = imageurl.indexOf("/commons/");
		imageurl = imageurl.substring(0, n + ("/commons/").length) + "thumb/" + imageurl.substring(n + ("/commons/").length);

		n = imageurl.lastIndexOf('/') + 1;
		imageurl = imageurl + "/300px-" + imageurl.substring(n); 
	}
    mw.util.addPortletLink('p-cactions', 'https://lens.google.com/uploadbyurl?url=' + encodeURIComponent(imageurl), 'is:Google', 'ca-googleimages', null);
    mw.util.addPortletLink('p-cactions', 'https://yandex.com/images/search?rpt=imageview&url=' + encodeURIComponent(imageurl), 'is:Yandex', 'ca-yandeximages', null);
	mw.util.addPortletLink('p-cactions', 'https://www.bing.com/images/search?view=detailv2&iss=sbi&FORM=IRSBIQ&q=imgurl:' + encodeURIComponent(imageurl), 'is:Bing', 'ca-bingimages', null);
	mw.util.addPortletLink('p-cactions', 'https://tineye.com/search?sort=size&order=desc&url=' + encodeURIComponent(imageurl), 'is:TinEye', 'ca-tineye', null);
/* need to be fixed
	mw.util.addPortletLink('p-cactions', 'https://pic.sogou.com/ris?flag=1&drag=0&query=https%3A%2F%2Fimg01.sogoucdn.com%2Fv2%2Fthumb%2Fretype_exclude_gif%2Fext%2Fauto%3Fappid%3D122%26url%3D' + encodeURIComponent(imageurl), 'is:Sogou', 'ca-sogouimages', null);
	mw.util.addPortletLink('p-cactions', 'https://graph.baidu.com/details?isfromtusoupc=1&tn=pc&carousel=0&image=' + encodeURIComponent(imageurl), 'is:Baidu', 'ca-baiduimages', null);
*/	
});