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

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

  1. // ==UserScript==
  2. // @name Google Image Search Direct Links Old Style
  3. // @version 1.0
  4. // @description Replace links and click actions of the Google Image Search results with direct links to the picture, and the page link
  5. // @namespace bp
  6. // @author Benjamin Philipp <benjamin_philipp [at - please don't spam] gmx.de>
  7. // @include /^https?:\/\/(www\.)*google\.[a-z\.]{2,5}\/search.*tbm=isch.*/
  8. // @require http://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
  9. // @run-at document-body
  10. // @grant GM_xmlhttpRequest
  11. // @connect *
  12. // ==/UserScript==
  13.  
  14. var maxtries = 10;
  15.  
  16. var idle = true;
  17. var disableUpdate = false;
  18.  
  19. function updatePage()
  20. {
  21. if($("#directLinkStyles").length<=0){
  22. disableUpdate = true;
  23. $("head").append("\
  24. <style id='directLinkStyles'>\
  25. .linkswait{ \
  26. box-shadow: 0 0 20px #f00; \
  27. border: 2px solid #f00; \
  28. border-radius: 5px; \
  29. </style>");
  30. disableUpdate = false;
  31. }
  32.  
  33. $(".rg_di.rg_bx a.rg_l:not(.linksdone):not([href='#'])").each(function(){
  34. var tp = this;
  35. var uriLink = $(this).parent().find("div.rg_ilmbg")[0];
  36. var imin = tp.href.indexOf("imgurl=");
  37. if(imin<0)
  38. {
  39. $(tp).attr("resTries", $(tp).attr("resTries")?$(tp).attr("resTries")*1+1:1);
  40. if($(tp).attr("resTries")*1>=maxtries){
  41. console.log("This Link won't come up with a good fragment: " + $(tp).find("img")[0].src);
  42. return true;
  43. }
  44. updater();
  45. return true;
  46. }
  47. var linkconts = tp.href.substr(imin+7);
  48. var piclink = linkconts.substr(0,linkconts.indexOf("&"));
  49. var reflink = linkconts.substr(linkconts.indexOf("imgrefurl=")+10);
  50. reflink = decodeURIComponent(reflink.substr(0, reflink.indexOf("&")));
  51. piclink = decodeURIComponent(piclink);
  52. disableUpdate = true;
  53. tp.href = piclink;
  54. $(tp).click(function(){
  55. window.open(this.href);
  56. return false;
  57. })
  58. $(uriLink).click(function(e){
  59. e.preventDefault();
  60. e.stopImmediatePropagation();
  61. window.open(reflink);
  62. return false;
  63. })
  64. $(this).removeClass("linkswait");
  65. $(this).addClass("linksdone");
  66. disableUpdate = false;
  67. });
  68. var notready = false;
  69. $(".rg_di.rg_bx a.rg_l[href='#']:not(.linksdone)").each(function(){
  70. notready = true;
  71. if(!$(this).hasClass("linkswait")){
  72. $(this).addClass("linkswait");
  73. }
  74. });
  75. if(notready){
  76. updater();
  77. }
  78. }
  79.  
  80. function updater(t = 1000){
  81. if(idle)
  82. {
  83. idle = false;
  84. updaterequest = false;
  85. updatePage();
  86. idletimer = setTimeout(function(){
  87. idle = true;
  88. if(updaterequest)
  89. updatePage();
  90. }, t);
  91. }
  92. else
  93. {
  94. updaterequest = true;
  95. }
  96. }
  97.  
  98. var bodyObserver = false;
  99. function observeResults(){
  100. // console.log("observing");
  101. resultsObserver = new MutationObserver(updater);
  102. resultsObserver.observe($("#ires #rg")[0], {subtree: true, childList: true});
  103. if(bodyObserver !== false)
  104. bodyObserver.disconnect();
  105. }
  106.  
  107.  
  108. if($("#ires #rg").length>0){
  109. observeResults();
  110. }
  111. else{
  112. bodyObserver = new MutationObserver(function(mutations){
  113. if(disableUpdate || !idle){
  114. return;
  115. }
  116. if($("#ires #rg").length>0)
  117. {
  118. observeResults();
  119. }
  120. });
  121. bodyObserver.observe($("body")[0], {subtree: true, childList: true});
  122. }
  123.  
  124. updatePage();