Skip to content

Commit b8134d1

Browse files
committed
Twitter: Use MutationObserver instead of DOM mutation events.
1 parent dec5323 commit b8134d1

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

TwitterA11yFixes.user.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212

1313
var lastFocusedTweet = null;
1414

15-
function onAttrModified(evt) {
16-
var attrName = evt.attrName;
17-
if (attrName != "class")
18-
return;
19-
var target = evt.target;
15+
function onClassModified(target) {
2016
var classes = target.classList;
2117
if (!classes)
2218
return;
@@ -46,10 +42,9 @@ function onAttrModified(evt) {
4642
}
4743
}
4844

49-
function onNodeRemoved(evt) {
45+
function onNodeRemoved(target) {
5046
if (!lastFocusedTweet)
5147
return;
52-
var target = evt.target;
5348
if (target.nodeType != Node.ELEMENT_NODE)
5449
return;
5550
var classes = target.getAttribute("class");
@@ -95,6 +90,17 @@ function onFocus(evt) {
9590
}
9691
}
9792

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"]});
100106
document.addEventListener("focus", onFocus, true);

0 commit comments

Comments
 (0)