Google Image Search Direct Links Old Style

Replace links and click actions of the Google Image Search results with direct links to the picture, and the page link. Now opens in the same tab by default

当前为 2021-02-21 提交的版本,查看 最新版本

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