playgo_next / public /embed.js
ChenyuRabbitLove's picture
feat: add embeddedable chat component
c4412d0
(function() {
console.log('Initializing PlayGo Chat Widget');
function createChatWidget(config) {
console.log('Creating chat widget with config:', config);
// Create container for positioning
const container = document.createElement('div');
Object.assign(container.style, {
position: 'fixed',
bottom: '0',
right: '0',
zIndex: '9999',
padding: '20px',
transition: 'all 0.3s ease-in-out',
background: 'transparent',
width: '250px',
height: '120px',
display: 'flex',
alignItems: 'flex-end',
justifyContent: 'flex-end'
});
// Create iframe
const iframe = document.createElement('iframe');
Object.assign(iframe.style, {
border: 'none',
background: 'transparent',
width: '180px',
height: '60px',
transition: 'all 0.3s ease-in-out',
transform: 'scale(1)',
transformOrigin: 'bottom right'
});
// Set source with config parameters
const queryParams = new URLSearchParams(config).toString();
const chatUrl = `${window.location.origin}/embed/chat?${queryParams}`;
iframe.src = chatUrl;
// Handle messages from the iframe
window.addEventListener('message', function(event) {
if (event.data.type === 'chatOpen') {
container.style.padding = '20px';
container.style.width = '380px';
container.style.height = '600px';
iframe.style.width = '100%';
iframe.style.height = '100%';
} else if (event.data.type === 'chatClose') {
container.style.padding = '20px';
container.style.width = '250px';
container.style.height = '120px';
iframe.style.width = '180px';
iframe.style.height = '60px';
}
});
container.appendChild(iframe);
return container;
}
// Initialize the widget
window.playgo = function(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);
}
}
};
// Process any queued commands
if (window.PlayGoChatWidget?.q) {
window.PlayGoChatWidget.q.forEach(args => window.playgo(...args));
}
})();