Fix image links on Old Reddit

Fix opening redd.it image links on Old Reddit

当前为 2024-07-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Fix image links on Old Reddit
  3. // @namespace https://github.com/abdurazaaqmohammed
  4. // @version 1.1.1
  5. // @description Fix opening redd.it image links on Old Reddit
  6. // @author Abdurazaaq Mohammed
  7. // @homepage https://github.com/abdurazaaqmohammed/userscripts
  8. // @license The Unlicense
  9. // @supportURL https://github.com/abdurazaaqmohammed/userscripts/issues
  10. // @match https://*.reddit.com/*
  11. // @exclude https://www.reddit.com/*
  12. // @exclude https://new.reddit.com/*
  13. // @run-at document-start
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. (function () {
  18. 'use strict';
  19.  
  20. function openImageViewer(imgLink) {
  21. // Uncomment below and comment/delete the other line if you want to redirect to the image directly instead of opening in a new tab.
  22. //window.location.href = 'https://abdurazaaqmohammed.github.io/website/imgviewer?viewimg=' + imgLink;
  23. window.open('https://abdurazaaqmohammed.github.io/website/imgviewer?viewimg=' + imgLink);
  24. }
  25.  
  26. function fixImageLinks(mutationsList, observer) {
  27. document.querySelectorAll(".media-preview").forEach((gallery) => gallery.addEventListener("click", function() { openImageViewer(Array.from(gallery.querySelectorAll('img')).map(id => `${id.src.split('?')[0]}`).join(',')) }) );
  28.  
  29. document.querySelectorAll('a[href^="https://preview.redd.it"], a[href^="https://i.redd.it"]').forEach(link => {
  30. const imgLink = link.href;
  31. // Overwriting the href would break copying the image link, so remove the old href and set onClick to open the viewer instead.
  32. link.removeAttribute("href");
  33. link.addEventListener("click", function() { openImageViewer(imgLink); });
  34. });
  35. }
  36.  
  37. const url = window.location.href;
  38. const brokenImg = 'https://old.reddit.com/r/funny/comments/media/nice_hat/?url=';
  39. url.startsWith(brokenImg) ? window.location.href = 'https://abdurazaaqmohammed.github.io/website/imgviewer?viewimg=' + url.split(brokenImg)[1] : new MutationObserver(fixImageLinks).observe(document, { subtree: true, childList: true });
  40.  
  41. })();