Advertisement
jcunews

convert-redit-image-urls.user.js

Nov 14th, 2022 (edited)
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Convert Reddit hosted image preview URLs to direct image URLs
  3. // @namespace   https://greasyfork.org/en/users/85671-jcunews
  4. // @version     1.0.1
  5. // @license     AGPL v3
  6. // @author      jcunews
  7. // @description Context: https://www.reddit.com/r/GreaseMonkey/comments/yuzgc2/request_script_to_replace_parts_of_a_url/
  8. // @match       *://*/*
  9. // @grant       none
  10. // @run-at      document-start
  11. // ==/UserScript==
  12.  
  13. //Applies to: URL of images, URL of links, text content of links if same as links' URL, and image URL in browser address bar (i.e. auto redirect).
  14.  
  15. ((rx, m) => {
  16.   function chkEle(link, prop, m) {
  17.     if (m = link[prop].match(rx)) {
  18.       m = m[1] + "i" + m[2];
  19.       if (link.textContent.trim() === link[prop]) link.textContent = m;
  20.       link[prop] = m
  21.     }
  22.   }
  23.   function doNode(node, recurs) {
  24.     switch (node.tagName) {
  25.       case "A":
  26.         chkEle(node, "href");
  27.         break;
  28.       case "IMG":
  29.         chkEle(node, "src");
  30.         break;
  31.       default:
  32.         if (recurs && node.tagName) chkNodes(node.querySelectorAll("a,img"))
  33.     }
  34.   }
  35.   function chkNodes(nodes) {
  36.     nodes.forEach(node => doNode(node, true))
  37.   }
  38.   rx = /^(https:\/\/)preview(\.redd\.it\/[^\.]+\.[a-z]+)\?/;
  39.   if (m = location.href.match(rx)) {
  40.     return location.href = m[1] + "i" + m[2]
  41.   }
  42.   (new MutationObserver(recs => {
  43.     recs.forEach(rec => {
  44.       if (rec.addedNodes) {
  45.         chkNodes(rec.addedNodes)
  46.       } else doNode(rec.target)
  47.     })
  48.   })).observe(document, {childList: true, subtree: true, attributeFilter: ["href", "src"]})
  49. })()
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement