cohost/userscripts/name-color.js

74 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2024-07-10 15:41:21 -04:00
// ==UserScript==
2024-07-10 17:15:21 -04:00
// @name name-color: cohost edition!
2024-07-10 15:41:21 -04:00
// @namespace https://terezi.pyrope.net
2024-07-10 19:50:07 -04:00
// @version 0.2
2024-07-10 17:15:21 -04:00
// @description colors names based on `name-color:` statements in profiles. there are no other editions
2024-07-10 15:41:21 -04:00
// @author mehbark
// @match https://*.cohost.org/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=cohost.org
// @grant none
// ==/UserScript==
2024-07-10 17:15:21 -04:00
const mentionee = elem => elem.href.split("/").at(-1);
2024-07-10 15:41:21 -04:00
2024-07-10 17:15:21 -04:00
function doit() {
"use strict";
// not just literal mentions btw
const mentions = [
...document.querySelectorAll("a[href^='https://cohost.org/']"),
].filter(
a =>
!(
a.href.includes("/rc/") ||
// sorry, @post
a.href.includes("/post/") ||
// sorry, @tagged
a.href.includes("/tagged/") ||
// sorry, @ask
a.href.endsWith("/ask") ||
a.href.endsWith("/")
)
);
const names = [...new Set(mentions.map(mentionee))];
2024-07-10 19:50:07 -04:00
names.sort();
2024-07-10 17:15:21 -04:00
const requester = document
.querySelector("header")
.querySelector("span")
.innerText.slice(1);
const url = new URL("https://pyrope.net/name-color/style.css");
url.searchParams.append("requester", requester);
2024-07-10 19:50:07 -04:00
2024-07-10 17:15:21 -04:00
for (const name of names) {
url.searchParams.append("name", name);
}
2024-07-10 19:50:07 -04:00
let style = document.head.querySelector(".name-color-stylesheet");
if (style) {
const style2 = document.createElement("link");
style2.rel = "stylesheet";
style2.href = url.toString();
style2.onload = () => {
document.head.querySelectorAll(".name-color-stylesheet").forEach(s => s.remove());
style2.className = "name-color-stylesheet";
};
document.head.appendChild(style2);
} else {
2024-07-10 17:15:21 -04:00
style = document.createElement("link");
2024-07-10 19:50:07 -04:00
style.className = "name-color-stylesheet";
2024-07-10 17:15:21 -04:00
style.rel = "stylesheet";
2024-07-10 19:50:07 -04:00
style.href = url.toString();
2024-07-10 17:15:21 -04:00
document.head.appendChild(style);
}
}
2024-07-10 19:50:07 -04:00
new MutationObserver(doit).observe(document.body, {
2024-07-10 17:15:21 -04:00
subtree: true,
childList: true,
});
doit();