-
Notifications
You must be signed in to change notification settings - Fork 0
/
heading.js
59 lines (50 loc) · 1.64 KB
/
heading.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
let isHeadingHighlighted = false;
const addHeadings = () => {
removeHeadings();
const headings = [
{ tag: 'h1', label: 'h1' },
{ tag: 'h2', label: 'h2' },
{ tag: 'h3', label: 'h3' },
{ tag: 'h4', label: 'h4' },
{ tag: 'h5', label: 'h5' },
{ tag: 'h6', label: 'h6' }
];
// Adding role="heading" and aria-level="1" to "6"
for (let i = 1; i <= 6; i++) {
const selector = `[role="heading"][aria-level="${i}"]`;
headings.push({
tag: selector,
label: selector.replace(/["']/g, '') // Remove quotes
});
}
const tagStyle = 'color:black; font-family:sans-serif; font-weight:bold; font-size:small; background-color:yellow;'; // Scoped style variable
headings.forEach(({ tag, label }) => {
document.querySelectorAll(tag).forEach(el => {
const openTag = document.createElement('strong');
openTag.className = 'openSpan';
openTag.style.cssText = tagStyle; // Apply scoped style
openTag.textContent = `<${label}>`;
const closeTag = document.createElement('strong');
closeTag.className = 'closeSpan';
closeTag.style.cssText = tagStyle; // Apply scoped style
closeTag.textContent = `</${label}>`;
el.prepend(openTag);
el.append(closeTag);
});
});
};
// Function to remove headings with <strong> tags
const removeHeadings = () => {
document.querySelectorAll("strong.openSpan, strong.closeSpan").forEach(el => el.remove());
};
// Function to toggle heading visibility
export const toggleHeadings = () => {
if (isHeadingHighlighted) {
removeHeadings();
alert("Heading Markers Removed.");
} else {
addHeadings();
alert("Heading Markers Added.");
}
isHeadingHighlighted = !isHeadingHighlighted;
};