Skip to content

Instantly share code, notes, and snippets.

@hazelweakly
Last active January 8, 2023 07:46
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hazelweakly/407068043965508a356b23ec6ef150b6 to your computer and use it in GitHub Desktop.
Save hazelweakly/407068043965508a356b23ec6ef150b6 to your computer and use it in GitHub Desktop.
Enron Mullet Is A Giant CryBaby
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Redirecting to the Fediverse</title>
<head>
<script>
// let's say this is hosted at example.com. Given a url of one of the forms:
// - example.com/@user@instance.com/post_id
// - example.com/@user@instance.com
// - example.com/@instance.com
// redirect accordingly
const url = new window.URL(window.location.href); // example.com/@user@instance.com/post_id
const path = url.pathname; // /@user@instance.com/post_id
const strippedPath = path.substring(path.indexOf("@") + 1); // user@instance.com/post_id
if (!strippedPath.includes('@')) { // if the url was of the form: example.com/@instance.com
window.location.href = `https://${strippedPath}`;
}
const [user, domain] = strippedPath.split("@"); // ["user", "instance.com/post_id"]
const [actualDomain, maybePost] = domain.split("/"); // ["instance.com", "post_id"]
window.location.href =
`https://${actualDomain}/@${user}` + (maybePost ? `/${maybePost}` : "");
// https://instance.com/@user/post_id
// if "/post_id" was never given, maybePost will be undefined and that won't show up
// resulting in the correct url of https://instance.com/@user
</script>
</head>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment