-
Notifications
You must be signed in to change notification settings - Fork 1
/
greentext.js
66 lines (58 loc) · 2.6 KB
/
greentext.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
function searcher(node) {
if (!node) return;
if ((node.previousSibling == null ||
node.previousSibling.tagName == 'DIV' ||
node.previousSibling.tagName == 'BR') &&
node.nodeType == 3 && greentextRegex.test(node.textContent) &&
!node.parentNode.isContentEditable &&
node.parentNode.tagName != 'A' &&
node.parentNode.tagName != 'S' &&
node.parentNode.className != 'greentext' &&
node.parentNode.className != 'quote' ) { // legit greentext on 4chan
greenTextify(node);
} else if(node.hasChildNodes()){
for (var i = 0; i < node.childNodes.length; i++) {
searcher(node.childNodes[i]);
}
}
}
function greenTextify(node) {
var greenSpan;
if (node.previousSibling != null && node.previousSibling.className == "greentext") { // Last node was already greentexted
greenSpan = node.previousSibling;
} else {
greenSpan = document.createElement('span'); // Create greentext node
greenSpan.setAttribute('class', 'greentext'); // Create make span green
node.parentNode.insertBefore(greenSpan, node); // Insert span into position
}
greenSpan.appendChild(node);
// DON'T USE SPECIAL G+ WORKAROUND ANYMORE
/* if (greenSpan.nextSibling != null) { // nextSibling exists
// Special G+ Workaround
if (greenSpan.nextSibling.className == "proflinkWrapper" && greenSpan.nextSibling.nextSibling.nodeType == 3) {
greenTextify(greenSpan.nextSibling.nextSibling, 0); // nextSibling is profile link wrapper, continue greentext after it
}
} */
if (greenSpan.nextSibling != null && // fuck order of operations holy shit
(greenSpan.nextSibling.nodeType != 1 ||
greenSpan.nextSibling.tagName != 'DIV' &&
greenSpan.nextSibling.tagName != 'BR')) {
greenTextify(greenSpan.nextSibling);
}
}
var greentextRegex = /^\s*(?:>|>)(?:[^<.>_]|>+[^>\s]+)/i;
var obs = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
searcher(mutation.target);
});
});
window.addEventListener('load', function() {
searcher(document);
obs.observe(document, {attributes: false, childList: true, characterData: true, subtree: true});
});
// function onModification(event) {
// searcher(event.relatedNode);
// }
// document.addEventListener("DOMNodeInserted", onModification, false);
// document.addEventListener("DOMCharacterDataModified", onModification, false);
// setInterval("searcher(document.getElementsByTagName('body') [0]);", 1000);