cohost-tweaks/cohost-dir-auto.js
Juhani Krekelä a6ef71be6d First commit
2024-06-10 19:36:45 +03:00

42 lines
1.1 KiB
JavaScript

// ==UserScript==
// @name Cohost dir="auto"
// @version 1
// @grant none
// @match https://cohost.org/*
// ==/UserScript==
const selectors = [
'textarea', // Post/comment editor fields
'.co-prose', // User-created non-inline content
].join(',');
function applyDirAuto(tree) {
if (tree.matches(selectors)) {
tree.dir = 'auto';
}
for (const post of tree.querySelectorAll(selectors)) {
post.dir = 'auto';
}
}
// Apply dir="auto" to elements that are there already on page load
applyDirAuto(document.documentElement);
// New posts are loaded dynamically using JavaScript, so we need to react to new elements being added
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
applyDirAuto(node);
}
}
}
});
// There is seemingly no good way to refer to the post list, so let's err on the side of firing too often
const contentsContainer = document.getElementById('app');
observer.observe(contentsContainer, {
subtree: true,
childList: true,
});