-
Notifications
You must be signed in to change notification settings - Fork 4
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
CherryKitten
wants to merge
1
commit into
ItsVipra:main
Choose a base branch
from
CherryKitten:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ const contributorList = [ | |
"[email protected]", | ||
]; | ||
|
||
import { mastodon } from "../libs/mastodon.js"; | ||
import { fetchPronouns } from "../libs/fetchPronouns"; | ||
import { | ||
accountVisibility, | ||
|
@@ -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(); | ||
|
@@ -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"); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -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) { | ||
|
@@ -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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
||
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 }); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.