Google Images direct links

Add direct links to the picture and the page link to the Google Image Search results.

当前为 2018-03-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. ///////////////// In case it fails to update in TamperMonkey, visit https://github.com/svArtist/Google-Images-Direct-Links/raw/master/googleImagesDirectLinks.user.js directly ////////
  3. // @name Google Images direct links
  4. // @version 1.4
  5. // @description Add direct links to the picture and the page link to the Google Image Search results.
  6. // @namespace Google
  7. // @author Benjamin Philipp <benjamin_philipp [at - please don't spam] gmx.de>
  8. // @description Add direct links to the Google Images search results. LeftClick to open image, CTRL+LeftClick to open source page
  9. // @include /^https?:\/\/(www\.)*google\.[a-z\.]{2,5}\/search.*tbm=isch.*/
  10. // @require http://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
  11. // @run-at document-body
  12. // @grant GM_xmlhttpRequest
  13. // @connect *
  14. // ==/UserScript==
  15.  
  16. var maxtries = 10;
  17.  
  18. var idle = true;
  19. var disableUpdate = false;
  20.  
  21. function updatePage()
  22. {
  23. if($("#directLinkStyles").length<=0){
  24. disableUpdate = true;
  25. $("head").append("\
  26. <style id='directLinkStyles'>\
  27. .linkToTarget{\
  28. box-shadow: 3px 5px 10px rgba(0,0,0,0.5); \
  29. cursor: default;\
  30. position: absolute; \
  31. right:0; top:0; \
  32. opacity: 0; \
  33. background-color: rgba(255,255,255,0.5);\
  34. transition: background-color 0.5s, opacity 0.5s \
  35. }\
  36. a:hover .linkToTarget{\
  37. opacity: 0.6; \
  38. }\
  39. .linksdone:hover .linkToTarget, .linkToTarget:hover{\
  40. opacity: 1; \
  41. background-color: rgba(255,255,255,1);\
  42. }\
  43. .linksdone:hover .linkToTarget{\
  44. cursor: pointer;\
  45. }\
  46. .linkToTargetLink, .linkToTarget>span{\
  47. color: rgba(200,200,200, 0.7)!important; \
  48. padding: 3px 10px; \
  49. font-size: 28pt; \
  50. display: block; \
  51. font-weight: bold; \
  52. text-decoration: none;\
  53. transition: color 0.5s, font-size 0.5s, padding 0.5s; \
  54. }\
  55. .linkToTargetLink:hover{\
  56. color: rgba(155,177,233, 1)!important; \
  57. padding:8px 20px; \
  58. font-size: 36pt; \
  59. } \
  60. </style>");
  61. disableUpdate = false;
  62. }
  63.  
  64. $(".rg_di.rg_bx a.rg_l:not(.linksdone):not([href='#'])").each(function(){
  65. var tp = this;
  66. var tppar = $(this).parent();
  67. var imin = tp.href.indexOf("imgurl=");
  68. if(imin<0)
  69. {
  70. $(tp).attr("resTries", $(tp).attr("resTries")?$(tp).attr("resTries")*1+1:1);
  71. if($(tp).attr("resTries")*1>=maxtries){
  72. console.log("This Link won't come up with a good fragment: " + $(tp).find("img")[0].src);
  73. return true;
  74. }
  75. updater();
  76. return true;
  77. }
  78. var linkconts = tp.href.substr(imin+7);
  79. var piclink = linkconts.substr(0,linkconts.indexOf("&"));
  80. var reflink = linkconts.substr(linkconts.indexOf("imgrefurl=")+10);
  81. reflink = decodeURIComponent(reflink.substr(0, reflink.indexOf("&")));
  82. piclink = decodeURIComponent(piclink);
  83. disableUpdate = true;
  84. var dirlink = $("<a class='linkToTargetLink' href='" + piclink + "'>+</a>");
  85. $(this).removeClass("linkswait");
  86. var templink = $(tppar).find(".linkToTarget.temp")[0];
  87. $(templink).removeClass("temp");
  88. $(templink).html(dirlink);
  89. $(this).addClass("linksdone");
  90. var urilink = $(tppar).find(".rg_ilmbg")[0];
  91. $(urilink).html("<a style='display: block; color: #fff; text-decoration: none;' href='" + reflink + "'>" + urilink.innerHTML + "</a>");
  92. $(dirlink).add(urilink).click(function(e){
  93. e.stopImmediatePropagation();
  94. });
  95. disableUpdate = false;
  96. });
  97. var notready = false;
  98. $(".rg_di.rg_bx a.rg_l[href='#']:not(.linksdone)").each(function(){
  99. notready = true;
  100. if(!$(this).hasClass("linkswait")){
  101. $(this).addClass("linkswait");
  102. $(this).append("<div class='linkToTarget temp'><span>...</span></div>");
  103. }
  104. });
  105. if(notready){
  106. updater();
  107. }
  108. }
  109.  
  110. function updater(t = 1000){
  111. if(idle)
  112. {
  113. idle = false;
  114. updaterequest = false;
  115. updatePage();
  116. idletimer = setTimeout(function(){
  117. idle = true;
  118. if(updaterequest)
  119. updatePage();
  120. }, t);
  121. }
  122. else
  123. {
  124. updaterequest = true;
  125. }
  126. }
  127.  
  128. var bodyObserver = false;
  129. function observeResults(){
  130. // console.log("observing");
  131. resultsObserver = new MutationObserver(updater);
  132. resultsObserver.observe($("#ires #rg")[0], {subtree: true, childList: true});
  133. if(bodyObserver !== false)
  134. bodyObserver.disconnect();
  135. }
  136.  
  137.  
  138. if($("#ires #rg").length>0){
  139. observeResults();
  140. }
  141. else{
  142. bodyObserver = new MutationObserver(function(mutations){
  143. if(disableUpdate || !idle){
  144. return;
  145. }
  146. if($("#ires #rg").length>0)
  147. {
  148. observeResults();
  149. }
  150. });
  151. bodyObserver.observe($("body")[0], {subtree: true, childList: true});
  152. }
  153. updatePage();