-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathWhatsAppA11yFixes.user.js
More file actions
270 lines (246 loc) · 7.63 KB
/
WhatsAppA11yFixes.user.js
File metadata and controls
270 lines (246 loc) · 7.63 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// ==UserScript==
// @name WhatsApp Web Accessibility Fixes
// @namespace http://axSgrease.nvaccess.org/
// @description Improves the accessibility of WhatsApp Web.
// @author James Teh <jamie@jantrid.net>
// @copyright 2019-2025 Mozilla Corporation, Derek Riemer, James Teh
// @license Mozilla Public License version 2.0
// @version 2025.2
// @include https://web.whatsapp.com/
// ==/UserScript==
/*** Functions for common tweaks. ***/
/**
* Adds text to the given live region, and clears it a second later so it's no
* longer perceivable.
* @param {string} regionid an id of a region.
*/
function announce(text, regionId) {
getLiveRegion(regionId)
.then((region) => {
region.innerText = text;
setTimeout(() => {
region.innerText = '';
}, 1000);
});
}
/**
* create or fetch a live region that can be used with announce(). Returns a promise with the region.
* @param {string} id the name of the new live region. This is an html id.
* @return {!Promise<HTMLElement>} a div that contains the live region. This can typically be ignored, this exists to aid in chaining creation of non-existant regions.
*/
function getLiveRegion(id) {
const updatePromise = new Promise((resolve, reject) => {
if (!id) {
reject('Need a valid id!');
return;
}
const existingRegion = document.getElementById(id);
if (existingRegion) {
resolve(existingRegion);
return;
}
const region = document.createElement('div');
region.id = id;
region.setAttribute('aria-live', 'polite');
region.setAttribute('aria-atomic', 'true');
region.style.position = 'absolute';
region.style.width = '50px';
region.style.height = '50px';
region.style.opasity = 0;
document.body.appendChild(region);
// we need to delay a little to get the new region to actually read contents.
// A11y APIs probably don't treat the relevant changes as "additions" until
//an annimation frame has passed. It may, in reality be more like 2-4
// annimation frames, so delay 134 ms to be safe.
setTimeout(() => {
resolve(region);
}, 134);
});
return updatePromise;
}
function makeHeading(el, level) {
el.setAttribute("role", "heading");
el.setAttribute("aria-level", level);
}
function makeRegion(el, label) {
el.setAttribute("role", "region");
el.setAttribute("aria-label", label);
}
function makeButton(el, label) {
el.setAttribute("role", "button");
if (label) {
el.setAttribute("aria-label", label);
}
}
function makePresentational(el) {
el.setAttribute("role", "presentation");
}
function setLabel(el, label) {
el.setAttribute("aria-label", label);
}
function makeHidden(el) {
el.setAttribute("aria-hidden", "true");
}
function setExpanded(el, expanded) {
el.setAttribute("aria-expanded", expanded ? "true" : "false");
}
var idCounter = 0;
// Get a node's id. If it doesn't have one, make and set one first.
function setAriaIdIfNecessary(elem) {
if (!elem.id) {
elem.setAttribute("id", "axsg-" + idCounter++);
}
return elem.id;
}
function makeElementOwn(parentElement, listOfNodes) {
ids = [];
for (let node of listOfNodes) {
ids.push(setAriaIdIfNecessary(node));
}
parentElement.setAttribute("aria-owns", ids.join(" "));
}
// Focus something even if it wasn't made focusable by the author.
function forceFocus(el) {
let focusable = el.hasAttribute("tabindex");
if (focusable) {
el.focus();
return;
}
el.setAttribute("tabindex", "-1");
el.focus();
}
/*** Code to apply the tweaks when appropriate. ***/
function applyTweak(el, tweak) {
if (Array.isArray(tweak.tweak)) {
let [func, ...args] = tweak.tweak;
func(el, ...args);
} else {
tweak.tweak(el);
}
}
function applyTweaks(root, tweaks, checkRoot, forAttrChange = false) {
for (let tweak of tweaks) {
if (!forAttrChange || tweak.whenAttrChangedOnAncestor !== false) {
for (let el of root.querySelectorAll(tweak.selector)) {
try {
applyTweak(el, tweak);
} catch (e) {
console.log("Exception while applying tweak for '" + tweak.selector + "': " + e);
}
}
}
if (checkRoot && root.matches(tweak.selector)) {
try {
applyTweak(root, tweak);
} catch (e) {
console.log("Exception while applying tweak for '" + tweak.selector + "': " + e);
}
}
}
}
let observer = new MutationObserver(function (mutations) {
for (let mutation of mutations) {
try {
if (mutation.type === "childList") {
for (let node of mutation.addedNodes) {
if (node.nodeType != Node.ELEMENT_NODE) {
continue;
}
applyTweaks(node, DYNAMIC_TWEAKS, true);
}
} else if (mutation.type === "attributes") {
applyTweaks(mutation.target, DYNAMIC_TWEAKS, true, true);
}
} catch (e) {
// Catch exceptions for individual mutations so other mutations are still handled.
console.log("Exception while handling mutation: " + e);
}
}
});
function init() {
applyTweaks(document, LOAD_TWEAKS, false);
applyTweaks(document, DYNAMIC_TWEAKS, false);
options = { childList: true, subtree: true };
if (DYNAMIC_TWEAK_ATTRIBS.length > 0) {
options.attributes = true;
options.attributeFilter = DYNAMIC_TWEAK_ATTRIBS;
}
observer.observe(document, options);
}
/*** Define the actual tweaks. ***/
// Tweaks that only need to be applied on load.
const LOAD_TWEAKS = [
];
// Attributes that should be watched for changes and cause dynamic tweaks to be
// applied.
const DYNAMIC_TWEAK_ATTRIBS = [];
// Tweaks that must be applied whenever an element is added/changed.
const DYNAMIC_TWEAKS = [
{selector: '[role=dialog]',
tweak: el => {
if (el.querySelector('[role=button][aria-pressed] img')) {
// This is the reaction picker. It contains buttons which wilh switch to
// browse mode, but we want to use WhatsApp's own arrow key navigation
// here. Therefore, use role="application". Menu would be more appropriate,
// but that's tricky because WhatsApp uses aria-pressed on the buttons.
el.role = "application";
}
}
},
];
/** add your specific initialization here, so that if you ever update the framework from new skeleton your inits are not overridden. */
function userInit(){
document.addEventListener("keydown", event => {
// Make alt+1 focus the first chat in the Chats list.
if (event.altKey && event.key == "1") {
const firstChat = document.querySelector('[role=grid] [role=gridcell] [aria-selected]');
if (firstChat) {
firstChat.focus();
}
return;
}
// Make alt+2 focus the last message in the active chat.
if (event.altKey && event.key == "2") {
const messages = document.querySelectorAll(".focusable-list-item");
if (messages.length > 0) {
const lastMessage = messages[messages.length - 1];
if (!lastMessage.hasAttribute("tabindex")) {
// Messages don't initially have the tabindex attribute, but they gain it
// once you navigate to them with the keyboard.
lastMessage.setAttribute("tabindex", 0);
}
lastMessage.focus();
}
return;
}
});
document.addEventListener("contextmenu", event => {
// The context menu key doesn't work properly for chat and message items.
// Fix that.
const focus = document.activeElement;
const button = focus.querySelector(
focus.classList.contains("focusable-list-item") ?
// Message
'[role=button][aria-expanded]' :
// Chat
'button'
);
if (button) {
event.preventDefault();
event.stopPropagation();
button.click();
}
}, { capture: true });
document.addEventListener("focusout", event => {
// When the tab loses focus, WhatsApp forces focus into the message list,
// regardless of where it was before. Override that absurdity.
setTimeout(() => {
if (!document.hasFocus() && document.activeElement != event.target) {
event.target.focus();
}
}, 50);
});
}
/*** Lights, camera, action! ***/
init();
userInit();