Image Proxier

Replaces images from stack.imgur.com and collates them

  1. // ==UserScript==
  2. // @name Image Proxier
  3. // @namespace https://github.com/GrumpyCrouton/Userscripts
  4. // @version 2.0
  5. // @description Replaces images from stack.imgur.com and collates them
  6. // @author GrumpyCrouton, Yilmaz Durmaz
  7. // @match *://*.stackoverflow.com/*
  8. // @match *://*.stackexchange.com/*
  9. // @match *://*.superuser.com/*
  10. // @grant GM.xmlHttpRequest
  11. // ==/UserScript==
  12. // OPTIONS
  13. var convertImages = true;
  14. // var convertLinks = true;
  15. var convertLinks = true;
  16.  
  17. var frequencyInSeconds = 5;
  18.  
  19. var addProxy = true;
  20.  
  21. var proxy = "http://service.hidebux.com/index.php";
  22. var matchURLs = ["https://i.stack.imgur.com/", "https://i.imgur.com/", "http://i.imgur.com/"];
  23.  
  24.  
  25. // DO NOT ALTER BELOW THIS LINE
  26.  
  27. var debug = false;
  28.  
  29. var modal = document.createElement("div");
  30. modal.id = "image-proxier-modal";
  31. modal.style.position = "fixed";
  32. modal.style.border = "5px outset #52575b";
  33. modal.style.zIndex = 999;
  34. modal.style.padding = "5px";
  35. modal.style.background = "white";
  36. modal.style.display = "none";
  37. modal.setAttribute("onmouseover", "modalOver1()");
  38. modal.setAttribute("onmouseleave", "modalOver0()");
  39.  
  40. modal.innerHTML = " \
  41. <div style='width:100%;text-align:right;'> \
  42. <strong><div id='image-proxier-close' style='cursor:pointer;'>X</div></strong> \
  43. </div> \
  44. <div style='width:100%;text-align:center;overflow:auto;'> \
  45. <img id='image-proxier-magnif'></img> \
  46. </div> \
  47. ";
  48.  
  49. document.getElementsByTagName("BODY")[0].appendChild(modal);
  50.  
  51. if (convertImages) {
  52. replaceImages();
  53. }
  54.  
  55. function goProxy(source, attr, val) {
  56. GM.xmlHttpRequest({
  57. synchronous: "True",
  58. method: "POST",
  59. url: proxy,
  60. data: "url=" + val,
  61. headers: {
  62. "Content-Type": "application/x-www-form-urlencoded"
  63. },
  64. onload: function(response) {
  65. if (debug) {
  66. console.log([
  67. response.status,
  68. response.statusText,
  69. response.readyState,
  70. response.responseHeaders,
  71. response.responseText,
  72. response.finalUrl,
  73. response.XML
  74. ].join("\n"));
  75. }
  76. source.setAttribute(attr, response.finalUrl);
  77. source.setAttribute("onmouseover", "showOver('" + attr + "',this)");
  78. source.setAttribute("onmouseleave", "hideOver()");
  79. source.classList = "comeover";
  80. //console.log(source.src);
  81. }
  82. });
  83. }
  84.  
  85. function replaceImages() {
  86. var allImages = document.getElementsByTagName('img');
  87. for (var i = 0; i < allImages.length; i++) {
  88. var sImage = allImages[i];
  89. href = sImage.getAttribute('src');
  90. $(sImage).wrap('<a href="' + href + '" class="link"></a>');
  91. var result = containsAny(sImage.src, matchURLs);
  92. //console.log(result);
  93. if (result) {
  94. if (addProxy) {
  95. // sImage.src = proxy + sImage.src;
  96. goProxy(sImage, "src", sImage.src);
  97. //console.log(sImage.src);
  98. }
  99. }
  100. }
  101. }
  102.  
  103. if (convertLinks) {
  104. // replaceLinks();
  105. replaceLinksSmall();
  106. }
  107.  
  108. function replaceLinksSmall() {
  109. var allLinks = document.getElementsByTagName('a');
  110. for (var i = 0; i < allLinks.length; i++) {
  111. var sLink = allLinks[i];
  112. var result = containsAny(sLink.href, matchURLs);
  113. if (result) {
  114. if (addProxy) {
  115. goProxy(sLink, "href", sLink.href);
  116. }
  117. }
  118. }
  119. }
  120.  
  121. function replaceLinks() {
  122. var links = document.getElementsByTagName('a');
  123. for (var i = 0; i < links.length; i++) {
  124. var matches = containsAny(links[i].href, matchURLs);
  125. if (matches) {
  126. //link variables
  127. var link = links[i];
  128. var linkParent = links[i].parentNode;
  129.  
  130. //create <img> element
  131. var image = document.createElement("img");
  132. if (addProxy) {
  133. goProxy(image, "src", link.href);
  134. } else {
  135. image.src = link.href;
  136. }
  137. image.style.height = "auto";
  138. image.style.width = "75px";
  139. image.style.border = "2px outset #52575b";
  140.  
  141. //alter <a> element
  142. if (addProxy) {
  143. var temp = {
  144. src: ""
  145. };
  146. goProxy(temp, "src", link.href);
  147. //console.log(temp.src);
  148. link.setAttribute("link", temp.src);
  149. }
  150. link.href = "";
  151. link.classList.add("image-proxier-image");
  152. link.innerHTML = "";
  153. link.style.borderBottom = "none";
  154. link.style.paddingRight = "3px";
  155.  
  156. //attach <img> to <a>
  157. link.append(image);
  158.  
  159. result = linkParent.querySelector('.imageContainer');
  160. if (!result) {
  161. //create imageContainer (div) element
  162. var container = document.createElement("div");
  163. container.classList.add("imageContainer");
  164. container.style.backgroundColor = "#d4d4d4";
  165.  
  166. container.style.padding = "3px";
  167. linkParent.prepend(container);
  168. container.prepend(scriptName());
  169. }
  170. //append image to container that exists from last check
  171. linkParent.querySelector(".imageContainer").appendChild(link);
  172. }
  173. }
  174. }
  175.  
  176. $("#image-proxier-close").click(function() {
  177. $("#image-proxier-modal").hide();
  178. return false;
  179. });
  180.  
  181. $(".image-proxier-image").click(function() {
  182. var proxierImage = document.querySelector("#image-proxier-magnif");
  183. proxierImage.src = $(this).attr('link');
  184. proxierImage.style.width = "800px";
  185. proxierImage.style.height = "auto";
  186. proxierImage.style.maxHeight = "550px";
  187. $("#image-proxier-modal").show();
  188. return false;
  189. });
  190.  
  191.  
  192. var showover = document.createElement("script");
  193. showover.innerHTML = '\
  194. function showOver(name,e){\
  195. var posX=window.event.clientX+50;\
  196. var posY=document.documentElement.scrollTop;\
  197. var hY=window.innerHeight;\
  198. var modal = document.querySelector("#image-proxier-modal");\
  199. modal.style.width = "auto";\
  200. modal.style.height = (hY-50)+"px";\
  201. modal.style.maxheight = "1000px";\
  202. modal.style.left = posX+"px";\
  203. modal.style.top = 25+"px";\
  204. var proxierImage = document.querySelector("#image-proxier-magnif");\
  205. proxierImage.src = $(e).attr(name);\
  206. proxierImage.style.width = "auto";\
  207. proxierImage.style.height = "auto";\
  208. proxierImage.style.textAlign = "center";\
  209. proxierImage.style.maxHeight = (hY-100)+"px";\
  210. $("#image-proxier-modal").show();}\
  211. function hideOver(){\
  212. $("#image-proxier-modal").hide();}\
  213. function modalOver1(){ }\
  214. function modalOver0(){\
  215. $("#image-proxier-modal").hide();}\
  216. ';
  217. //'function showOver(name,e){console.log(name+": "+e.getAttribute(name)+":"+"mouse over");}';
  218. document.getElementsByTagName("BODY")[0].appendChild(showover);
  219.  
  220. $(document).mouseup(function(e) {
  221. var container = $("#image-proxier-modal");
  222.  
  223. // if the target of the click isn't the container nor a descendant of the container
  224. if (!container.is(e.target) && container.has(e.target).length === 0) {
  225. container.hide();
  226. }
  227. });
  228.  
  229. function scriptName() {
  230. var node = document.createElement("div");
  231. node.style.fontSize = "10px";
  232. node.style.width = "100%";
  233. node.innerHTML = "<center><a href='https://github.com/GrumpyCrouton/Userscripts' target='_blank'>Image Proxier</a></center>";
  234. return node;
  235. }
  236.  
  237. function containsAny(str, substrings) {
  238. for (var i = 0; i < substrings.length; i++) {
  239. if (str.startsWith(substrings[i])) {
  240. return true;
  241. }
  242. }
  243. return false;
  244. }