MediaWiki:Gadget-GoogleImagesTineyeMobile.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.
// Google images and TinEye search gadget for mobile code starts
$.when(mw.loader.using(['oojs-ui-core','oojs-ui.styles.icons-interactions'])).then( function () { 
//Only load in file pages in view action that have a file (not file redirects)
if (mw.config.get('wgNamespaceNumber') == 6 && mw.config.get('wgAction') === "view" && document.getElementById('file') && mw.config.get('skin') === 'minerva') {
//Some return conditions
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";
}
//Button construction start
var	gimage = 'https://www.google.com/searchbyimage?image_url=' + encodeURIComponent(imgURL) ,
	tineye = 'https://tineye.com/search?sort=size&order=desc&url=' + encodeURIComponent(imgURL) ,
	gimageBtn = new OO.ui.ButtonWidget({
		label: 'Google images',
		href: gimage ,
		icon: 'search',
		target: '_blank',
		title: 'Find copies and similar images using Google images search'
}),
	tineyeBtn = new OO.ui.ButtonWidget({
		label: 'TinEye',
		href: tineye ,
		icon: 'search',
		target: '_blank',
		title: 'Find copies and similar images using TinEye search'
});
//Append the button to siteNotice div id. Change link order here.
$("#siteNotice").append(gimageBtn.$element, tineyeBtn.$element);
}
});