-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
73 lines (62 loc) · 2.29 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const PUBLIC_JSON_URL = "https://hartomedia.github.io/bsky-emoji-db/config.json";
fetch(PUBLIC_JSON_URL)
.then((response) => response.json())
.then((config) => {
const replacements = config.replacements;
function replaceText(node) {
if (node.nodeType === Node.TEXT_NODE) {
const parent = node.parentElement;
let textContent = node.textContent;
replacements.forEach((replacement) => {
const regex = new RegExp(replacement.replace, "gi");
if (replacement.with.startsWith("http://") || replacement.with.startsWith("https://")) {
if (regex.test(textContent)) {
const updatedHTML = textContent.replace(regex, () => {
return `<img src="${replacement.with}" alt="${replacement.replace}" style="width: 1em; height: 1em;">`;
});
const wrapper = document.createElement("span");
wrapper.innerHTML = updatedHTML;
parent.replaceChild(wrapper, node);
}
} else {
textContent = textContent.replace(regex, replacement.with);
}
});
if (textContent !== node.textContent) {
node.textContent = textContent;
}
} else if (node.nodeType === Node.ELEMENT_NODE) {
for (const child of node.childNodes) {
replaceText(child);
}
}
}
function replaceInSpecialPlaces() {
if (document.title) {
let updatedTitle = document.title;
replacements.forEach((replacement) => {
const regex = new RegExp(replacement.replace, "gi");
updatedTitle = updatedTitle.replace(regex, replacement.with);
});
document.title = updatedTitle;
}
}
if (document.body) {
replaceText(document.body);
}
replaceInSpecialPlaces();
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const addedNode of mutation.addedNodes) {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
replaceText(addedNode);
}
}
}
replaceInSpecialPlaces();
});
observer.observe(document.body, { childList: true, subtree: true });
})
.catch((error) => {
console.error("Failed to load configuration or replace text:", error);
});