Spaces:
Sleeping
Sleeping
import { NextResponse } from 'next/server'; | |
export async function GET() { | |
const baseUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'; | |
// Return the actual embed.js content directly instead of loading it from a file | |
const embedScript = ` | |
(function() { | |
console.log('Initializing PlayGo Chat Widget'); | |
function createChatWidget(config) { | |
console.log('Creating chat widget with config:', config); | |
const iframe = document.createElement('iframe'); | |
const iframeStyles = { | |
border: 'none', | |
position: 'fixed', | |
bottom: '0', | |
right: '0', | |
width: '380px', | |
height: '100px', | |
maxHeight: '600px', | |
zIndex: '9999', | |
background: 'none', | |
pointerEvents: 'all', | |
transition: 'height 0.5s ease-in-out', | |
overflow: 'hidden', | |
}; | |
Object.assign(iframe.style, iframeStyles); | |
iframe.onload = function() { | |
const style = document.createElement('style'); | |
style.textContent = \` | |
::-webkit-scrollbar { | |
display: none !important; | |
} | |
* { | |
-ms-overflow-style: none !important; | |
scrollbar-width: none !important; | |
} | |
\`; | |
iframe.contentDocument.head.appendChild(style); | |
}; | |
const queryParams = new URLSearchParams(config).toString(); | |
const chatUrl = '${baseUrl}/embed/chat?' + queryParams; | |
console.log('Chat URL:', chatUrl); | |
iframe.src = chatUrl; | |
window.addEventListener('message', function(event) { | |
console.log('Received message:', event.data); | |
if (event.data.type === 'CHAT_READY') { | |
console.log('Chat widget ready'); | |
} else if (event.data.type === 'chatOpen') { | |
iframe.style.height = '600px'; | |
} else if (event.data.type === 'chatClose') { | |
iframe.style.height = '100px'; | |
} | |
}); | |
return iframe; | |
} | |
window.playgo = function(action, config) { | |
console.log('PlayGo action called:', action, config); | |
if (action === 'init') { | |
try { | |
const existingWidget = document.getElementById('playgo-chat-widget'); | |
if (existingWidget) { | |
existingWidget.remove(); | |
} | |
const widget = createChatWidget(config); | |
widget.id = 'playgo-chat-widget'; | |
document.body.appendChild(widget); | |
console.log('PlayGo chat initialized successfully'); | |
} catch (error) { | |
console.error('Error initializing PlayGo chat:', error); | |
} | |
} | |
}; | |
if (window.PlayGoChatWidget && window.PlayGoChatWidget.q) { | |
console.log('Processing queued commands:', window.PlayGoChatWidget.q); | |
window.PlayGoChatWidget.q.forEach(args => { | |
window.playgo.apply(null, args); | |
}); | |
} | |
if (window.PlayGoChatWidget) { | |
window.PlayGoChatWidget.ready = true; | |
} | |
console.log('PlayGo Chat Widget initialization complete'); | |
})(); | |
`; | |
return new NextResponse(embedScript, { | |
headers: { | |
'Content-Type': 'application/javascript', | |
'Cache-Control': 'no-store, max-age=0', | |
}, | |
}); | |
} |