Skip to content

Commit f227e68

Browse files
committed
Google Keep: Fix moving focus when search is dismissed.
Previously, this code was triggering when it shouldn't such as when editing a new note, causing focus to get spuriously lost. Now, we use the visibility of the Clear search button to figure out whether search was dismissed. This required adding a whenAttrChangedOnAncestor option to prevent a tweak from being applied when an attribute changes on an ancestor. Otherwise, every time the style attribute changed on an ancestor, this code would trigger and throw focus.
1 parent f4021df commit f227e68

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

GoogleKeepA11yFixes.user.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// @namespace http://axSgrease.nvaccess.org/
44
// @description Improves the accessibility of Google Keep.
55
// @author James Teh <jamie@jantrid.net>
6-
// @copyright 2021 James Teh, Mozilla Corporation, Derek Riemer
6+
// @copyright 2022 James Teh, Mozilla Corporation, Derek Riemer
77
// @license Mozilla Public License version 2.0
8-
// @version 2021.1
8+
// @version 2022.1
99
// @include https://keep.google.com/*
1010
// ==/UserScript==
1111

@@ -83,13 +83,15 @@ function applyTweak(el, tweak) {
8383
}
8484
}
8585

86-
function applyTweaks(root, tweaks, checkRoot) {
86+
function applyTweaks(root, tweaks, checkRoot, forAttrChange=false) {
8787
for (let tweak of tweaks) {
88-
for (let el of root.querySelectorAll(tweak.selector)) {
89-
try {
90-
applyTweak(el, tweak);
91-
} catch (e) {
92-
console.log("Exception while applying tweak for '" + tweak.selector + "': " + e);
88+
if (!forAttrChange || tweak.whenAttrChangedOnAncestor !== false) {
89+
for (let el of root.querySelectorAll(tweak.selector)) {
90+
try {
91+
applyTweak(el, tweak);
92+
} catch (e) {
93+
console.log("Exception while applying tweak for '" + tweak.selector + "': " + e);
94+
}
9395
}
9496
}
9597
if (checkRoot && root.matches(tweak.selector)) {
@@ -113,7 +115,7 @@ let observer = new MutationObserver(function(mutations) {
113115
applyTweaks(node, DYNAMIC_TWEAKS, true);
114116
}
115117
} else if (mutation.type === "attributes") {
116-
applyTweaks(mutation.target, DYNAMIC_TWEAKS, true);
118+
applyTweaks(mutation.target, DYNAMIC_TWEAKS, true, true);
117119
}
118120
} catch (e) {
119121
// Catch exceptions for individual mutations so other mutations are still handled.
@@ -189,11 +191,13 @@ const DYNAMIC_TWEAKS = [
189191
el.setAttribute("aria-labelledby", setAriaIdIfNecessary(content));
190192
}
191193
}},
192-
// When the notes list reappears after dismissing search, move focus away from
193-
// the search box so keyboard shortcuts work without having to tab.
194-
{selector: '.h1U9Be-xhiy4',
194+
// When the Clear search button disappears after dismissing search, move focus
195+
// away from the search box so keyboard shortcuts work without having to
196+
// tab.
197+
{selector: '.gb_nf',
198+
whenAttrChangedOnAncestor: false,
195199
tweak: el => {
196-
if (el.style.display != "none") {
200+
if (el.style.visibility == "hidden") {
197201
document.activeElement.blur();
198202
}
199203
}},

0 commit comments

Comments
 (0)