Skip to content

Instantly share code, notes, and snippets.

@dbazuin
Forked from bramus/bookmarklet.md
Created November 24, 2022 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbazuin/b10ef93027701e40432ea0e35fe1f4b8 to your computer and use it in GitHub Desktop.
Save dbazuin/b10ef93027701e40432ea0e35fe1f4b8 to your computer and use it in GitHub Desktop.
Mastodon User Page Bookmarklet
javascript:(function(){
const MY_MASTO_LOCAL_DOMAIN = 'front-end.social'; /* 👈 Change this value */
const MY_MASTO_WEB_DOMAIN = MY_MASTO_LOCAL_DOMAIN; /* 👈 Only change this value if your Masto host is hosted an different domain than the LOCAL_DOMAIN */
function tryAndGetUserName() {
/* Profile with a moved banner (e.g. https://mastodon.social/@bramus): follow that link */
const userNewProfile = document.querySelector('.moved-account-banner .button')?.getAttribute('href');
if (userNewProfile) {
return userNewProfile.substring(2);
}
/* Profile page, e.g. https://fediverse.zachleat.com/@zachleat and https://front-end.social/@mia */
const userFromProfilePage = document.querySelector('meta[property="profile:username"]')?.getAttribute('content');
if (userFromProfilePage) {
/* Don’t return if already watching on own LOCAL_DOMAIN instance */
if (window.location.host === MY_MASTO_LOCAL_DOMAIN) return null;
return userFromProfilePage;
};
/* Message detail, e.g. https://front-end.social/@mia/109348973362020954 and https://bell.bz/@andy/109392510558650993 and https://bell.bz/@andy/109392510558650993 */
const userFromDetailPage = document.querySelector('.detailed-status .display-name__account')?.innerText;
if (userFromDetailPage) return userFromDetailPage.substring(1);
return null;
};
let user = tryAndGetUserName();
if (!user) return;
/* Trim off @domain suffix in case it matches with MY_MASTO_LOCAL_DOMAIN. This due to https://github.com/mastodon/mastodon/issues/21469 */
if (user.endsWith(`@${MY_MASTO_LOCAL_DOMAIN}`)) {
user = user.substring(0, user.length - `@${MY_MASTO_LOCAL_DOMAIN}`.length);
}
window.location.href = `https://${MY_MASTO_WEB_DOMAIN}/@${user}`;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment