🏠 Home 

Imgur links rewriting on Ptt

Rewrite imgur links to bypass referrer check.


Install this script?
  1. // ==UserScript==
  2. // @name Imgur links rewriting on Ptt
  3. // @namespace https://github.com/gslin/imgur-links-rewriting-on-ptt
  4. // @match https://www.ptt.cc/bbs/*
  5. // @match https://www.ptt.cc/man/*
  6. // @grant GM_xmlhttpRequest
  7. // @version 0.20240912.1
  8. // @author Gea-Suan Lin <gslin@gslin.com>
  9. // @description Rewrite imgur links to bypass referrer check.
  10. // @license MIT
  11. // ==/UserScript==
  12. (() => {
  13. 'use strict';
  14. const get_imgur_id_suffix = url => {
  15. const a = url.match(/^https?:\/\/(i\.|m\.)?imgur\.com\/(\w+)\.?(\w+)?/);
  16. return [a[2], a[3]];
  17. };
  18. const re_imgur_album = /^https?:\/\/imgur\.com\/(a|gallery)\//;
  19. document.querySelectorAll('a[href*="//imgur.com/"], a[href*="//i.imgur.com/"], a[href*="//m.imgur.com/"]').forEach(async el => {
  20. // Remove ".richcontent" if existing.
  21. const next = el.nextElementSibling;
  22. if (next && next.classList.contains('richcontent')) {
  23. next.remove();
  24. }
  25. // Remove ".richcontent" if existing.
  26. const next2 = el.parentElement.nextElementSibling;
  27. if (next2 && next2.classList.contains('richcontent')) {
  28. next2.remove();
  29. }
  30. // Remove ".richcontent" for ".push" case.
  31. const el_p2 = el.parentElement.parentElement;
  32. if (el_p2 && el_p2.classList.contains('push')) {
  33. const el_p2_next = el_p2.nextElementSibling;
  34. if (el_p2_next && el_p2_next.classList.contains('richcontent')) {
  35. el_p2_next.remove();
  36. }
  37. }
  38. const href = el.getAttribute('href');
  39. let imgur_id = '';
  40. let imgur_suffix = '';
  41. if (href.match(re_imgur_album)) {
  42. // album case.
  43. const res = await new Promise(resolve => {
  44. GM_xmlhttpRequest({
  45. 'anonymous': true,
  46. 'headers': {
  47. 'Referer': 'https://imgur.com/',
  48. },
  49. 'method': 'GET',
  50. 'onload': res => {
  51. resolve(res.responseText);
  52. },
  53. 'url': href,
  54. });
  55. });
  56. const parser = new DOMParser();
  57. const doc = parser.parseFromString(res, 'text/html');
  58. const og_image = doc.querySelector('meta[property="og:image"]');
  59. const img_url = og_image.getAttribute('content');
  60. [imgur_id, imgur_suffix] = get_imgur_id_suffix(img_url);
  61. } else {
  62. // image case.
  63. [imgur_id, imgur_suffix] = get_imgur_id_suffix(href);
  64. }
  65. // Change to webp only if it's not gif.
  66. if (imgur_suffix !== 'gif') {
  67. imgur_suffix = 'webp';
  68. }
  69. const container = document.createElement('div');
  70. container.setAttribute('style', 'margin-top:0.5em;text-align:center;');
  71. const img = document.createElement('img');
  72. img.setAttribute('referrerpolicy', 'no-referrer');
  73. img.setAttribute('src', 'https://i.imgur.com/' + imgur_id + '.' + imgur_suffix);
  74. container.appendChild(img);
  75. el.insertAdjacentElement('afterend', container);
  76. });
  77. })();