|
12 | 12 |
|
13 | 13 | var lastFocusedTweet = null; |
14 | 14 |
|
15 | | -function onAttrModified(evt) { |
16 | | - var attrName = evt.attrName; |
17 | | - if (attrName != "class") |
18 | | - return; |
19 | | - var target = evt.target; |
| 15 | +function onClassModified(target) { |
20 | 16 | var classes = target.classList; |
21 | 17 | if (!classes) |
22 | 18 | return; |
@@ -46,10 +42,9 @@ function onAttrModified(evt) { |
46 | 42 | } |
47 | 43 | } |
48 | 44 |
|
49 | | -function onNodeRemoved(evt) { |
| 45 | +function onNodeRemoved(target) { |
50 | 46 | if (!lastFocusedTweet) |
51 | 47 | return; |
52 | | - var target = evt.target; |
53 | 48 | if (target.nodeType != Node.ELEMENT_NODE) |
54 | 49 | return; |
55 | 50 | var classes = target.getAttribute("class"); |
@@ -95,6 +90,17 @@ function onFocus(evt) { |
95 | 90 | } |
96 | 91 | } |
97 | 92 |
|
98 | | -document.addEventListener("DOMAttrModified", onAttrModified, false); |
99 | | -document.addEventListener("DOMNodeRemoved", onNodeRemoved, false); |
| 93 | +var observer = new MutationObserver(function(mutations) { |
| 94 | + mutations.forEach(function(mutation) { |
| 95 | + if (mutation.type === "childList") { |
| 96 | + for (var i = 0; i < mutation.removedNodes.length; ++i) |
| 97 | + onNodeRemoved(mutation.removedNodes[i]); |
| 98 | + } else if (mutation.type === "attributes") { |
| 99 | + // This observer only watches the class attribute. |
| 100 | + onClassModified(mutation.target); |
| 101 | + } |
| 102 | + }); |
| 103 | +}); |
| 104 | +observer.observe(document, {childList: true, attributes: true, |
| 105 | + subtree: true, attributeFilter: ["class"]}); |
100 | 106 | document.addEventListener("focus", onFocus, true); |
0 commit comments