|
| 1 | +export async function GET() { |
| 2 | + const script = `(function() { |
| 3 | + function resizeIframeToContentSize(iframe) { |
| 4 | + if (iframe.contentWindow) { |
| 5 | + const maxHeight = window.innerHeight * 0.8; // 80% of window height |
| 6 | + const contentHeight = iframe.contentWindow.document.body.scrollHeight; |
| 7 | + console.log("contentHeight", contentHeight); |
| 8 | + iframe.style.height = Math.min(contentHeight, maxHeight) + "px"; |
| 9 | + } |
| 10 | + } |
| 11 | +
|
| 12 | + document.addEventListener('DOMContentLoaded', function() { |
| 13 | + const button = document.createElement('button'); |
| 14 | + button.className = 'fixed bottom-5 right-5 z-50 px-1.5 py-1 bg-blue-500 text-white rounded cursor-pointer hover:bg-blue-600 transition-colors flex items-center focus:outline-none'; |
| 15 | +
|
| 16 | + const img = document.createElement('img'); |
| 17 | + img.src = 'https://huggingface.co/chat/huggingchat/logo.svg'; |
| 18 | + img.alt = 'HuggingChat Logo'; |
| 19 | + img.className = 'size-4 mr-0.5'; |
| 20 | +
|
| 21 | + const text = document.createTextNode('Chat'); |
| 22 | +
|
| 23 | + button.appendChild(img); |
| 24 | + button.appendChild(text); |
| 25 | +
|
| 26 | + const modal = document.createElement('div'); |
| 27 | + modal.className = 'hidden fixed inset-0 z-[1001] overflow-auto bg-black bg-opacity-50'; |
| 28 | +
|
| 29 | + const modalContent = document.createElement('div'); |
| 30 | + modalContent.className = 'bg-transparent mx-auto my-[5%] max-w-2xl rounded'; |
| 31 | +
|
| 32 | + const closeButton = document.createElement('span'); |
| 33 | + closeButton.innerHTML = '×'; |
| 34 | + closeButton.className = 'text-gray-500 float-right text-2xl font-bold cursor-pointer hover:text-gray-700'; |
| 35 | +
|
| 36 | + const iframe = document.createElement('iframe'); |
| 37 | + iframe.className = 'w-full rounded-xl'; |
| 38 | + iframe.style.height = '500px'; // Set an initial height |
| 39 | + iframe.src = \`http://localhost:5173/chat/assistant/66f40815222227d57241a145/embedded\`; |
| 40 | + |
| 41 | + iframe.onload = function() { |
| 42 | + const iframeWindow = this.contentWindow; |
| 43 | + iframeWindow.parent = { |
| 44 | + resizeIframeToContentSize: resizeIframeToContentSize.bind(null, iframe) |
| 45 | + }; |
| 46 | +
|
| 47 | + const script = iframeWindow.document.createElement('script'); |
| 48 | + script.textContent = \` |
| 49 | + this.container = this.frameElement.contentWindow.document.body; |
| 50 | + this.lastScrollHeight = 0; |
| 51 | +
|
| 52 | + this.watch = () => { |
| 53 | + cancelAnimationFrame(this.watcher); |
| 54 | +
|
| 55 | + if (this.lastScrollHeight !== container.scrollHeight) { |
| 56 | + parent.resizeIframeToContentSize(); |
| 57 | + } |
| 58 | + this.lastScrollHeight = container.scrollHeight; |
| 59 | + this.watcher = requestAnimationFrame(this.watch); |
| 60 | + }; |
| 61 | + this.watcher = window.requestAnimationFrame(this.watch); |
| 62 | + \`; |
| 63 | + iframeWindow.document.body.appendChild(script); |
| 64 | + }; |
| 65 | +
|
| 66 | + modalContent.appendChild(closeButton); |
| 67 | + modalContent.appendChild(iframe); |
| 68 | + modal.appendChild(modalContent); |
| 69 | +
|
| 70 | + function closeModal() { |
| 71 | + modal.classList.add('hidden'); |
| 72 | + } |
| 73 | +
|
| 74 | + button.onclick = function() { |
| 75 | + modal.classList.remove('hidden'); |
| 76 | + resizeIframeToContentSize(iframe); // Resize on opening to ensure correct initial size |
| 77 | + }; |
| 78 | +
|
| 79 | + closeButton.onclick = closeModal; |
| 80 | +
|
| 81 | + window.onclick = function(event) { |
| 82 | + if (event.target == modal) { |
| 83 | + closeModal(); |
| 84 | + } |
| 85 | + }; |
| 86 | +
|
| 87 | + document.addEventListener('keydown', function(event) { |
| 88 | + if (event.key === 'Escape') { |
| 89 | + closeModal(); |
| 90 | + } |
| 91 | + }); |
| 92 | +
|
| 93 | + // Add resize event listener to adjust iframe height when window is resized |
| 94 | + window.addEventListener('resize', function() { |
| 95 | + if (!modal.classList.contains('hidden')) { |
| 96 | + resizeIframeToContentSize(iframe); |
| 97 | + } |
| 98 | + }); |
| 99 | +
|
| 100 | + document.body.appendChild(button); |
| 101 | + document.body.appendChild(modal); |
| 102 | + }); |
| 103 | +})(); |
| 104 | +`; |
| 105 | + |
| 106 | + return new Response(script, { |
| 107 | + headers: { |
| 108 | + "Content-Type": "application/javascript", |
| 109 | + "Access-Control-Allow-Origin": "*", |
| 110 | + }, |
| 111 | + }); |
| 112 | +} |
0 commit comments