54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
/*
|
|
|
|
Extracts the table of contents from [see supported pages] into your clipboard as a markdown checklist
|
|
|
|
Supported pages:
|
|
- https://www.w3.org/TR/WCAG21/
|
|
- https://www.w3.org/TR/WCAG22/
|
|
- https://www.w3.org/TR/wcag-3.0/
|
|
|
|
How to use:
|
|
1. Open the page
|
|
2. Copy the script below into the clipboard
|
|
3. Open the developer console (usually with "F12" and by then navigating to "Console" in the developer settings)
|
|
4. Paste the script into the console and press enter to confirm
|
|
5. A red button appears on the page. Click it.
|
|
6. The checklist is now in your clipboard. Paste it whereever you need.
|
|
*/
|
|
let indentation = "\t"; // Choose your indentation
|
|
const copyButton = document.createElement('button');
|
|
copyButton.textContent = 'Copy TOC to Clipboard';
|
|
copyButton.style.position = 'fixed';
|
|
copyButton.style.top = '20px';
|
|
copyButton.style.right = '90px';
|
|
copyButton.style.zIndex = '1000';
|
|
copyButton.style.backgroundColor = 'red';
|
|
copyButton.style.color = 'white';
|
|
copyButton.style.scale = '2';
|
|
|
|
copyButton.addEventListener('click', function() {
|
|
let elements = document.querySelectorAll(".toc .tocline .tocxref:has(bdi)");
|
|
let text = "";
|
|
elements.forEach(element => {
|
|
let text1 = element.childNodes[0].textContent;
|
|
let text2 = element.childNodes[1].textContent;
|
|
let segmentCount = text1.replace(". ", "").split(".").length;
|
|
text += indentation.repeat(segmentCount - 1) + "- [ ] " + text1 + text2 + "\n";
|
|
});
|
|
|
|
navigator.clipboard.writeText(text)
|
|
.then(() => {
|
|
console.log("Copied to clipboard successfully!");
|
|
this.textContent = "Copied!";
|
|
setTimeout(() => {
|
|
this.textContent = "Copy TOC to Clipboard";
|
|
}, 2000);
|
|
})
|
|
.catch(err => {
|
|
console.error("Failed to copy: ", err);
|
|
alert("Failed to copy to clipboard. Please try again or copy manually.");
|
|
});
|
|
});
|
|
|
|
document.body.appendChild(copyButton);
|