Fix image links on Old Reddit

Fix opening redd.it image links on Old Reddit

目前为 2024-05-28 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Fix image links on Old Reddit
  3. // @namespace https://github.com/abdurazaaqmohammed
  4. // @version 1.0.2
  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. // @match https://abdurazaaqmohammed.github.io/website/imgviewer
  12. // @exclude https://www.reddit.com/*
  13. // @exclude https://new.reddit.com/*
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20. const url = window.location.href;
  21.  
  22. function openImageViewer(imgLink) {
  23. GM_setValue('img', imgLink);
  24. // Uncomment below and comment/delete the other line if you want to redirect to the image directly instead of opening in a new tab.
  25. //window.location.href = 'https://abdurazaaqmohammed.github.io/website/imgviewer';
  26. window.open('https://abdurazaaqmohammed.github.io/website/imgviewer');
  27. }
  28.  
  29. function fixImageLinks(mutationsList, observer) {
  30. document.querySelectorAll('a[href^="https://preview.redd.it"], a[href^="https://i.redd.it"]').forEach(link => {
  31. const imgLink = link.href;
  32. // Overwriting the href would break copying the image link, so remove the old href and set onClick to open the viewer instead.
  33. link.removeAttribute("href");
  34. link.addEventListener("click", function() { openImageViewer(imgLink); });
  35. });
  36. }
  37.  
  38. url.startsWith('https://old.reddit.com/') ? new MutationObserver(fixImageLinks).observe(document.body, { subtree: true, childList: true }) : (document.getElementById('urlInput').value = GM_getValue('img'), document.getElementById('displayButton').click());
  39.  
  40. })();