Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Convert Reddit hosted image preview URLs to direct image URLs
- // @namespace https://greasyfork.org/en/users/85671-jcunews
- // @version 1.0.1
- // @license AGPL v3
- // @author jcunews
- // @description Context: https://www.reddit.com/r/GreaseMonkey/comments/yuzgc2/request_script_to_replace_parts_of_a_url/
- // @match *://*/*
- // @grant none
- // @run-at document-start
- // ==/UserScript==
- //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).
- ((rx, m) => {
- function chkEle(link, prop, m) {
- if (m = link[prop].match(rx)) {
- m = m[1] + "i" + m[2];
- if (link.textContent.trim() === link[prop]) link.textContent = m;
- link[prop] = m
- }
- }
- function doNode(node, recurs) {
- switch (node.tagName) {
- case "A":
- chkEle(node, "href");
- break;
- case "IMG":
- chkEle(node, "src");
- break;
- default:
- if (recurs && node.tagName) chkNodes(node.querySelectorAll("a,img"))
- }
- }
- function chkNodes(nodes) {
- nodes.forEach(node => doNode(node, true))
- }
- rx = /^(https:\/\/)preview(\.redd\.it\/[^\.]+\.[a-z]+)\?/;
- if (m = location.href.match(rx)) {
- return location.href = m[1] + "i" + m[2]
- }
- (new MutationObserver(recs => {
- recs.forEach(rec => {
- if (rec.addedNodes) {
- chkNodes(rec.addedNodes)
- } else doNode(rec.target)
- })
- })).observe(document, {childList: true, subtree: true, attributeFilter: ["href", "src"]})
- })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement