Spaces:
Running

chatdepp / embed.js
ericmendez's picture
Como configurar com a minha API? - Initial Deployment
ca7e0e3 verified
Raw History Blame Contribute Delete
5.46 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DeepSeek Chat Web Component</title>
<script>
// Define the Web Component
class DeepSeekChat extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
// Get attributes
const token = this.getAttribute('token') || '';
const theme = this.getAttribute('theme') || 'auto';
const lang = this.getAttribute('lang') || 'en';
const hideHeader = this.hasAttribute('hide-header');
const templateId = this.getAttribute('template-id') || '';
// Create iframe
this.iframe = document.createElement('iframe');
this.iframe.src = `https://app.tuaapp.com/embed?token=${token}&theme=${theme}&lang=${lang}&hideHeader=${hideHeader ? '1' : '0'}&templateId=${templateId}`;
this.iframe.style.width = '100%';
this.iframe.style.height = '100%';
this.iframe.style.border = 'none';
this.iframe.loading = 'lazy';
// Append to shadow DOM
this.shadowRoot.appendChild(this.iframe);
// Listen for messages from iframe
window.addEventListener('message', this.handleMessage.bind(this));
}
connectedCallback() {
// Set initial styles
this.style.display = 'block';
this.style.width = '100%';
this.style.height = '500px';
this.style.maxHeight = '100vh';
this.style.borderRadius = '0.5rem';
this.style.overflow = 'hidden';
this.style.boxShadow = '0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06)';
}
disconnectedCallback() {
window.removeEventListener('message', this.handleMessage);
}
handleMessage(event) {
// Verify origin
if (!event.origin.match(/app\.tuaapp\.com$/)) return;
const data = event.data;
switch (data.type) {
case 'heightChange':
this.style.height = `${data.height}px`;
this.dispatchEvent(new CustomEvent('ds:heightChange', { detail: data.height }));
break;
case 'messageSent':
this.dispatchEvent(new CustomEvent('ds:messageSent', { detail: data.content }));
break;
case 'messageReceived':
this.dispatchEvent(new CustomEvent('ds:messageReceived', { detail: data.content }));
break;
case 'error':
this.dispatchEvent(new CustomEvent('ds:error', { detail: data.message }));
break;
case 'ready':
this.dispatchEvent(new CustomEvent('ds:ready'));
break;
}
}
// Public methods
setTheme(theme) {
this.iframe.contentWindow.postMessage({
type: 'setTheme',
theme: theme
}, '*');
}
setLang(lang) {
this.iframe.contentWindow.postMessage({
type: 'setLang',
lang: lang
}, '*');
}
setTemplate(templateId) {
this.iframe.contentWindow.postMessage({
type: 'setTemplate',
templateId: templateId
}, '*');
}
focus() {
this.iframe.contentWindow.postMessage({
type: 'focus'
}, '*');
}
resize(height) {
this.style.height = `${height}px`;
}
}
// Register the Web Component
customElements.define('deepseek-chat', DeepSeekChat);
</script>
</head>
<body>
<h1>DeepSeek Chat Web Component</h1>
<p>This file defines the Web Component that can be used to embed the chat interface.</p>
<script>
// Example usage (for documentation purposes)
document.addEventListener('DOMContentLoaded', () => {
const chat = document.createElement('deepseek-chat');
chat.setAttribute('token', 'YOUR_JWT_TOKEN');
chat.setAttribute('theme', 'auto');
chat.setAttribute('lang', 'en');
document.body.appendChild(chat);
chat.addEventListener('ds:ready', () => {
console.log('Chat is ready');
});
chat.addEventListener('ds:messageSent', (e) => {
console.log('Message sent:', e.detail);
});
chat.addEventListener('ds:messageReceived', (e) => {
console.log('Message received:', e.detail);
});
});
</script>
</body>
</html>