Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Mastodon support #102

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/content_script/mastodonInject.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function onClickFollow(event) {
function onClickInteract(event) {
event.stopPropagation();
event.preventDefault();
const articleElement = event.target.closest(".status.status-public[data-id]");
const articleElement = event.target.closest(".status.status-public[data-id], .status.status-unlisted[data-id]");
const getId = () => {
const rawId = articleElement.getAttribute("data-id");
return rawId.slice(0, 2) === "f-" ? rawId.slice(2) : rawId;
Expand Down 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 isLoggedIn() {
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,13 @@ async function injectInteractionButtons() {
* @returns {void}
*/
function initInjections() {
injectFollowButton().catch(console.error);
injectInteractionButtons().catch(console.error);
isLoggedIn().then((isLoggedIn) => {
if (isLoggedIn) {
return;
}
injectFollowButton().catch(console.error);
injectInteractionButtons().catch(console.error);
});
}

/**
Expand Down