Skip to content

Instantly share code, notes, and snippets.

@drt24
Forked from bramus/bookmarklet.md
Last active November 24, 2022 10:46
Show Gist options
  • Save drt24/b54cc48bdf24b04d16d6600b0f9766c9 to your computer and use it in GitHub Desktop.
Save drt24/b54cc48bdf24b04d16d6600b0f9766c9 to your computer and use it in GitHub Desktop.
Mastodon User Page Bookmarklet
javascript:(function(){
const MY_MASTO_LOCAL_DOMAIN = 'social.coop'; /* 👈 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