-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarklet.htm
196 lines (170 loc) · 5.65 KB
/
bookmarklet.htm
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
<!doctype html>
<html lang="en-gb">
<head>
<meta charset="utf-8" />
<title>Accessibility linter bookmarklet generator</title>
<style>
html {
font: 1em/1.5 sans-serif;
background: #ddd;
}
body {
max-width: 800px;
margin: 1em auto;
background: #fff;
padding: 1em;
}
label {
display: block;
}
input[type=text] {
width: 100%;
box-sizing: border-box;
padding: .2em;
}
table {
width: 100%;
border-collapse: collapse;
}
caption {
font-weight: bold;
text-align: left;
}
tr {
text-align: left;
}
#bookmark {
text-decoration: none;
display: block;
white-space: pre;
overflow-y: auto;
color: #666;
font-family: monospace;
}
#bookmark:empty {
display: none;
}
</style>
</head>
<body>
<h1>Accessibility linter bookmarklet generator</h1>
<form id="form" autocomplete="off">
<p>
<label for="enable-all">
<input type="checkbox" id="enable-all" checked />
Enable all rules by default
</label>
</p>
<p>
<label for="whitelist">Global whitelist selector</label>
<input type="text" id="whitelist" placeholder="Selector for elements to whitelist" />
</p>
<table>
<caption>Customise rules</caption>
<thead>
<tr>
<th>Rule
<th>Activation
<th>Whitelist
<tbody id="table-body">
<template id="row-template">
<tr role="group">
<td><input type="hidden" name="rule-name" />
<td>
<select name="rule-state" aria-label="Activation">
<option value="">Default</option>
<option value="error">Error</option>
<option value="warn">Warn</option>
<option value="off">Off</option>
</select>
<td>
<input type="text" name="rule-whitelist" placeholder="Selector for elements to ignore" aria-label="Whitelist selector" />
</template>
<tfoot>
<tr>
<td colspan="3" role="group" aria-label="Add rules">
<select id="all-rules" aria-label="Select rule">
<option value="">Select a rule...</option>
</select>
<button type="button" id="add-rule">Add rule</button>
<button type="button" id="add-all-rules">Add all rules</button>
</table>
<p><input type="submit" value="Generate bookmarklet" />
<p><a id="bookmark"></a>
</form>
<script src="build/umd.min.js"></script>
<script>
const $ = selector => document.querySelector(selector);
Array.from(AccessibilityLinter.rules.keys())
.sort()
.forEach(key => {
const option = document.createElement('option');
option.value = key;
option.innerText = key;
$('#all-rules').appendChild(option);
});
$('#add-rule').addEventListener('click', e => {
const name = $('#all-rules').value;
if (!name || $(`[aria-label="Rule ${name}"]`)) {
return;
}
$(`#all-rules > option[value="${name}"]`).disabled = true;
const row = document.importNode($('#row-template').content.querySelector('tr'), true);
row.setAttribute('aria-label', `Rule ${name}`);
row.querySelector('[name="rule-name"]').value = name;
row.querySelector('td').appendChild(document.createTextNode(name));
$('#table-body').appendChild(row);
});
$('#add-all-rules').addEventListener('click', e => {
Array.from(AccessibilityLinter.rules.keys())
.forEach(key => {
$('#all-rules').value = key;
$('#add-rule').click();
});
e.target.focus();
});
$('#bookmark').addEventListener('click', e => {
e.preventDefault();
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(e.target);
selection.removeAllRanges();
selection.addRange(range);
});
$('#form').addEventListener('submit', e => {
e.preventDefault();
let settings = {
whitelist: $('#whitelist').value.trim() || undefined,
defaultOff: !$('#enable-all').checked || undefined,
ruleSettings: {},
};
Array.from(e.target.querySelectorAll('[name="rule-name"]'))
.map((item, i) => {
const state = e.target.querySelectorAll('[name="rule-state"]')[i].value;
settings.ruleSettings[item.value] = {
whitelist: e.target.querySelectorAll('[name="rule-whitelist"]')[i].value.trim() || undefined,
enabled: state !== 'off',
type: state !== 'off' && state || undefined,
};
});
if (Object.keys(settings.ruleSettings).length === 0) {
settings.ruleSettings = undefined;
}
const url = new URL('build/linter.min.js', location.href);
const bookmarklet = `javascript:(function() {
if (window.accessibilityLinter) {
console.log('Linter is already on page');
return;
}
const script = document.createElement('script');
script.src = ${JSON.stringify(url.href)};
script.textContent = JSON.stringify(${JSON.stringify(settings)});
document.head.appendChild(script);
}());`
const a = $('#bookmark');
a.href = bookmarklet.replace(/[\n\s]+/g, ' ');
a.innerText = bookmarklet;
});
</script>
</body>
</html>