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

refactor: extract mastodon-specific code into own file #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
76 changes: 11 additions & 65 deletions src/content_scripts/protoots.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const contributorList = [
"[email protected]",
];

import { mastodon } from "../libs/mastodon.js";
import { fetchPronouns } from "../libs/fetchPronouns";
import {
accountVisibility,
Expand All @@ -18,19 +19,9 @@ import {
notificationVisibility,
statusVisibility,
} from "../libs/settings";
import { warn, log } from "../libs/logging";
import {
findAllDescendants,
hasClasses,
insertAfter,
waitForElement,
waitForElementRemoved,
} from "../libs/domhelpers";
import {
accountNameFromURL,
addTypeAttribute,
normaliseAccountName,
} from "../libs/protootshelpers";
import { warn } from "../libs/logging";
import { insertAfter, waitForElement, waitForElementRemoved } from "../libs/domhelpers";
import { accountNameFromURL, addTypeAttribute, normaliseAccountName } from "../libs/protootshelpers";

//before anything else, check whether we're on a Mastodon page
checkSite();
Expand All @@ -54,57 +45,12 @@ async function checkSite() {
*/
function main() {
// debug('selection for id mastodon', {'result': document.querySelector("#mastodon")})
if (!document.querySelector("#mastodon")) {
warn("Not a Mastodon instance");
return;
}

//All of this is Mastodon specific - factor out into mastodon.js?
log("Mastodon instance, activating Protoots");

//create a global tootObserver to handle all article objects
const tootObserver = new IntersectionObserver((entries) => {
onTootIntersection(entries);
});

// We are tracking navigation changes with the location and a MutationObserver on `document`,
// because the popstate event from the History API is only triggered with the back/forward buttons.
let lastUrl = location.href;
new MutationObserver((mutations) => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
}
if (document.querySelector("#mastodon")) {
mastodon();
} else {

/**
* Checks whether the given n is eligible to have a proplate added
* @param {Node} n
* @returns {Boolean}
*/
function isPronounableElement(n) {
return (
n instanceof HTMLElement &&
((n.nodeName == "ARTICLE" && n.hasAttribute("data-id")) ||
hasClasses(
n,
"detailed-status",
"status",
"conversation",
"account-authorize",
"notification",
"notification__message",
"account",
))
);
}

mutations
.flatMap((m) => Array.from(m.addedNodes).map((m) => findAllDescendants(m)))
.flat()
// .map((n) => console.log("found node: ", n));
.filter(isPronounableElement)
.forEach((a) => addtoTootObserver(a, tootObserver));
}).observe(document, { subtree: true, childList: true });
warn("Not a Supported site");
}
}

/**
Expand All @@ -115,7 +61,7 @@ function main() {
* Once a toot has left the viewport its "protoots-checked" attribute will be removed.
* @param {IntersectionObserverEntry[]} observerentries
*/
function onTootIntersection(observerentries) {
export function onTootIntersection(observerentries) {
for (const observation of observerentries) {
const ArticleElement = observation.target;
if (!observation.isIntersecting) {
Expand Down Expand Up @@ -144,7 +90,7 @@ function onTootIntersection(observerentries) {
* @param {HTMLElement} ActionElement
* @param {IntersectionObserver} tootObserver Observer to add the element to
*/
function addtoTootObserver(ActionElement, tootObserver) {
export function addtoTootObserver(ActionElement, tootObserver) {
// console.log(ActionElement);
if (ActionElement.hasAttribute("protoots-tracked")) return;

Expand Down
51 changes: 51 additions & 0 deletions src/libs/mastodon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { log } from "./logging.js";
import { findAllDescendants, hasClasses } from "./domhelpers.js";
import { addtoTootObserver, onTootIntersection } from "../content_scripts/protoots.js";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely not a fan of circular dependencies. Those methods should be extracted either into the mastodon-specific lib or another one that can be used with future implementations as well.


export function mastodon() {
log("Mastodon instance, activating Protoots");

//create a global tootObserver to handle all article objects
const tootObserver = new IntersectionObserver((entries) => {
onTootIntersection(entries);
});

// We are tracking navigation changes with the location and a MutationObserver on `document`,
// because the popstate event from the History API is only triggered with the back/forward buttons.
let lastUrl = location.href;
new MutationObserver((mutations) => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
}

/**
* Checks whether the given n is eligible to have a proplate added
* @param {Node} n
* @returns {Boolean}
*/
function isPronounableElement(n) {
return (
n instanceof HTMLElement &&
((n.nodeName == "ARTICLE" && n.hasAttribute("data-id")) ||
hasClasses(
n,
"detailed-status",
"status",
"conversation",
"account-authorize",
"notification",
"notification__message",
"account",
))
);
}

mutations
.flatMap((m) => Array.from(m.addedNodes).map((m) => findAllDescendants(m)))
.flat()
// .map((n) => console.log("found node: ", n));
.filter(isPronounableElement)
.forEach((a) => addtoTootObserver(a, tootObserver));
}).observe(document, { subtree: true, childList: true });
}