forked from jcsteh/axSGrease
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchfoxA11yFixes.user.js
More file actions
139 lines (122 loc) · 3.87 KB
/
SearchfoxA11yFixes.user.js
File metadata and controls
139 lines (122 loc) · 3.87 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
// ==UserScript==
// @name Searchfox Accessibility Fixes
// @namespace http://axSgrease.nvaccess.org/
// @description Improves the accessibility of Searchfox.
// @author James Teh <jteh@mozilla.com>
// @copyright 2018 Mozilla Corporation
// @license Mozilla Public License version 2.0
// @version 2018.1
// @grant GM_log
// @include https://searchfox.org/*
// ==/UserScript==
/*** Functions for common tweaks. ***/
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");
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");
}
/*** 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) {
for (let tweak of tweaks) {
for (let el of root.querySelectorAll(tweak.selector)) {
applyTweak(el, tweak);
}
if (checkRoot && root.matches(tweak.selector)) {
applyTweak(root, tweak);
}
}
}
function init() {
applyTweaks(document, LOAD_TWEAKS, false);
applyTweaks(document, DYNAMIC_TWEAKS, false);
}
/*
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") {
if (mutation.attributeName == "class") {
applyTweaks(mutation.target, DYNAMIC_TWEAKS, true);
}
}
} catch (e) {
// Catch exceptions for individual mutations so other mutations are still handled.
GM_log("Exception while handling mutation: " + e);
}
}
});
observer.observe(document, {childList: true, attributes: true,
subtree: true, attributeFilter: ["class"]});
*/
/*** Define the actual tweaks. ***/
// Tweaks that only need to be applied on load.
const LOAD_TWEAKS = [
// We'll fake our own table below, since this HTML table is only 2 x 2 and
// browsers get confused when you mix HTML and ARIA tables.
{selector: '#file, #file tbody, #file tbody tr, #file td.code',
tweak: makePresentational},
// We don't really care about the header row. It's visually hidden anyway.
// We also don't need the line numbers cell. We'll aria-owns each line number
// inside it later.
{selector: '#file thead, #line-numbers',
tweak: makeHidden},
{selector: '#file pre',
tweak: el => el.setAttribute("role", "table")},
{selector: '.line-number',
tweak: el => el.setAttribute("role", "cell")},
// Expose the blame strip for each line number.
{selector: '.blame-strip',
tweak: [makeButton, "blame"]},
{selector: 'code',
tweak: code => {
code.setAttribute("role", "cell");
// We need a container to be our row.
let row = document.createElement("span");
row.setAttribute("role", "row");
// code.id is "line-nnn".
let lineNum = code.id.substring(5); // Strip "line-" prefix.
// The row will have two cells: the line number and the line of code.
// We make them children of the row using aria-owns.
row.setAttribute("aria-owns", "l" + lineNum + " " + code.id);
code.parentNode.insertBefore(row, code);
}},
]
// Tweaks that must be applied whenever a node is added/changed.
const DYNAMIC_TWEAKS = [
]
/*** Lights, camera, action! ***/
init();