Last active
January 8, 2023 14:58
-
-
Save Rainyan/0fdf3d449404fb786ea1a29589608ce5 to your computer and use it in GitHub Desktop.
Fix old reddit URLs – Fixes problems with displaying old.reddit URLs. Examples of breakage include links with underscores, double quotes, or trailing closing braces. If you also want to enforce old.reddit via another userscript, see: https://gist.github.com/Rainyan/fbb1ff337eac3e6c604d9a6eee7badda
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 Fix old reddit URLs | |
// @description Fixes problems with displaying old.reddit URLs. Examples of breakage include links with underscores, double quotes, or trailing closing braces. | |
// @version 1.4.1 | |
// @namespace redditFixOldLinks | |
// @updateURL https://gist.githubusercontent.com/Rainyan/0fdf3d449404fb786ea1a29589608ce5/raw/ | |
// @include https://old.reddit.com/* | |
// @run-at document-idle | |
// ==/UserScript== | |
// Optional: If you want to enforce old.reddit via userscript, see also: | |
// https://gist.github.com/Rainyan/fbb1ff337eac3e6c604d9a6eee7badda | |
// Will replace these patterns in hyperlink hrefs. | |
const URL_REPLACEMENTS = { | |
"%5C\"": "%22", | |
"%5C_": "%5F", | |
}; | |
// Will replace these patterns in hyperlink titles. | |
const TEXT_REPLACEMENTS = { | |
"\\\"": "\"", | |
"\\_": "_", | |
}; | |
const NUM_LINKS = document.links.length; | |
for (let i = 0; i < NUM_LINKS; ++i) { | |
let hyperlink_title_equals_url = false; | |
try { | |
hyperlink_title_equals_url = (decodeURI(document.links[i].text) === decodeURI(document.links[i].href)); | |
} catch { | |
// If it failed, we have a malformed URI, or most likely link title is not an URI. Do nothing. | |
} | |
for (const key in URL_REPLACEMENTS) { | |
document.links[i].href = document.links[i].href.replaceAll(key, URL_REPLACEMENTS[key]); | |
} | |
// If hyperlink title is the same as the URL, also clean up formatting of the title. | |
if (hyperlink_title_equals_url) { | |
for (const key in TEXT_REPLACEMENTS) { | |
document.links[i].text = document.links[i].text.replaceAll(key, TEXT_REPLACEMENTS[key]); | |
} | |
document.links[i].text = decodeURI(document.links[i].text); | |
} | |
// Special cases | |
else { | |
// This can fail on non-URI content, so we wrap it to an empty try-catch, | |
// because there's no need to do anything if decode failed. | |
try { | |
// For some reason, Reddit can eat the trailing ")" character from an URL, | |
// so manually checking for that. This can occur for Wikipedia disambiguation page links, etc. | |
// | |
// TODO: we will still fail for links that have text different from the href. | |
// Example of such content: [fighting windmills](https://en.wikipedia.org/wiki/Tilting_at_windmills_(disambiguation)), | |
// where old.reddit will end the URL on first ), and render the closing ) as text. | |
// Should rework this to instead parse the immediately following text to detect the failed closing brace case. | |
if (decodeURI(document.links[i].href) + ")" === decodeURI(document.links[i].text)) { | |
document.links[i].href += ")"; | |
document.links[i].text = decodeURI(document.links[i].text); | |
} | |
} catch { } | |
} | |
} |
Thanks! I've updated the file extension.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome script! You should change the file extension to
.user.js
so Tampermonkey recognizes it as a userscript and offers to install it when accessing the raw link.