-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver.js
116 lines (94 loc) · 3.41 KB
/
observer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Acrylic League of legends client theme */
/* Adjusted by egirlcatnip from Sarah's acrylic theme */
/* Version: 1.5 */
/* credits: @aimslut(discord), @egirlcatnip(discord), @unproductive(discord) */
/* this source: https://github.com/PrincessAkira/league-launcher-theme/tree/main/Acrylical */
/* Sarah's source: https://github.com/PrincessAkira/league-launcher-theme/tree/main/Acrylical */
/* Code for the shadow-dom obeserver-fixer-thing-a-do by @unproductive */
let observerObject
let observerCreationCallbacks = { idCallbacks: {}, tagCallbacks: {}, classCallbacks: {} }
let observerDeletionCallbacks = { idCallbacks: {}, tagCallbacks: {}, classCallbacks: {} }
function observerSubscribeToElement(target, callback, callbackList) {
function push(target, callback, observerMap) {
let v = observerMap[target]
if (v === undefined) {
observerMap[target] = [callback]
} else {
v.push(callback)
}
}
if (target[0] === '.') {
push(target.slice(1), callback, callbackList.classCallbacks)
} else if (target[0] === '#') {
push(target.slice(1), callback, callbackList.idCallbacks)
} else {
push(target, callback, callbackList.tagCallbacks)
}
}
export function subscribeToElementCreation(target, callback) {
observerSubscribeToElement(target, callback, observerCreationCallbacks)
}
export function subscribeToElementDeletion(target, callback) {
observerSubscribeToElement(target, callback, observerDeletionCallbacks)
}
function observerHandleElement(element, isNew, callbacks) {
//if (isNew) {
// console.log(`new element: <${element.tagName} id="${element.id}" class="${element.className}">`)
//
if (element.id != "") {
const cb = callbacks.idCallbacks[element.id]
if (cb != undefined) {
for (const obj of cb) {
obj(element)
}
}
}
const tagLowered = element.tagName.toLowerCase()
const cb = callbacks.tagCallbacks[tagLowered]
if (cb != undefined) {
for (const obj of cb) {
obj(element)
}
}
const classList = element.classList
if (classList) {
for (const nodeClass of classList) {
const classLowered = nodeClass.toLowerCase()
const cb = callbacks.classCallbacks[classLowered]
if (cb != undefined) {
for (const obj of cb) {
obj(element)
}
}
}
}
if (isNew && element.shadowRoot != null) {
//console.warn('observing shadowroot for element ' + element.tagName)
const handleChildren = (element, isNew, callbacks) => {
observerHandleElement(element, isNew, callbacks)
for (const child of element.children) {
handleChildren(child, isNew, callbacks)
}
}
for (const child of element.shadowRoot.children) {
handleChildren(child, isNew, callbacks)
}
observerObject.observe(element.shadowRoot, { attributes: false, childList: true, subtree: true })
}
}
function observerCallback(mutationsList) {
for (const mutation of mutationsList) {
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
observerHandleElement(node, true, observerCreationCallbacks)
}
}
for (const node of mutation.removedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
observerHandleElement(node, false, observerDeletionCallbacks)
}
}
}
}
observerObject = new MutationObserver(observerCallback)
observerObject.observe(document, { attributes: false, childList: true, subtree: true })