mutation_observer.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script>
  8. let mutations = []
  9. let pauseCollection = false
  10. function reverseMutation(mutation) {
  11. let table = {
  12. characterData: () => {
  13. mutation.target.textContent = mutation.oldValue
  14. },
  15. attributes: () => {
  16. if (! mutation.oldValue) {
  17. mutation.target.removeAttribute(mutation.attributeName)
  18. return
  19. }
  20. mutation.target.setAttribute(mutation.attributeName, mutation.oldValue)
  21. },
  22. childList: () => {
  23. if (mutation.removedNodes) {
  24. let parent = mutation.target
  25. }
  26. }
  27. }
  28. console.log(mutation);
  29. table[mutation.type]()
  30. }
  31. function reverse() {
  32. pauseCollection = true
  33. while (mutations.length > 0) reverseMutation(mutations.pop())
  34. pauseCollection = false
  35. }
  36. document.addEventListener('DOMContentLoaded', () => {
  37. let observer = new MutationObserver(imutations => {
  38. if (pauseCollection) return
  39. imutations.forEach(i => mutations.push(i))
  40. console.log(imutations);
  41. })
  42. observer.observe(document.body, { characterData: true, attributeOldValue: true, characterDataOldValue: true, childList: true, subtree: true, attributes: true })
  43. })
  44. </script>
  45. </head>
  46. <body>
  47. <div>
  48. <h1 foo="bar">yo</h1>
  49. <h1>there</h1>
  50. <button>this is cool</button>
  51. </div>
  52. </body>
  53. </html>