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 currentURL = new URL(changeInfo.url);
if (ownMastodon.server !== currentURL.hostname){
browser.tabs.executeScript({
browser.tabs.executeScript(tabId, {
file: "/content_script/mastodonInject.js",
});
}

View file

@ -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");
@ -101,13 +105,15 @@ 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)) {