async function embedChatbot() {
const chatBtnId = 'fastgpt-chatbot-button';
const chatWindowId = 'fastgpt-chatbot-window';
const script = document.getElementById('fastgpt-iframe');
const botSrc = script?.getAttribute('data-src');
const primaryColor = script?.getAttribute('data-color') || '#4e83fd';
if (!botSrc) {
console.error(`Can't find appid`);
return;
}
if (document.getElementById(chatBtnId)) {
return;
}
const MessageIcon = ``;
const CloseIcon = ``;
const ChatBtn = document.createElement('div');
ChatBtn.id = chatBtnId;
ChatBtn.style.cssText =
'position: fixed; bottom: 1rem; right: 1rem; width: 40px; height: 40px; cursor: pointer; z-index: 2147483647; ';
const ChatBtnDiv = document.createElement('div');
ChatBtnDiv.style.cssText =
'transition: all 0.2s ease-in-out 0s; left: unset; transform: scale(1); :hover {transform: scale(1.1);} display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; z-index: 9999;';
ChatBtnDiv.innerHTML = MessageIcon;
ChatBtn.appendChild(ChatBtnDiv);
const iframe = document.createElement('iframe');
iframe.allow = 'fullscreen;microphone';
iframe.title = 'FastGpt Chat Window';
iframe.id = chatWindowId;
iframe.src = botSrc;
iframe.style.cssText =
'visibility: hidden; border: none; position: fixed; flex-direction: column; justify-content: space-between; box-shadow: rgba(150, 150, 150, 0.2) 0px 10px 30px 0px, rgba(150, 150, 150, 0.2) 0px 0px 0px 1px; bottom: 4rem; right: 1rem; width: 24rem; height: 40rem; max-width: 90vw; max-height: 85vh; border-radius: 0.75rem; display: flex; z-index: 2147483647; overflow: hidden; left: unset; background-color: #F3F4F6;';
document.body.appendChild(iframe);
iframe.onload = () => {
document.body.appendChild(ChatBtn);
};
ChatBtn.addEventListener('click', function () {
const chatWindow = document.getElementById(chatWindowId);
if (!chatWindow) return;
const visibilityVal = chatWindow.style.visibility;
if (visibilityVal === 'hidden') {
chatWindow.style.visibility = 'unset';
ChatBtnDiv.innerHTML = CloseIcon;
} else {
chatWindow.style.visibility = 'hidden';
ChatBtnDiv.innerHTML = MessageIcon;
}
});
}
document.body.onload = embedChatbot;