Skip to content

Commit

Permalink
Mastodon: don't redirect on logged-in instances
Browse files Browse the repository at this point in the history
Close rugk#92
  • Loading branch information
austinhuang0131 committed Mar 18, 2023
1 parent 755dbc2 commit 5876255
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/content_script/mastodonInject.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ function waitForElement(selector, multiple = false, timeoutDuration = 200000) {
});
}

/**
* Checks whether the user has logged into the instance.
*
* @returns {boolean}
*/
async function checkLoggedIn() {
try {
const initialState = await waitForElement("#initial-state", false);
return JSON.parse(initialState.textContent).meta.access_token != null;
} catch(error) {
// cannot fetch login status
return false;
}
}

/**
* Inject replacement onClick handler for Follow button.
*
Expand Down Expand Up @@ -137,8 +152,12 @@ async function injectInteractionButtons() {
* @returns {void}
*/
function initInjections() {
injectFollowButton().catch(console.error);
injectInteractionButtons().catch(console.error);
checkLoggedIn().then((r) => {
if (!r) {
injectFollowButton().catch(console.error);
injectInteractionButtons().catch(console.error);
}
});
}

/**
Expand Down

0 comments on commit 5876255

Please sign in to comment.