Wiki source code of LLMChatRenderer
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{html}} | ||
2 | <script> | ||
3 | console.log("✅ Script running without Velocity"); | ||
4 | |||
5 | document.addEventListener('DOMContentLoaded', function () { | ||
6 | function processAssistantMessages() { | ||
7 | const messages = document.querySelectorAll('.assistantMessage'); | ||
8 | messages.forEach(msg => { | ||
9 | const html = msg.innerHTML; | ||
10 | const chunkMap = {}; | ||
11 | const chunkRegex = /\[(\d+)\]\s*\((https:\/\/[^)]+)\)/g; | ||
12 | let match; | ||
13 | while ((match = chunkRegex.exec(html)) !== null) { | ||
14 | const index = match[1]; | ||
15 | const url = match[2]; | ||
16 | const path = extractDownloadPath(url); | ||
17 | if (path) chunkMap[index] = path; | ||
18 | } | ||
19 | |||
20 | let currentIndex = null; | ||
21 | const processed = html.replace(/(\[\d+\])\s*\((https:\/\/[^)]+)\)/g, (full, idx) => { | ||
22 | currentIndex = idx.replace(/\D/g, ''); | ||
23 | return full; | ||
24 | }).replace(/\[\[(image|doc_)?([^\]]+?)\]\]/g, function (_, type, filename) { | ||
25 | if (!currentIndex || !chunkMap[currentIndex]) return ''; | ||
26 | const path = chunkMap[currentIndex]; | ||
27 | const downloadURL = `/bin/download/${path}/${filename}`; | ||
28 | return type === 'image' | ||
29 | ? `<img src="${downloadURL}" style="max-width:300px; display:block; margin:1em 0;" />` | ||
30 | : `<a href="${downloadURL}" target="_blank">📄 Download ${filename}</a>`; | ||
31 | }); | ||
32 | |||
33 | msg.innerHTML = processed; | ||
34 | }); | ||
35 | } | ||
36 | |||
37 | function extractDownloadPath(viewURL) { | ||
38 | const match = viewURL.match(/\/bin\/view\/([^?#]+)/); | ||
39 | return match ? decodeURIComponent(match[1]) : null; | ||
40 | } | ||
41 | |||
42 | const observer = new MutationObserver(processAssistantMessages); | ||
43 | observer.observe(document.body, { childList: true, subtree: true }); | ||
44 | }); | ||
45 | </script> | ||
46 | {{/html}} |