-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmutation_observer.html
65 lines (53 loc) · 1.86 KB
/
mutation_observer.html
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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
let mutations = []
let pauseCollection = false
function reverseMutation(mutation) {
let table = {
characterData: () => {
mutation.target.textContent = mutation.oldValue
},
attributes: () => {
if (! mutation.oldValue) {
mutation.target.removeAttribute(mutation.attributeName)
return
}
mutation.target.setAttribute(mutation.attributeName, mutation.oldValue)
},
childList: () => {
if (mutation.removedNodes) {
let parent = mutation.target
}
}
}
console.log(mutation);
table[mutation.type]()
}
function reverse() {
pauseCollection = true
while (mutations.length > 0) reverseMutation(mutations.pop())
pauseCollection = false
}
document.addEventListener('DOMContentLoaded', () => {
let observer = new MutationObserver(imutations => {
if (pauseCollection) return
imutations.forEach(i => mutations.push(i))
console.log(imutations);
})
observer.observe(document.body, { characterData: true, attributeOldValue: true, characterDataOldValue: true, childList: true, subtree: true, attributes: true })
})
</script>
</head>
<body>
<div>
<h1 foo="bar">yo</h1>
<h1>there</h1>
<button>this is cool</button>
</div>
</body>
</html>