Google Images direct links 2

Add direct links to the picture to the Google Image Search results.

目前為 2021-01-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
///////////////// In case it fails to update in TamperMonkey, visit  https://www.benjamin-philipp.com/fff/userScripts/Google_Images_Direct_Links_2.user.js  directly ////////
// @name		Google Images direct links 2
// @version		2.1
// @description Add direct links to the picture to the Google Image Search results.
// @namespace	Google
// @author		Benjamin Philipp <dev [at - please don't spam] benjamin-philipp.com>
// @include		/^https?:\/\/(www\.)*google\.[a-z\.]{2,5}\/search.*tbm=isch.*/
// @require 	http://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
// @run-at		document-start
// @grant		GM_xmlhttpRequest
// @connect		*
// ==/UserScript==

// Ugly adaptation for the way Google handles image URLs right now :(
// The real URLs are only loaded once you click on an image. I haven't been able to stop the details view to open upon click (and I'd rather not force it to stay hidden completely)
// If you can decouple the "getting link" part from the "opening details view" part, let me know!!! (I can't see through the minimized entangled mess of the actual google functions)


var updateInterval = 1000;
var maxtries = 100;

var idle = true;

function updatePage()
{
	if($("#directLinkStyles").length<=0){
		$("head").append("\
		<style id='directLinkStyles'>\
		.linkToTarget{\
			box-shadow: 3px 5px 10px rgba(0,0,0,0.5); \
			cursor: default;\
			position: absolute; \
			right:0; top:0; \
			opacity: 0; \
			background-color: rgba(255,255,255,0.5);\
			transition: background-color 0.5s, opacity 0.5s \
		}\
		.failed .linkToTargetlink{\
			color: rgba(230,100,100)!important; \
		}\
		a:hover .linkToTarget{\
			opacity: 0.6; \
		}\
		a:hover .linkToTarget:hover{\
			opacity: 1; \
		} \
		.linksdone:hover .linkToTarget{\
			cursor: pointer;\
		}\
		.linkToTargetLink{\
			color: rgba(155,177,233, 1)!important; \
			font-size: 22pt; \
			display: block; \
			font-weight: bold; \
			text-decoration: none!important;\
			transition: color 0.5s, font-size 0.5s, padding 0.5s; \
		}\
		.temp .linkToTargetLink{\
			color: rgba(200,200,200)!important; \
		}\
		.linkToTargetLink:hover{\
			color: rgba(155,177,233, 1)!important; \
			padding:8px; \
			font-size: 30pt; \
		} \
		html body#yDmH0d div#islmp div#islrg div.islrc div.isv-r a.islib:hover{\
			overflow: visible;\
			z-index: 100;\
		}\
		</style>");
	}

	$(".rg_di.rg_bx a.rg_l:not(.linksdone), #islrg div.isv-r a.wXeWr.islib:not(.linksdone)").each(function(){
		var tp = this;
		$(tp).append("<div class='linkToTarget'><a class='linkToTargetLink'>↗️</a></div>");
		$(tp).find("a.linkToTargetLink").click(clickLink);
		$(tp).addClass("linksdone");
	});
}

function clickLink(e){
	e.stopPropagation();
	e.preventDefault();
	var t = e.target;
	
	waitForLink(t, e);
	return false;
}

function waitForLink(t, e){
	var tp = $(t).parent().parent()[0];
	var imin = tp.href.indexOf("imgurl=");
	var openInNew = e.ctrlKey || e.which==2;
	if(imin<0)
	{
		var $e = tp;
		var restries = $(tp).attr("resTries")?$(tp).attr("resTries")*1+1:1;
		if(restries==1){
			$($e).click();
			$(tp).find("img").click();
			setTimeout(function(){
				$($e).click();
			}, 200);
			
//			$(tp).find("img").contextmenu();
//			$(tp).trigger({
//				type: 'mousedown',
//				which: 2
//			});
//			waitfor("#islsp a[aria-label='Close']", function(o){
//				$(o).click(); // somehow doesn't close the details view either
//			});
		}
		
//		console.log("try", restries);
		$(tp).attr("resTries", restries);

		if($(tp).attr("resTries")*1>=maxtries){
			console.log("This Link won't come up with a good fragment: " + $(tp).find("img")[0].src);
			$(tp).addClass("linksdone");
			$(tp).addClass("failed");
			$(tp).find(".linkToTarget span").html("x");
			return true;
		}
		
		if(!$(tp).hasClass("linkswait")){
			$(tp).addClass("linkswait");
			$(tp).find(".linkToTarget").addClass("temp");
			$(tp).find(".linkToTarget a").html("...");
		}
//			console.log("Not ready");
		setTimeout(function(){
			console.log("try again");
			waitForLink(t, e);
		}, 200);
		
		return true;
	}
	else{
		console.log("got link");
		var linkconts = tp.href.substr(imin+7);
		var piclink = linkconts.substr(0,linkconts.indexOf("&"));
		var reflink = linkconts.substr(linkconts.indexOf("imgrefurl=")+10);
		reflink = decodeURIComponent(reflink.substr(0, reflink.indexOf("&")));
		piclink = decodeURIComponent(piclink);
		$(tp).removeClass("linkswait");
		$(tp).find(".linkToTarget").removeClass("temp");
		$(tp).find(".linkToTarget").html("<a class='linkToTargetLink' href='" + piclink + "'>↗️</a>");
		$(tp).removeClass("linkswait");
		$(tp).addClass("linksdone");
		if(e.which == 3)
			return false; // Don't open new tab on right click
		
		// decided to have it always open in a new tab for now, because middle click wasn't playing nice in my Firefox
		if(openInNew){
			window.open(piclink);
		}
		else{
			location.href = piclink;
		}
	}
}

function waitfor(sel, cb, cbfail, delay, maxtries){
	if(delay===undefined)
		delay = 500;
	if(maxtries===undefined)
		maxtries = 50;
	
	if($(sel).length<=0){
//		console.log("Not loaded yet: " + sel);
		if(cbfail!==undefined){
			if (cbfail() !== false){
				if(maxtries>1)
					setTimeout(function(){
						waitfor(sel, cb, cbfail, delay, maxtries-1);
					}, delay);
			}
			else{
//				console.log("cbfail returned false, no retry");
			}
		}else{
			if(maxtries>1)
				setTimeout(function(){
					waitfor(sel, cb, cbfail, delay, maxtries-1);
				}, delay);
		}
		return;
	}
	cb($(sel));
}

setInterval(updatePage, updateInterval);

updatePage();