Imgur Mirror (Modified)

Switches all imgur links to imgurp.com

目前为 2018-03-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Imgur Mirror (Modified)
  3. // @version 2.7.4
  4. // @description Switches all imgur links to imgurp.com
  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. // @grant unsafeWindow
  15. // @namespace https://greasyfork.org/users/159159
  16. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  17. // @require https://greasyfork.org/scripts/37236-monkeyconfig/code/MonkeyConfig.js
  18. // ==/UserScript==
  19. var cfg = new MonkeyConfig({
  20. title: 'Imgur Mirror Configuration',
  21. menuCommand: true,
  22. params: {
  23. wikipedia_redirect: {
  24. type: 'checkbox',
  25. default: false
  26. },
  27. reddit_redirect: {
  28. type: 'checkbox',
  29. default: false
  30. },
  31. imgur_redirect: {
  32. type: 'checkbox',
  33. default: true
  34. },
  35. reddit_link_hijack_remover: {
  36. type: 'checkbox',
  37. default: true
  38. },
  39. google_yandex_link_hijack_remover: {
  40. type: 'checkbox',
  41. default: true
  42. },
  43. open_links_in_new_tab: {
  44. type: 'checkbox',
  45. default: true
  46. },
  47. paticik_convert_links_to_video: {
  48. type: 'checkbox',
  49. default: true
  50. },
  51. paticik_youtube_fullscreen_fix: {
  52. type: 'checkbox',
  53. default: true
  54. }
  55.  
  56. }
  57. });
  58.  
  59. var red_cfg = cfg.get('reddit_redirect');
  60. var wiki_cfg = cfg.get('wikipedia_redirect');
  61. var img_cfg = cfg.get('imgur_redirect');
  62. var red_link_cfg = cfg.get('reddit_link_hijack_remover');
  63. var yan_link_cfg = cfg.get('google_yandex_link_hijack_remover');
  64. var new_tab_cfg = cfg.get('open_links_in_new_tab');
  65. var pati_ytb_fix = cfg.get('paticik_youtube_fullscreen_fix');
  66. var pati_url_fix = cfg.get('paticik_convert_links_to_video');
  67.  
  68. changeLinks();
  69. document.addEventListener('scroll', changeLinks);
  70. document.addEventListener('click', changeLinks);
  71. function get_ext(ext_url) {
  72. return (ext_url = ext_url.substr(1 + ext_url.lastIndexOf("/")).split('?')[0]).split('#')[0].substr(ext_url.lastIndexOf("."))
  73. }
  74. //Remove Yandex/Google Redirect by raletag https://greasyfork.org/en/scripts/22737-remove-yandex-redirect
  75. if (yan_link_cfg != false) {
  76. (function() {
  77. 'use strict';
  78. var win = unsafeWindow || window;
  79. if (win.top !== win.self) return;
  80.  
  81. function remove (e) {
  82. var links = e.querySelectorAll('a[onmousedown*="/clck/jsredir"]'), i;
  83. for (i = links.length - 1; i >= 0; --i) {
  84. links[i].removeAttribute('onmousedown');
  85. }
  86. var links = e.querySelectorAll('a[onmousedown*="return rwt"]'), i;
  87. for (i = links.length - 1; i >= 0; --i) {
  88. links[i].removeAttribute('onmousedown');
  89. }
  90. links = e.querySelectorAll('a[data-counter]');
  91. for (i = links.length - 1; i >= 0; --i) {
  92. links[i].removeAttribute('data-counter');
  93. links[i].removeAttribute('data-bem');
  94. }
  95. links = e.querySelectorAll('a[data-vdir-href]');
  96. for (i = links.length - 1; i >= 0; --i) {
  97. links[i].removeAttribute('data-vdir-href');
  98. links[i].removeAttribute('data-orig-href');
  99. }
  100. }
  101.  
  102. remove (document.body);
  103.  
  104. var o = new MutationObserver(function(ms){
  105. ms.forEach(function(m){
  106. m.addedNodes.forEach(function(n){
  107. if (n.nodeType !== Node.ELEMENT_NODE) {
  108. return;
  109. }
  110. remove(n);
  111. });
  112. });
  113. });
  114. o.observe(document.body, {childList: true, subtree: true});
  115. })();
  116. }
  117. //Remove Yandex/Google Redirect by raletag https://greasyfork.org/en/scripts/22737-remove-yandex-redirect
  118. function changeLinks() {
  119. if (window.location.hostname == "forum.paticik.com") {
  120. $(".img").click(function () {
  121. $(this).removeClass("img");
  122. $(this).removeClass("img2");
  123. $(this).addClass("img-exp");
  124. });
  125. $(".img-exp").click(function () {
  126. $(this).removeClass("img2");
  127. $(this).removeClass("img-exp");
  128. $(this).removeClass("img");
  129. $(this).removeAttr( 'style' );
  130. $(this).css({'max-width': '450px', 'max-height': '100%'});
  131. $(this).addClass("img2");
  132. });
  133. $(".img2").click(function () {
  134. $(this).removeAttr( 'style' );
  135. $(this).removeClass("img2");
  136. $(this).addClass("img");
  137. });
  138. }
  139. if (img_cfg != false) {
  140. //wikipedia
  141. if (wiki_cfg != false) {
  142. var wurl = window.location.href;
  143. if ((wurl.indexOf("duckduckgo.com/?") > -1) && (wurl.indexOf("duckduckgo.com/html") == -1)) {
  144. window.stop();
  145. var query = location.search;
  146. query = query.split('q=');
  147. window.location.replace("https://duckduckgo.com/html/?q="+query[query.length-1]);
  148. }
  149. if ((window.location.href).indexOf("wikipedia.kim") > -1) { $('.band-alert').remove(); }
  150. var wikiList = document.querySelectorAll('a[href*="wikipedia.org"]');
  151. for (var i = 0; i < wikiList.length; i++)
  152. {
  153. wikiList[i].href = wikiList[i].href.replace('wikipedia.org/','wikipedia.kim/');
  154. }
  155. }
  156. //wikipedia
  157. //imgur
  158. var imgur_old_href = "imgur.com";
  159. var imgur_new_href = "imgurp.com";
  160. var nodeList = document.querySelectorAll('[href*="imgur.com"]');
  161. for (var i = 0; i < nodeList.length; i++)
  162. {
  163. if (nodeList[i].href.indexOf(imgur_new_href)==-1) {
  164. nodeList[i].href = nodeList[i].href.replace(imgur_old_href,imgur_new_href);
  165. var o_url = nodeList[i].href.split(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/);
  166. var lastPart = get_ext(nodeList[i].href);
  167. var url_part = o_url[5].split("/")[1];
  168. var url_part2 = '' + o_url[5].split("/")[1];
  169. if(lastPart == '.gifv' || lastPart == '.mp4') {
  170. if (url_part != "a" && url_part != "gallery") {
  171. nodeList[i].href = nodeList[i].href.replace(url_part,url_part2);
  172. nodeList[i].href = nodeList[i].href.replace(/\.(gifv|mp4)($|\?)/, '.gif');
  173. }
  174. }
  175. else {
  176. if (url_part != "a" && url_part != "gallery" && lastPart.indexOf(".") !== -1) { nodeList[i].href = nodeList[i].href.replace(url_part,url_part2); }
  177. }
  178. if (new_tab_cfg != false) { nodeList[i].setAttribute('target', '_blank'); }
  179. }
  180. }
  181.  
  182. var imgur_old_src = "imgur.com";
  183. var imgur_new_src = "imgurp.com";
  184. var nodeList_src = document.querySelectorAll('[src*="imgur.com"]');
  185. for (var i = 0; i < nodeList_src.length; i++)
  186. {
  187. 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);
  188. }
  189. }
  190. if (red_cfg != false) {
  191. var list = document.querySelectorAll('[href*="redd.it"]');
  192. for (var i = 0; i < list.length; i++) {
  193. var node = list.item(i);
  194. var proxy_node = "https://proxy.duckduckgo.com/iu/?u="
  195. if (node.href.indexOf(proxy_node)==-1) {
  196. node.setAttribute('href', node.getAttribute('href')
  197. .replace(node.getAttribute('href'), proxy_node + node.getAttribute('href') + '&f=1'));
  198. if (new_tab_cfg != false) { node.setAttribute('target', '_blank'); }
  199. }
  200. }
  201. list = document.querySelectorAll('[src*="redd.it"]');
  202. for (i = 0; i < list.length; i++) {
  203. var node = list.item(i);
  204. var proxy_node = "https://proxy.duckduckgo.com/iu/?u="
  205. if (node.src.indexOf(proxy_node)==-1) {
  206. node.setAttribute('src', node.getAttribute('src')
  207. .replace(node.getAttribute('src'), proxy_node + node.getAttribute('src') + '&f=1'));
  208. }
  209. }
  210. }
  211. }
  212. if (red_link_cfg != false) {
  213. // Reddit Link Hijack Remover by jmesmon https://greasyfork.org/tr/scripts/20752-reddit-link-hijack-remover
  214. (function () {
  215. 'use strict';
  216. if (window.location.host == "www.reddit.com") {
  217. function cl(ac) {
  218. var a = ac.querySelectorAll('a[data-href-url]');
  219. var ct_out = 0, ct_aff = 0, ct = 0, ct_in;
  220. for (var i = 0; i < a.length; i++) {
  221. /*
  222. // This is reddit's function to determine the url, which is stored in `o`.
  223. // It then hooks window unload to call sendBeacon with the url in `o` or
  224. // modifies the href attribute (if sendBeacon is disabled in config or unsupported by the browser).
  225. function o(e) {
  226. var t = $(e),
  227. r = Date.now(),
  228. o;
  229. return t.attr('data-inbound-url')
  230. ? o = t.attr('data-inbound-url')
  231. : !n && t.attr('data-outbound-expiration') > r && (o = t.attr('data-outbound-url')),
  232. o && (i ? s = o : e.href = o),
  233. !0
  234. }
  235. */
  236.  
  237. // Some minimal counting so we can tell things are working
  238. if (a[i].getAttribute('data-inbound-url')) {
  239. ct_in++;
  240. }
  241. if (a[i].getAttribute('data-affiliate-url')) {
  242. ct_aff++;
  243. }
  244. if (a[i].getAttribute('data-outbound-url') || a[i].getAttribute('data-outbound-expiration')) {
  245. ct_out++;
  246. }
  247.  
  248. // Goals:
  249. // - make sure `o` stays undefined.
  250. // - avoid ever getting this function called
  251. // Removing all the relevent attributes gets us both of those
  252.  
  253. // Unclear what the purpose of these is, but they are being used to
  254. // re-write urls (and trigger entry to the fn above), so remove.
  255. a[i].removeAttribute('data-inbound-url');
  256.  
  257. // Doesn't appear that reddit is injecting these affiliate links
  258. // anymore, but no reason to remove this
  259. a[i].removeAttribute('data-affiliate-url');
  260.  
  261. // We don't actually need to remove this, but it does short circuit
  262. // the condition quicker & cleans up the html, so do it.
  263. a[i].removeAttribute('data-outbound-expiration');
  264. a[i].removeAttribute('data-outbound-url');
  265.  
  266. ct++;
  267. }
  268.  
  269. console.log('>>>');
  270. console.log('Outbound redirects removed: ' + ct_out);
  271. console.log('Inbound redirects removed: ' + ct_out);
  272. console.log('Affiliate redirects removed: ' + ct_aff);
  273. console.log('Total redirects removed: ' + ct);
  274. console.log('<<<');
  275. }
  276.  
  277. var tbls = document.querySelector('#siteTable');
  278. var obs = new MutationObserver(function (r, self) {
  279. for (var i = 0; i < r.length; i++) {
  280. var ad = r[i].addedNodes;
  281. for (var j = 0; j < ad.length; j++) {
  282. var n = ad[j];
  283. cl(n);
  284. }
  285. }
  286. });
  287. obs.observe(tbls, {
  288. childList: true
  289. });
  290.  
  291. // TODO: consider patching out window.navigator.sendBeacon (which reddit only uses for this link tracking)
  292. // TODO: consider blocking the recent_srs cookie used for tracking
  293. cl(document);
  294. }
  295. }) ();
  296. }
  297. // Reddit Link Hijack Remover by jmesmon https://greasyfork.org/tr/scripts/20752-reddit-link-hijack-remover
  298. // Restore Youtube Embed Defaults by chaimleib (modified for paticik) https://greasyfork.org/en/scripts/829-restore-youtube-embed-defaults/code
  299. if (window.location.hostname == "forum.paticik.com") {
  300.  
  301. if (pati_url_fix != false) {
  302. $('body').prepend("<style> .img { cursor:zoom-in; position: absolute; left: 0; right: 0; max-width: 100%; max-height: 100%; margin: auto; overflow: auto; } .img-exp { cursor:zoom-out; position: absolute; margin-left: auto; margin-right: auto; left: 0; right: 0; overflow: auto; }</style>");
  303.  
  304. pati_img();
  305. document.addEventListener('click', pati_img);
  306. document.addEventListener('scroll', pati_img);
  307. function pati_img() {
  308. $('a').click(function(e)
  309. {
  310. var pattern = /([-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?(?:webm|mp4|mp4v|gif|gifv|png|jpg|jpeg))/gi;
  311. var regex = /\.\w{3,4}($|\?)/gmi;
  312. if (pattern.test(this)) {
  313. var target = $(this).attr('href');
  314. var match = target.match(regex);
  315. if (match) {
  316. var file = match[0].split(/\#|\?/g)[0];
  317. if (file == '.gifv' || file == '.mp4') {
  318. target = target.replace(/\.(gifv|mp4)($|\?)/, '.mp4');
  319. }
  320. }
  321. if (file == '.gif' && target.indexOf("imgur") > -1) { file = '.gifv'; }
  322. var video = $('<video controls="" controls autoplay src="' + target + '" style="max-width: 100%; max-height: 350px;"></video>');
  323. var imajine = $('<img id="img" class="img" src="' + target + '" style="overflow:visible" /></a>');
  324. /*
  325. var imaj = $('<img src="' + target + '" style="max-width: 100%; max-height: 350px;" />');
  326. if (file == '.gif') {
  327. $(this).replaceWith(imaj);
  328. e.preventDefault();
  329. }
  330. */
  331. if (file == '.png' || file == '.jpg' || file == '.jpeg' || file == '.gif') {
  332. $(this).replaceWith(imajine);
  333. e.preventDefault();
  334. }
  335. if (file == '.gifv' || file == '.mp4' || file == '.webm') {
  336. $(this).replaceWith(video);
  337. e.preventDefault();
  338. }
  339. }
  340. });
  341. }
  342. }
  343. if (pati_ytb_fix != false) {
  344. pati_ytb();
  345. document.addEventListener('click', pati_ytb);
  346. document.addEventListener('scroll', pati_ytb);
  347. function pati_ytb() {
  348. var i, j, k, index;
  349. var video_id, video_url, video_link;
  350. var risky_elements, risky_attributes, risky_node;
  351. var risky_tags = ["object", "embed"];
  352. var bad_elements = [];
  353. var bad_ids = [];
  354. for (i = 0; i < risky_tags.length; i++) {
  355. risky_elements = document.getElementsByTagName(risky_tags[i]);
  356. for (j = 0; j < risky_elements.length; j++) {
  357. index = 0;
  358. risky_attributes = risky_elements[j].attributes;
  359. for (k = 0; k < risky_attributes.length; k++) {
  360. risky_node = risky_attributes[k].nodeValue;
  361. if ((risky_node.indexOf("youtube.com") >= 0) || (risky_node.indexOf("ytimg.com") >= 0) || (risky_node.indexOf("youtube-nocookie.com") >= 0)) {
  362. risky_elements[j].style.display = "none";
  363. if (risky_node.indexOf("/v/") >= 0) {
  364. index = risky_node.indexOf("/v/") + 3;
  365. } else if (risky_node.indexOf("?v=") >= 0) {
  366. index = risky_node.indexOf("?v=") + 3;
  367. } else if (risky_node.indexOf("/embed/") >= 0) {
  368. index = risky_node.indexOf("/embed/") + 7;
  369. }
  370. if (index > 0) {
  371. video_id = risky_node.substring(index, index + 11);
  372. bad_elements.push(risky_elements[j]);
  373. bad_ids.push(video_id);
  374. }
  375. break;
  376. }
  377. }
  378. }
  379. }
  380.  
  381. for (i = 0; i < bad_ids.length; i++) {
  382. video_id = bad_ids[i];
  383. video_url = "//www.youtube.com/embed/" + video_id;
  384. video_link = document.createElement("iframe");
  385. video_link.setAttribute("src", video_url);
  386.  
  387. video_link.setAttribute("width", '100%');
  388. video_link.setAttribute("height", '350');
  389. video_link.setAttribute("frameborder", "0");
  390. video_link.setAttribute("allowfullscreen", "1");
  391. bad_elements[i].parentNode.replaceChild(video_link, bad_elements[i]);
  392. }
  393. }
  394. }
  395. }
  396. // Restore Youtube Embed Defaults by chaimleib https://greasyfork.org/en/scripts/829-restore-youtube-embed-defaults/code