Skip to content

Commit

Permalink
Merge branch 'fix/v4_bugs' of https://github.com/hueyy/mastodon-simpl…
Browse files Browse the repository at this point in the history
…ified-federation into hueyy-fix/v4_bugs
  • Loading branch information
rugk committed Dec 13, 2022
2 parents 6105b79 + 379f8f3 commit 2c82e7b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/background/modules/AutoRemoteFollow.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ async function onTabUpdate(tabId, changeInfo) {
const ownMastodon = await AddonSettings.get("ownMastodon");
const currentURL = new URL(changeInfo.url);
if (ownMastodon.server !== currentURL.hostname){
browser.tabs.executeScript({
browser.tabs.executeScript(tabId, {
file: "/content_script/mastodonInject.js",
});
}
Expand Down
36 changes: 21 additions & 15 deletions src/content_script/mastodonInject.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ function onClickInteract(event) {
event.stopPropagation();
event.preventDefault();
const articleElement = event.target.closest("article[data-id]");
const getId = () => {
const rawId = articleElement.getAttribute("data-id");
return rawId.slice(0, 2) === "f-" ? rawId.slice(2) : rawId;
};
const tootId = (
articleElement === null
? window.location.pathname.split("/").slice(-1)[0]
: articleElement.getAttribute("data-id")
: getId()
);
// activate AutoRemoteFollow
window.open(`/interact/${tootId}`, "_blank");
Expand Down Expand Up @@ -57,14 +61,14 @@ function waitForElement(selector, multiple = false, timeoutDuration = 200000) {
}, timeoutDuration);

const element = getElement();
if (isElementFound(element)){
if (isElementFound(element)) {
window.clearTimeout(timeout);
return resolve(element);
}

const observer = new MutationObserver(() => {
const element = getElement();
if (isElementFound(element)){
if (isElementFound(element)) {
window.clearTimeout(timeout);
resolve(element);
observer.disconnect();
Expand All @@ -77,7 +81,7 @@ function waitForElement(selector, multiple = false, timeoutDuration = 200000) {
});

return null;
});
});
}

/**
Expand All @@ -101,16 +105,18 @@ async function injectFollowButton() {
*/
async function injectInteractionButtons() {
const INJECTED_REPLY_CLASS = "mastodon-simplified-federation-injected-interaction";
const TIMELINE_SELECTOR = "#mastodon .item-list[role='feed'] article[data-id] .status__action-bar button"; // timeline / user profile
const STATUS_NO_REPLIES_SELECTOR = "#mastodon .detailed-status__wrapper .detailed-status__action-bar button"; // status with no replies
const STATUS_WITH_REPLIES_SELECTOR = "#mastodon .status__wrapper .status__action-bar button"; // status with replies
try {
const replyButtons = await waitForElement(
"#mastodon .item-list[role='feed'] article[data-id] .status__action-bar button," + // timeline / user profile
"#mastodon .detailed-status__wrapper .detailed-status__action-bar button," + // status with no replies
"#mastodon .status__wrapper .status__action-bar button", // status with replies
true,
);
const replyButtons = await waitForElement([
TIMELINE_SELECTOR,
STATUS_NO_REPLIES_SELECTOR,
STATUS_WITH_REPLIES_SELECTOR,
].join(","), true,);
replyButtons.forEach((button) => {
try {
if (!button.classList.contains(INJECTED_REPLY_CLASS)){
if (!button.classList.contains(INJECTED_REPLY_CLASS)) {
button.addEventListener("click", onClickInteract);
button.classList.add(INJECTED_REPLY_CLASS);
}
Expand All @@ -121,8 +127,8 @@ async function injectInteractionButtons() {
} catch (error) {
// Interaction buttons failed to appear
}


}

/**
Expand All @@ -142,8 +148,8 @@ function initInjections() {
*/
async function init() {
const MASTODON_INJECTED_CLASS = "mastodon-simplified-federation-injected";
if (document.body.classList.contains(MASTODON_INJECTED_CLASS)){

if (document.body.classList.contains(MASTODON_INJECTED_CLASS)) {
// init has already run
return;
}
Expand Down

0 comments on commit 2c82e7b

Please sign in to comment.