|
| 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.innerHTML = 'AI Chat'; |
| 15 | + button.className = 'fixed bottom-5 left-5 z-50 px-5 py-2.5 bg-blue-500 text-white rounded cursor-pointer hover:bg-blue-600 transition-colors'; |
| 16 | +
|
| 17 | + const modal = document.createElement('div'); |
| 18 | + modal.className = 'hidden fixed inset-0 z-[1001] overflow-auto bg-black bg-opacity-50'; |
| 19 | +
|
| 20 | + const modalContent = document.createElement('div'); |
| 21 | + modalContent.className = 'bg-transparent mx-auto my-[5%] max-w-2xl rounded'; |
| 22 | +
|
| 23 | + const closeButton = document.createElement('span'); |
| 24 | + closeButton.innerHTML = '×'; |
| 25 | + closeButton.className = 'text-gray-500 float-right text-2xl font-bold cursor-pointer hover:text-gray-700'; |
| 26 | +
|
| 27 | + const iframe = document.createElement('iframe'); |
| 28 | + iframe.className = 'w-full rounded-xl'; |
| 29 | + iframe.style.height = '500px'; // Set an initial height |
| 30 | + iframe.src = \`http://localhost:5173/chat/\`; |
| 31 | + |
| 32 | + iframe.onload = function() { |
| 33 | + const iframeWindow = this.contentWindow; |
| 34 | + iframeWindow.parent = { |
| 35 | + resizeIframeToContentSize: resizeIframeToContentSize.bind(null, iframe) |
| 36 | + }; |
| 37 | +
|
| 38 | + const script = iframeWindow.document.createElement('script'); |
| 39 | + script.textContent = \` |
| 40 | + this.container = this.frameElement.contentWindow.document.body; |
| 41 | + this.lastScrollHeight = 0; |
| 42 | +
|
| 43 | + this.watch = () => { |
| 44 | + cancelAnimationFrame(this.watcher); |
| 45 | +
|
| 46 | + if (this.lastScrollHeight !== container.scrollHeight) { |
| 47 | + parent.resizeIframeToContentSize(); |
| 48 | + } |
| 49 | + this.lastScrollHeight = container.scrollHeight; |
| 50 | + this.watcher = requestAnimationFrame(this.watch); |
| 51 | + }; |
| 52 | + this.watcher = window.requestAnimationFrame(this.watch); |
| 53 | + \`; |
| 54 | + iframeWindow.document.body.appendChild(script); |
| 55 | + }; |
| 56 | +
|
| 57 | + modalContent.appendChild(closeButton); |
| 58 | + modalContent.appendChild(iframe); |
| 59 | + modal.appendChild(modalContent); |
| 60 | +
|
| 61 | + function closeModal() { |
| 62 | + modal.classList.add('hidden'); |
| 63 | + } |
| 64 | +
|
| 65 | + button.onclick = function() { |
| 66 | + modal.classList.remove('hidden'); |
| 67 | + resizeIframeToContentSize(iframe); // Resize on opening to ensure correct initial size |
| 68 | + }; |
| 69 | +
|
| 70 | + closeButton.onclick = closeModal; |
| 71 | +
|
| 72 | + window.onclick = function(event) { |
| 73 | + if (event.target == modal) { |
| 74 | + closeModal(); |
| 75 | + } |
| 76 | + }; |
| 77 | +
|
| 78 | + document.addEventListener('keydown', function(event) { |
| 79 | + if (event.key === 'Escape') { |
| 80 | + closeModal(); |
| 81 | + } |
| 82 | + }); |
| 83 | +
|
| 84 | + // Add resize event listener to adjust iframe height when window is resized |
| 85 | + window.addEventListener('resize', function() { |
| 86 | + if (!modal.classList.contains('hidden')) { |
| 87 | + resizeIframeToContentSize(iframe); |
| 88 | + } |
| 89 | + }); |
| 90 | +
|
| 91 | + document.body.appendChild(button); |
| 92 | + document.body.appendChild(modal); |
| 93 | + }); |
| 94 | +})(); |
| 95 | +`; |
| 96 | + |
| 97 | + return new Response(script, { |
| 98 | + headers: { |
| 99 | + "Content-Type": "application/javascript", |
| 100 | + "Access-Control-Allow-Origin": "*", |
| 101 | + }, |
| 102 | + }); |
| 103 | +} |
0 commit comments