39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
/**
|
|
* Strips ".html" from link text, preserving href.
|
|
* Idempotent and works for dynamically added links.
|
|
*
|
|
* @param {HTMLElement} link - The <a> element to process
|
|
*/
|
|
function beautifyLinkText(link) {
|
|
const originalText = link.dataset.originalText || link.textContent;
|
|
|
|
// Only strip if the text ends with ".html"
|
|
if (originalText.endsWith(".html")) {
|
|
link.textContent = originalText.replace(/\.html$/, "");
|
|
}
|
|
|
|
// Store original text to avoid double-processing
|
|
link.dataset.originalText = originalText;
|
|
}
|
|
|
|
// Process all existing links
|
|
document.querySelectorAll("a").forEach(beautifyLinkText);
|
|
|
|
// Observe new links added dynamically
|
|
const observer = new MutationObserver(mutations => {
|
|
mutations.forEach(mutation => {
|
|
mutation.addedNodes.forEach(node => {
|
|
if (node.nodeType !== 1) return; // Not an element
|
|
|
|
if (node.tagName === "A") {
|
|
beautifyLinkText(node);
|
|
} else {
|
|
node.querySelectorAll("a").forEach(beautifyLinkText);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
observer.observe(document.body, { childList: true, subtree: true });
|
|
|