-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
197 lines (174 loc) · 5.41 KB
/
content.js
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
/* content.js */
var dataAttribName = 'data-skipto';
var debug = false;
/*
** Connect to popup script and set up listener/handler
*/
var popupPort = chrome.runtime.connect({ name: 'content' });
popupPort.onMessage.addListener(messageHandler);
function messageHandler (message) {
switch (message.id) {
case 'storage':
if (debug) console.log(`content: 'storage' message`);
processPage(message.options);
break;
case 'skipto':
skipToContent(message.dataId);
break;
case 'cleanup':
removeDataAttributes();
break;
}
}
/*
** getTargetElement: Find an element that is focusable based on the
** aria role of the landmark (indicated by dataId prefix).
*/
function getTargetElement (dataId, element) {
let selectorsArray = ['h1', 'a[href]', 'h2', 'h3', 'section', 'article', 'h4', 'h5', 'h6', 'p', 'li'];
let isSearch = dataId.startsWith('s-');
let isNav = dataId.startsWith('n-');
if (isSearch) {
return element.querySelector('input');
}
if (isNav) {
let elements = element.querySelectorAll('a');
for (const elem of elements) {
if (isVisible(elem)) return elem;
}
return element;
}
// Must be main landmark
for (const selector of selectorsArray) {
let elem = element.querySelector(selector);
if (elem && isVisible(elem)) {
if (debug) console.log(`target for main: ${elem.tagName.toLowerCase()}`);
return elem;
}
}
return element;
}
/*
** Perform the action specified by activated menu item
*/
function skipToContent (dataId) {
let selector = `[${dataAttribName}="${dataId}"]`;
let isHeading = dataId.startsWith('h-');
let target = null;
let element = document.querySelector(selector);
if (element) {
target = isHeading ? element : getTargetElement(dataId, element);
if (target && isVisible(target)) {
let options = { behavior: 'smooth', block: 'center' };
target.setAttribute('tabindex', '-1');
target.focus();
target.scrollIntoView(options);
}
else {
let status = (target === null) ? 'null' : !isVisible(target) ? 'not visible' : 'unknown';
if (debug) console.log(`target: ${target.tagName.toLowerCase()} - status: ${status}`);
}
}
}
/*
** getHeadingSelector: Return a CSS selector for heading levels 1
** through 'maxLevel'. If 'mainOnly' is true, select headings only
** if they are contained within a 'main' landmark.
*/
function getHeadingSelector (options) {
let maxLevel = options.maxLevelIndex + 2;
let mainOnly = options.mainOnly;
let selectors = [];
for (let i = 1; i <= maxLevel; i++) {
let tagName = `h${i}`;
if (mainOnly) {
selectors.push(`main ${tagName}`, `[role="main"] ${tagName}`);
}
else {
selectors.push(tagName);
}
}
return selectors.join(', ');
}
/*
** getHeadingElements: Return a nodelist of all headings in the
** document based on the specified and constructed CSS selector.
*/
function getHeadingElements (options) {
if (debug) console.log(options);
let selector = getHeadingSelector(options);
console.log(selector);
return document.querySelectorAll(selector);
}
/*
** Process the page by (1) setting the data attribute on the selected elements
** and (2) collecting the skipTo menu data and sending it to the popup script.
*/
function processPage (options) {
let landmarksArray = [];
let headingsArray = [];
let counter = 0;
// Process the landmark elements
let mainLandmarks = document.querySelectorAll('main, [role="main"]');
let searchLandmarks = document.querySelectorAll('[role="search"]');
let navigationLandmarks = document.querySelectorAll('nav, [role="navigation"]');
function getLandmarkInfo (elem, dataId, role) {
return {
tagName: elem.tagName.toLowerCase(),
ariaRole: role,
accessibleName: getLandmarkAccessibleName(elem),
dataId: dataId
};
}
mainLandmarks.forEach(function (elem) {
if (isVisible(elem)) {
let dataId = `m-${++counter}`;
elem.setAttribute(dataAttribName, dataId);
landmarksArray.push(getLandmarkInfo(elem, dataId, 'main'));
}
});
searchLandmarks.forEach(function (elem) {
if (isVisible(elem)) {
let dataId = `s-${++counter}`;
elem.setAttribute(dataAttribName, dataId);
landmarksArray.push(getLandmarkInfo(elem, dataId, 'search'));
}
});
navigationLandmarks.forEach(function (elem) {
if (isVisible(elem)) {
let dataId = `n-${++counter}`;
elem.setAttribute(dataAttribName, dataId);
landmarksArray.push(getLandmarkInfo(elem, dataId, 'navigation'));
}
});
// Process the heading elements
let headingElements = getHeadingElements(options);
headingElements.forEach(function (elem) {
if (isVisible(elem)) {
let dataId = `h-${++counter}`;
elem.setAttribute(dataAttribName, dataId);
headingsArray.push({
tagName: elem.tagName.toLowerCase(),
content: getTextContent(elem).trim(),
dataId: dataId
});
}
});
const message = {
id: 'menudata',
landmarks: landmarksArray,
headings: headingsArray
};
popupPort.postMessage(message);
}
/*
** removeDataAttributes: Prevent skipTo links from being out-of-sync
** when user preferences/options are changed.
*/
function removeDataAttributes () {
const dataElements = document.querySelectorAll(`[${dataAttribName}]`);
console.log(`dataElements: ${dataElements.length}`);
dataElements.forEach(elem => {
elem.removeAttribute(dataAttribName);
})
}