Imgur Mirror (Modified)

Switches all imgur links to 0imgur

当前为 2018-01-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Imgur Mirror (Modified)
  3. // @version 2.3
  4. // @description Switches all imgur links to 0imgur
  5. // @author Ad3m
  6. // @include *://*/*
  7. // @exclude *://imgur.com/*
  8. // @exclude *://*.imgur.com/*
  9. // @grant GM_Config
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_addStyle
  13. // @grant GM_registerMenuCommand
  14. // @namespace https://greasyfork.org/users/159159
  15. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  16. // @require https://greasyfork.org/scripts/37236-monkeyconfig/code/MonkeyConfig.js
  17. // ==/UserScript==
  18. var cfg = new MonkeyConfig({
  19. title: 'Imgur Mirror Configuration',
  20. menuCommand: true,
  21. params: {
  22. reddit_redirect: {
  23. type: 'checkbox',
  24. default: false
  25. },
  26. imgur_redirect: {
  27. type: 'checkbox',
  28. default: true
  29. },
  30. reddit_link_hijack_remover: {
  31. type: 'checkbox',
  32. default: true
  33. },
  34. open_links_in_new_tab: {
  35. type: 'checkbox',
  36. default: true
  37. },
  38. paticik_youtube_fullscreen_fix: {
  39. type: 'checkbox',
  40. default: true
  41. }
  42.  
  43. }
  44. });
  45.  
  46. var red_cfg = cfg.get('reddit_redirect');
  47. var img_cfg = cfg.get('imgur_redirect');
  48. var red_link_cfg = cfg.get('reddit_link_hijack_remover');
  49. var new_tab_cfg = cfg.get('open_links_in_new_tab');
  50. var pati_ytb_fix = cfg.get('paticik_youtube_fullscreen_fix');
  51.  
  52. changeLinks();
  53. document.addEventListener('scroll', changeLinks);
  54. document.addEventListener('click', changeLinks);
  55. function changeLinks() {
  56. if (img_cfg != false) {
  57. if (window.location.hostname == "i.0imgur.com") {
  58. $('style').remove();
  59. $('.icerik').remove();
  60. $('.yatay').remove();
  61. $('.dikey').remove();
  62. jQuery('body')
  63. .prepend("<style> .img { cursor:zoom-in; position: fixed; top: 0; bottom: 0; left: 0; right: 0; max-width: 100%; max-height: 100%; margin: auto; overflow: auto; } .img-exp { cursor:zoom-out; position: fixed; top: 0; bottom: 0; left: 0; right: 0; max-width: none; margin: auto; }</style>");
  64. $(".img").click(function () {
  65. $(this).removeClass("img");
  66. $(this).addClass("img-exp");
  67. });
  68. $(".img-exp").click(function () {
  69. $(this).removeClass("img-exp");
  70. $(this).addClass("img");
  71. });
  72. }
  73. var imgur_old_href = "imgur.com";
  74. var imgur_new_href = "0imgur.com";
  75. var nodeList = document.querySelectorAll('[href*="imgur.com"]');
  76. for (var i = 0; i < nodeList.length; i++)
  77. {
  78. if (nodeList[i].href.indexOf(imgur_new_href)==-1) {
  79. nodeList[i].href = nodeList[i].href.replace(imgur_old_href,imgur_new_href);
  80. if (new_tab_cfg != false) { nodeList[i].setAttribute('target', '_blank'); }
  81. }
  82. }
  83.  
  84. var imgur_old_src = "imgur.com";
  85. var imgur_new_src = "0imgur.com";
  86. var nodeList_src = document.querySelectorAll('[src*="imgur.com"]');
  87. for (var i = 0; i < nodeList_src.length; i++)
  88. {
  89. if (nodeList_src[i].src.indexOf(imgur_new_src)==-1) nodeList_src[i].src = nodeList_src[i].src.replace(imgur_old_src,imgur_new_src);
  90. }
  91. }
  92. if (red_cfg != false) {
  93. var list = document.querySelectorAll('[href*="redd.it"]');
  94. for (var i = 0; i < list.length; i++) {
  95. var node = list.item(i);
  96. var proxy_node = "https://proxy.duckduckgo.com/iu/?u="
  97. if (node.href.indexOf(proxy_node)==-1) {
  98. node.setAttribute('href', node.getAttribute('href')
  99. .replace(node.getAttribute('href'), proxy_node + node.getAttribute('href') + '&f=1'));
  100. if (new_tab_cfg != false) { node.setAttribute('target', '_blank'); }
  101. }
  102. }
  103. list = document.querySelectorAll('[src*="redd.it"]');
  104. for (i = 0; i < list.length; i++) {
  105. var node = list.item(i);
  106. var proxy_node = "https://proxy.duckduckgo.com/iu/?u="
  107. if (node.src.indexOf(proxy_node)==-1) {
  108. node.setAttribute('src', node.getAttribute('src')
  109. .replace(node.getAttribute('src'), proxy_node + node.getAttribute('src') + '&f=1'));
  110. }
  111. }
  112. }
  113. }
  114. if (red_link_cfg != false) {
  115. // Reddit Link Hijack Remover by jmesmon https://greasyfork.org/tr/scripts/20752-reddit-link-hijack-remover
  116. (function () {
  117. 'use strict';
  118. if (window.location.host == "www.reddit.com") {
  119. function cl(ac) {
  120. var a = ac.querySelectorAll('a[data-href-url]');
  121. var ct_out = 0, ct_aff = 0, ct = 0, ct_in;
  122. for (var i = 0; i < a.length; i++) {
  123. /*
  124. // This is reddit's function to determine the url, which is stored in `o`.
  125. // It then hooks window unload to call sendBeacon with the url in `o` or
  126. // modifies the href attribute (if sendBeacon is disabled in config or unsupported by the browser).
  127. function o(e) {
  128. var t = $(e),
  129. r = Date.now(),
  130. o;
  131. return t.attr('data-inbound-url')
  132. ? o = t.attr('data-inbound-url')
  133. : !n && t.attr('data-outbound-expiration') > r && (o = t.attr('data-outbound-url')),
  134. o && (i ? s = o : e.href = o),
  135. !0
  136. }
  137. */
  138.  
  139. // Some minimal counting so we can tell things are working
  140. if (a[i].getAttribute('data-inbound-url')) {
  141. ct_in++;
  142. }
  143. if (a[i].getAttribute('data-affiliate-url')) {
  144. ct_aff++;
  145. }
  146. if (a[i].getAttribute('data-outbound-url') || a[i].getAttribute('data-outbound-expiration')) {
  147. ct_out++;
  148. }
  149.  
  150. // Goals:
  151. // - make sure `o` stays undefined.
  152. // - avoid ever getting this function called
  153. // Removing all the relevent attributes gets us both of those
  154.  
  155. // Unclear what the purpose of these is, but they are being used to
  156. // re-write urls (and trigger entry to the fn above), so remove.
  157. a[i].removeAttribute('data-inbound-url');
  158.  
  159. // Doesn't appear that reddit is injecting these affiliate links
  160. // anymore, but no reason to remove this
  161. a[i].removeAttribute('data-affiliate-url');
  162.  
  163. // We don't actually need to remove this, but it does short circuit
  164. // the condition quicker & cleans up the html, so do it.
  165. a[i].removeAttribute('data-outbound-expiration');
  166. a[i].removeAttribute('data-outbound-url');
  167.  
  168. ct++;
  169. }
  170.  
  171. console.log('>>>');
  172. console.log('Outbound redirects removed: ' + ct_out);
  173. console.log('Inbound redirects removed: ' + ct_out);
  174. console.log('Affiliate redirects removed: ' + ct_aff);
  175. console.log('Total redirects removed: ' + ct);
  176. console.log('<<<');
  177. }
  178.  
  179. var tbls = document.querySelector('#siteTable');
  180. var obs = new MutationObserver(function (r, self) {
  181. for (var i = 0; i < r.length; i++) {
  182. var ad = r[i].addedNodes;
  183. for (var j = 0; j < ad.length; j++) {
  184. var n = ad[j];
  185. cl(n);
  186. }
  187. }
  188. });
  189. obs.observe(tbls, {
  190. childList: true
  191. });
  192.  
  193. // TODO: consider patching out window.navigator.sendBeacon (which reddit only uses for this link tracking)
  194. // TODO: consider blocking the recent_srs cookie used for tracking
  195. cl(document);
  196. }
  197. }) ();
  198. }
  199. // Reddit Link Hijack Remover by jmesmon https://greasyfork.org/tr/scripts/20752-reddit-link-hijack-remover
  200. // Restore Youtube Embed Defaults by chaimleib (modified for paticik) https://greasyfork.org/en/scripts/829-restore-youtube-embed-defaults/code
  201. if (pati_ytb_fix != false) {
  202. if (window.location.hostname == "forum.paticik.com") {
  203. pati_ytb();
  204. document.addEventListener('click', pati_ytb);
  205. function pati_ytb() {
  206. var i, j, k, index;
  207. var video_id, video_url, video_link;
  208.  
  209. var risky_elements, risky_attributes, risky_node;
  210. var risky_tags = ["object", "embed"];
  211.  
  212. var bad_elements = [];
  213. var bad_ids = [];
  214. for (i = 0; i < risky_tags.length; i++) {
  215. risky_elements = document.getElementsByTagName(risky_tags[i]);
  216. for (j = 0; j < risky_elements.length; j++) {
  217. index = 0;
  218. risky_attributes = risky_elements[j].attributes;
  219. for (k = 0; k < risky_attributes.length; k++) {
  220. risky_node = risky_attributes[k].nodeValue;
  221. if ((risky_node.indexOf("youtube.com") >= 0) || (risky_node.indexOf("ytimg.com") >= 0) || (risky_node.indexOf("youtube-nocookie.com") >= 0)) {
  222. risky_elements[j].style.display = "none";
  223. if (risky_node.indexOf("/v/") >= 0) {
  224. index = risky_node.indexOf("/v/") + 3;
  225. } else if (risky_node.indexOf("?v=") >= 0) {
  226. index = risky_node.indexOf("?v=") + 3;
  227. } else if (risky_node.indexOf("/embed/") >= 0) {
  228. index = risky_node.indexOf("/embed/") + 7;
  229. }
  230. if (index > 0) {
  231. video_id = risky_node.substring(index, index + 11);
  232. bad_elements.push(risky_elements[j]);
  233. bad_ids.push(video_id);
  234. }
  235. break;
  236. }
  237. }
  238. }
  239. }
  240.  
  241. for (i = 0; i < bad_ids.length; i++) {
  242. video_id = bad_ids[i];
  243. video_url = "//www.youtube.com/embed/" + video_id;
  244. video_link = document.createElement("iframe");
  245. video_link.setAttribute("src", video_url);
  246.  
  247. video_link.setAttribute("width", '100%');
  248. video_link.setAttribute("height", '350');
  249. video_link.setAttribute("frameborder", "0");
  250. video_link.setAttribute("allowfullscreen", "1");
  251. bad_elements[i].parentNode.replaceChild(video_link, bad_elements[i]);
  252. }
  253. }
  254. }
  255. }
  256. // Restore Youtube Embed Defaults by chaimleib https://greasyfork.org/en/scripts/829-restore-youtube-embed-defaults/code