MediaWiki:Gadget-GoogleImagesTineyeMobile.js

From wikishia

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.
// 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);
}
});