Image Wizard

Does some magic with images.

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

  1. // ==UserScript==
  2. // @name Image Wizard
  3. // @namespace https://github.com/GrumpyCrouton/Userscripts
  4. // @version 1.5
  5. // @description Does some magic with images.
  6. // @author GrumpyCrouton
  7. // @match *://*.stackoverflow.com/*
  8. // @match *://*.stackexchange.com/*
  9. // @match *://*.superuser.com/*
  10. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
  11. // @require https://gf.qytechs.cn/scripts/48306-waitforkeyelements/code/waitForKeyElements.js?version=275769
  12. // @grant GM.xmlHttpRequest
  13. // @grant GM_addStyle
  14. // @grant GM.getValue
  15. // ==/UserScript==
  16. var proxy = "http://service.bypass123.com/index.php";
  17. var replaceFrom = ["imgur.com", "facebook.com"];
  18.  
  19. var processEvery = 0;
  20. //proces every x seconds (Useful until I can figure out how to detect when a new image or link is added to DOM. Set to 0 to disable.
  21. //There is an automated way built into the script, processEvery is deprecated since v1.5 but can still be used if the automated method is not working for you.
  22.  
  23. /* DEBUG */
  24. var debugImg = false;
  25. var debugA = false;
  26. var debug = false;
  27.  
  28. var supportedExtensions = ["png", "jpg", "gif"];
  29.  
  30. (function() {
  31.  
  32. process();
  33.  
  34. function handleImgTag() {
  35. $('img').each(function() {
  36. url = $(this).prop('src').split('?').shift();
  37. extension = url.split('.').pop();
  38. if (replaceFrom.some(function(v) {
  39. return url.indexOf(v) >= 0;
  40. }) && supportedExtensions.some(function(v) {
  41. return url.indexOf(v) >= 0;
  42. })) {
  43. if (!$(this).attr('processed')) {
  44. $(this).attr('processed', true);
  45. replaceImg(url, $(this));
  46. }
  47. return true;
  48. }
  49. if (debugImg) console.log("No URL match in <img>: " + url);
  50. });
  51. }
  52.  
  53. function handleATag() {
  54. $('a').each(function() {
  55. url = $(this).prop('href').split('?').shift();
  56. extension = url.split('.').pop();
  57. if (replaceFrom.some(function(v) {
  58. return url.indexOf(v) >= 0;
  59. }) && supportedExtensions.some(function(v) {
  60. return url.indexOf(v) >= 0;
  61. })) {
  62. if ($(this).has('img').length < 1) {
  63. replaceA(url, $(this));
  64. return true;
  65. }
  66. }
  67. if (debugA) console.log("No URL match in <a>: " + url);
  68. });
  69. }
  70.  
  71. function replaceImg(src, element) {
  72. GM.xmlHttpRequest({
  73. method: "POST",
  74. url: proxy,
  75. data: "url=" + src,
  76. headers: {
  77. "Content-Type": "application/x-www-form-urlencoded"
  78. },
  79. onload: function(response) {
  80. element.prop('src', response.finalUrl);
  81. element.prop('alt', "Image replaced by Image Wizard");
  82.  
  83. parent_link = element.parent("a");
  84. if (parent_link.length) {
  85. if (parent_link.prop('href') == src) {
  86. parent_link.prop('href', response.finalUrl);
  87. parent_link.prop('target', "_blank");
  88. }
  89. } else {
  90. element.wrap("<a href='" + response.finalUrl + "' target='_blank'></a>");
  91. }
  92. element = element.parent();
  93.  
  94. if (element.parents('.post-text').length) {
  95. wrapContentElement(element);
  96. } else {
  97. wrapDefaultElement(element);
  98. }
  99. }
  100. });
  101. }
  102.  
  103. function replaceA(src, element, addImg = true) {
  104. GM.xmlHttpRequest({
  105. method: "POST",
  106. url: proxy,
  107. data: "url=" + src,
  108. headers: {
  109. "Content-Type": "application/x-www-form-urlencoded"
  110. },
  111. onload: function(response) {
  112. if (element.parents('.post-text').length) {
  113. element.text("");
  114. element.append("<img src='" + response.finalUrl + "'></img>");
  115. wrapContentElement(element);
  116. } else {
  117. element.text("[Image Wizard");
  118. element.wrap("<span></span>").parent('span').append("<a href='https://github.com/GrumpyCrouton/Userscripts/blob/master/Image%20Wizard'>]</a>");
  119. }
  120. element.prop('href', response.finalUrl).prop('target', "_blank");
  121. }
  122. });
  123. }
  124.  
  125. function wrapContentElement(element) {
  126. element.wrap("<div class='module community-bulletin image-proxier'></div>");
  127. element = element.parent(".image-proxier");
  128. element.prepend("\
  129. <div class='bulletin-title'>\
  130. <center>\
  131. <a \
  132. style='text-decoration:none;' \
  133. href='https://github.com/GrumpyCrouton/Userscripts/blob/master/Image%20Wizard'>\
  134. Image Wizard\
  135. </a>\
  136. </center>\
  137. </div>\
  138. <hr>");
  139. }
  140.  
  141. function wrapDefaultElement(element) {
  142. element.wrap("<span></span>");
  143. }
  144.  
  145. function process() {
  146. if(debug) console.log("Processing page.");
  147. handleImgTag();
  148. handleATag();
  149. }
  150.  
  151. $('img').on('load', function() {
  152. handleImgTag();
  153. });
  154.  
  155. $('a').on('load', function() {
  156. handleATag();
  157. });
  158.  
  159. if(processEvery) {
  160. setInterval (process, processEvery * 1000);
  161. }
  162.  
  163. waitForKeyElements ("img", loadHandling);
  164.  
  165. function loadHandling (jNode) {
  166. if (jNode[0].complete) {
  167. handlePostLoadedImg (jNode);
  168. }
  169. else {
  170. jNode.on ('load', handlePostLoadedImg);
  171. }
  172. }
  173. function handlePostLoadedImg (jNodeOrEvent) {
  174. var newImg; // will be jQuery node
  175. if (jNodeOrEvent instanceof jQuery) {
  176. newImg = jNodeOrEvent;
  177. }
  178. else {
  179. newImg = $(jNodeOrEvent.target);
  180. }
  181. handleImgTag();
  182. }
  183.  
  184. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址