|
| 1 | +/* |
| 2 | + * Replace the contents of the Chainlit watermark/footer |
| 3 | + */ |
| 4 | +(function () { |
| 5 | + const CUSTOM_FOOTER_HTML = ` |
| 6 | + <div class="text-xs text-muted-foreground text-center"> |
| 7 | + <span> |
| 8 | + <em> |
| 9 | + <strong>Disclaimer:</strong> |
| 10 | + Our chatbot uses AI to assist you. |
| 11 | + Responses are generated automatically and may not always be accurate. |
| 12 | + Do not share sensitive, personal or confidential information. |
| 13 | + For more information, please click on the “Readme” icon at the top-right of this window. |
| 14 | + </em> |
| 15 | + </span> |
| 16 | + </div> |
| 17 | + `.trim(); |
| 18 | + |
| 19 | + const WATERMARK_SELECTOR = 'a.watermark'; |
| 20 | + const APPLIED_ATTR = 'data-custom-watermark'; |
| 21 | + |
| 22 | + function replaceFooterContents(root = document) { |
| 23 | + const nodes = root instanceof Element |
| 24 | + ? root.querySelectorAll(WATERMARK_SELECTOR) |
| 25 | + : document.querySelectorAll(WATERMARK_SELECTOR); |
| 26 | + |
| 27 | + nodes.forEach((el) => { |
| 28 | + if (!(el instanceof HTMLElement)) return; |
| 29 | + if (el.getAttribute(APPLIED_ATTR) === '1') return; |
| 30 | + |
| 31 | + el.innerHTML = CUSTOM_FOOTER_HTML; |
| 32 | + |
| 33 | + // disable the link behaviour |
| 34 | + el.removeAttribute('href'); |
| 35 | + el.removeAttribute('target'); |
| 36 | + el.style.pointerEvents = 'none'; |
| 37 | + |
| 38 | + el.setAttribute(APPLIED_ATTR, '1'); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + // Initial run (in case the element is already present). |
| 43 | + if (document.readyState === 'loading') { |
| 44 | + document.addEventListener('DOMContentLoaded', () => replaceFooterContents(document)); |
| 45 | + } else { |
| 46 | + replaceFooterContents(document); |
| 47 | + } |
| 48 | + |
| 49 | + // Re-apply on future UI updates (SPA re-renders). |
| 50 | + const mo = new MutationObserver((mutations) => { |
| 51 | + for (const m of mutations) { |
| 52 | + for (const node of m.addedNodes) { |
| 53 | + if (node instanceof Element) { |
| 54 | + // If the watermark itself is added or its parent subtree changes, update. |
| 55 | + if (node.matches?.(WATERMARK_SELECTOR) || node.querySelector?.(WATERMARK_SELECTOR)) { |
| 56 | + replaceFooterContents(node); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + mo.observe(document.documentElement, { childList: true, subtree: true }); |
| 64 | +})(); |
0 commit comments