Last active
April 24, 2022 06:31
-
-
Save lbmaian/c53a9642e15495006e4324d50adce46e to your computer and use it in GitHub Desktop.
old.reddit.com fixes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name old.reddit.com fixes | |
// @namespace https://gist.github.com/lbmaian/c53a9642e15495006e4324d50adce46e | |
// @version 0.2 | |
// @description old.reddit.com fixes: converts subreddit i.redd.it links to comment links, removes backslashes from links | |
// @author lbmaian | |
// @match https://old.reddit.com/r/* | |
// @match https://old.reddit.com/user/* | |
// @icon https://www.google.com/s2/favicons?domain=reddit.com | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const isSubredditPage = !location.href.includes('/comments/'); | |
function fixLinks(target, logPrefix) { | |
if (isSubredditPage) { | |
for (const eltLink of target.querySelectorAll('a.thumbnail[href^="https://i.redd.it/"], a.title[href^="https://i.redd.it/"]')) { | |
console.debug(logPrefix, 'found i.redd.it link', eltLink); | |
convertToCommentLink(eltLink); | |
} | |
for (const eltLink of target.querySelectorAll('a.thumbnail[href^="https://v.redd.it/"], a.title[href^="https://v.redd.it/"]')) { | |
console.debug(logPrefix, 'found v.redd.it link', eltLink); | |
convertToCommentLink(eltLink); | |
} | |
} | |
for (const eltLink of target.querySelectorAll('a[href*="%5C"]')) { | |
console.debug(logPrefix, 'found broken link', eltLink); | |
fixBrokenLink(eltLink); | |
} | |
} | |
function convertToCommentLink(eltLink) { | |
let eltThing = eltLink; | |
while (eltThing.tagName !== 'DIV' || !eltThing.classList.contains('thing')) { | |
if (eltThing === null) { | |
console.error('could not find div.thing parent for element', eltThing); | |
return; | |
} | |
eltThing = eltThing.parentNode; | |
} | |
const eltCommentLink = eltThing.querySelector('a.bylink[data-event-action$="comments"]'); | |
if (eltCommentLink === null) { | |
console.error('could not find a.bylink.comments within element', eltThing); | |
return; | |
} | |
const newHref = eltCommentLink.href; | |
if (eltLink.dataset.hrefUrl === eltLink.href) { | |
console.log('data-href-url: %s => %s', eltLink.dataset.hrefUrl, newHref); | |
eltLink.dataset.hrefUrl = newHref; | |
} | |
if (eltLink.dataset.outboundUrl === eltLink.href) { | |
console.log('data-outbound-url: %s => %s', eltLink.dataset.outboundUrl, newHref); | |
eltLink.dataset.outboundUrl = newHref; | |
} | |
console.log('href: %s => %s', eltLink.href, newHref); | |
eltLink.href = newHref; | |
} | |
function fixBrokenLink(eltLink) { | |
const newHref = eltLink.href.replaceAll('%5C', '') | |
const linkHtml = eltLink.innerHTML; | |
if (linkHtml === eltLink.href.replaceAll('%5C', '\\')) { // if <a href="XXX">XXX</a> | |
console.log('a innerHTML: %s => %s', linkHtml, newHref); | |
eltLink.textContent = newHref; | |
} | |
console.log('href: %s => %s', eltLink.href, newHref); | |
eltLink.href = newHref; | |
} | |
new MutationObserver(mutations => { | |
for (const mutation of mutations) { | |
for (const node of mutation.addedNodes) { | |
if (node.nodeType === 1) { // element | |
fixLinks(node, '[observer]'); | |
} | |
} | |
} | |
}).observe(document.body, { | |
childList: true, | |
subtree: true, | |
}); | |
fixLinks(document, '[init]'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment