-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlandmark.js
84 lines (75 loc) · 2.94 KB
/
landmark.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
let isLandmarkHighlighted = false;
const highlightLandmarks = () => {
removeHighlights();
const landmarks = [
{ tag: '[role=main]', label: 'role="main"' },
{ tag: '[role=search]', label: 'role="search"' },
{ tag: '[role=contentinfo]', label: 'role="contentinfo"' },
{ tag: '[role=banner]', label: 'role="banner"' },
{ tag: '[role=navigation]', label: 'role="navigation"' },
{ tag: '[role=complementary]', label: 'role="complementary"' },
{ tag: '[role=application]', label: 'role="application"' }
];
const sections = [
{ tag: 'main', label: '<main>' },
{ tag: 'banner', label: '<banner>' },
{ tag: 'footer', label: '<footer>' },
{ tag: 'header', label: '<header>' },
{ tag: 'aside', label: '<aside>' },
{ tag: 'nav', label: '<nav>' }
];
const spanStyle = 'outline:green 2px solid;padding:1px;color:black;font-family:sans-serif;font-weight:bold;font-size:small;background-color:yellow;position:relative;line-height:100%;z-index:2147483647;';
const elementStyle = 'outline:green 2px solid;padding:2px;';
landmarks.forEach(({ tag, label }) => {
document.querySelectorAll(tag).forEach(el => {
el.setAttribute('style', elementStyle);
const span = document.createElement('span');
span.className = 'axSpan';
span.style.cssText = spanStyle;
span.textContent = label;
el.before(span);
});
});
sections.forEach(({ tag, label }) => {
document.querySelectorAll(tag).forEach(el => {
el.setAttribute('style', elementStyle);
const span = document.createElement('span');
span.className = 'axSpan';
span.style.cssText = spanStyle;
span.textContent = label;
el.before(span);
});
});
if (!document.querySelectorAll(landmarks.map(l => l.tag).join(',')).length) {
const failure = document.createElement('strong');
failure.id = 'failure';
failure.setAttribute('role', 'status');
failure.style.cssText = 'color:black;font-weight:bold;font-family:sans-serif;font-size:small;background-color:yellow;margin:0 2px; padding:2px;';
failure.textContent = 'No Landmarks Found on Page: ' + document.title;
document.body.prepend(failure);
setTimeout(() => failure.remove(), 6000);
} else {
const success = document.createElement('div');
success.id = 'success';
success.setAttribute('role', 'alert');
success.style.cssText = 'position:absolute; width:0; height:0; clip: rect(0,0,0,0);';
success.textContent = 'Success! Landmarks Found on Page: ' + document.title;
document.body.appendChild(success);
setTimeout(() => success.remove(), 3000);
}
};
const removeHighlights = () => {
document.querySelectorAll("span.axSpan").forEach(el => el.remove());
const successMessage = document.getElementById("success");
if (successMessage) successMessage.remove();
};
export const toggleLandmarks = () => {
if (isLandmarkHighlighted) {
removeHighlights();
alert("Landmark Markers Removed.");
} else {
highlightLandmarks();
alert("Landmark Markers Added.");
}
isLandmarkHighlighted = !isLandmarkHighlighted;
};