fix: injection where new tab is not the active tab

This commit is contained in:
Huey 2022-12-10 10:45:36 +08:00
parent ff52b95ee4
commit 379f8f3583
No known key found for this signature in database
GPG key ID: 54C82E718C137231
2 changed files with 22 additions and 16 deletions

View file

@ -155,7 +155,7 @@ async function onTabUpdate(tabId, changeInfo) {
const ownMastodon = await AddonSettings.get("ownMastodon"); const ownMastodon = await AddonSettings.get("ownMastodon");
const currentURL = new URL(changeInfo.url); const currentURL = new URL(changeInfo.url);
if (ownMastodon.server !== currentURL.hostname){ if (ownMastodon.server !== currentURL.hostname){
browser.tabs.executeScript({ browser.tabs.executeScript(tabId, {
file: "/content_script/mastodonInject.js", file: "/content_script/mastodonInject.js",
}); });
} }

View file

@ -24,10 +24,14 @@ function onClickInteract(event) {
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
const articleElement = event.target.closest("article[data-id]"); 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 = ( const tootId = (
articleElement === null articleElement === null
? window.location.pathname.split("/").slice(-1)[0] ? window.location.pathname.split("/").slice(-1)[0]
: articleElement.getAttribute("data-id") : getId()
); );
// activate AutoRemoteFollow // activate AutoRemoteFollow
window.open(`/interact/${tootId}`, "_blank"); window.open(`/interact/${tootId}`, "_blank");
@ -57,14 +61,14 @@ function waitForElement(selector, multiple = false, timeoutDuration = 200000) {
}, timeoutDuration); }, timeoutDuration);
const element = getElement(); const element = getElement();
if (isElementFound(element)){ if (isElementFound(element)) {
window.clearTimeout(timeout); window.clearTimeout(timeout);
return resolve(element); return resolve(element);
} }
const observer = new MutationObserver(() => { const observer = new MutationObserver(() => {
const element = getElement(); const element = getElement();
if (isElementFound(element)){ if (isElementFound(element)) {
window.clearTimeout(timeout); window.clearTimeout(timeout);
resolve(element); resolve(element);
observer.disconnect(); observer.disconnect();
@ -77,7 +81,7 @@ function waitForElement(selector, multiple = false, timeoutDuration = 200000) {
}); });
return null; return null;
}); });
} }
/** /**
@ -101,16 +105,18 @@ async function injectFollowButton() {
*/ */
async function injectInteractionButtons() { async function injectInteractionButtons() {
const INJECTED_REPLY_CLASS = "mastodon-simplified-federation-injected-interaction"; 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 { try {
const replyButtons = await waitForElement( const replyButtons = await waitForElement([
"#mastodon .item-list[role='feed'] article[data-id] .status__action-bar button," + // timeline / user profile TIMELINE_SELECTOR,
"#mastodon .detailed-status__wrapper .detailed-status__action-bar button," + // status with no replies STATUS_NO_REPLIES_SELECTOR,
"#mastodon .status__wrapper .status__action-bar button", // status with replies STATUS_WITH_REPLIES_SELECTOR,
true, ].join(","), true,);
);
replyButtons.forEach((button) => { replyButtons.forEach((button) => {
try { try {
if (!button.classList.contains(INJECTED_REPLY_CLASS)){ if (!button.classList.contains(INJECTED_REPLY_CLASS)) {
button.addEventListener("click", onClickInteract); button.addEventListener("click", onClickInteract);
button.classList.add(INJECTED_REPLY_CLASS); button.classList.add(INJECTED_REPLY_CLASS);
} }
@ -121,8 +127,8 @@ async function injectInteractionButtons() {
} catch (error) { } catch (error) {
// Interaction buttons failed to appear // Interaction buttons failed to appear
} }
} }
/** /**
@ -142,8 +148,8 @@ function initInjections() {
*/ */
async function init() { async function init() {
const MASTODON_INJECTED_CLASS = "mastodon-simplified-federation-injected"; 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 // init has already run
return; return;
} }