use overall MutationObserver and avoid re-running content script functions

This commit is contained in:
Huey 2022-12-01 10:43:13 +08:00
parent 8490dbe1a7
commit ff52b95ee4
No known key found for this signature in database
GPG key ID: 54C82E718C137231

View file

@ -101,45 +101,28 @@ 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 replyButtons = await waitForElement(
"#mastodon .item-list[role='feed'] article[data-id] .status__action-bar button," +
"#mastodon .detailed-status__wrapper .detailed-status__action-bar button",
true,
);
replyButtons.forEach((button) => {
try {
if (!button.classList.contains(INJECTED_REPLY_CLASS)){
button.addEventListener("click", onClickInteract);
button.classList.add(INJECTED_REPLY_CLASS);
}
} catch (error) {
// Interaction buttons failed to appear
}
});
}
/**
* Initialise injection for feedElement.
*
* @returns {void}
*/
async function injectFeed() {
const observer = new MutationObserver(() => {
injectInteractionButtons();
});
try { try {
const feedElement = await waitForElement( const replyButtons = await waitForElement(
"#mastodon .item-list[role='feed']", "#mastodon .item-list[role='feed'] article[data-id] .status__action-bar button," + // timeline / user profile
false, "#mastodon .detailed-status__wrapper .detailed-status__action-bar button," + // status with no replies
"#mastodon .status__wrapper .status__action-bar button", // status with replies
true,
); );
replyButtons.forEach((button) => {
observer.observe(feedElement, { try {
childList: true, subtree: true, if (!button.classList.contains(INJECTED_REPLY_CLASS)){
button.addEventListener("click", onClickInteract);
button.classList.add(INJECTED_REPLY_CLASS);
}
} catch (error) {
// Failed to inject interaction buttons
}
}); });
} catch (error){ } catch (error) {
// feedElement not found // Interaction buttons failed to appear
} }
} }
/** /**
@ -147,18 +130,40 @@ async function injectFeed() {
* *
* @returns {void} * @returns {void}
*/ */
function init() { function initInjections() {
injectFollowButton(); injectFollowButton().catch(console.error);
injectInteractionButtons().catch(console.error);
const ogType = document.querySelector("meta[property='og:type']");
// inject only once on detail toots view pages
if (ogType && ogType.getAttribute("content") === "article"){
injectInteractionButtons();
} else {
// otherwise listen to the feed for new posts
injectFeed();
}
} }
init(); /**
* Initialise script and re-run if there are changes.
*
* @returns {void}
*/
async function init() {
const MASTODON_INJECTED_CLASS = "mastodon-simplified-federation-injected";
if (document.body.classList.contains(MASTODON_INJECTED_CLASS)){
// init has already run
return;
}
document.body.classList.add(MASTODON_INJECTED_CLASS);
initInjections();
const observer = new MutationObserver(() => {
initInjections();
});
// monitor only the main column in the Mastodon UI
const mainColumn = await waitForElement(
"#mastodon .ui",
false,
);
observer.observe(mainColumn, {
childList: true,
subtree: true,
});
}
init().catch(console.error);