Fix image links on Old Reddit

Fix opening redd.it image links on Old Reddit

目前为 2024-01-15 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Fix image links on Old Reddit
  3. // @namespace https://github.com/fxolan
  4. // @version 1.0
  5. // @description Fix opening redd.it image links on Old Reddit
  6. // @author Abdurazaaq Mohammed
  7. // @homepage https://github.com/fxolan/userscripts
  8. // @license The Unlicense
  9. // @supportURL https://github.com/fxolan/userscripts/issues
  10. // @match https://old.reddit.com/*
  11. // @match https://fxolan.github.io/website/imgviewer
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // ==/UserScript==
  15.  
  16. (function () {
  17. 'use strict';
  18. const url = window.location.href;
  19.  
  20. function openImageViewer(imgLink) {
  21. GM_setValue('img', imgLink);
  22. // Uncomment below and comment/delete the other line if you want to redirect to the image directly instead of opening in a new tab.
  23. //window.location.href = 'https://fxolan.github.io/website/imgviewer';
  24. window.open('https://fxolan.github.io/website/imgviewer');
  25. }
  26.  
  27. function fixImageLinks(mutationsList, observer) {
  28. const imageLinks = document.querySelectorAll('a[href^="https://preview.redd.it"], a[href^="https://i.redd.it"]');
  29. imageLinks.forEach(link => {
  30. const imgLink = link.href;
  31. // Overwriting the href will break copying the image link
  32. link.removeAttribute("href");
  33. link.addEventListener("click", function() { openImageViewer(imgLink); });
  34. });
  35. }
  36.  
  37. if (url.startsWith('https://old.reddit.com/')) {
  38. const observer = new MutationObserver(fixImageLinks);
  39. observer.observe(document.body, { subtree: true, childList: true });
  40. } else {
  41. document.getElementById('urlInput').value = GM_getValue('img');
  42. document.getElementById('displayButton').click();
  43. }
  44. })();