diff --git a/projects/app/public/chrome_extension/fastgpt_agent/img/favicon16.png b/projects/app/public/chrome_extension/fastgpt_agent/img/favicon16.png new file mode 100644 index 000000000..db96f84ad Binary files /dev/null and b/projects/app/public/chrome_extension/fastgpt_agent/img/favicon16.png differ diff --git a/projects/app/public/chrome_extension/fastgpt_agent/img/favicon32.png b/projects/app/public/chrome_extension/fastgpt_agent/img/favicon32.png new file mode 100644 index 000000000..f5619a938 Binary files /dev/null and b/projects/app/public/chrome_extension/fastgpt_agent/img/favicon32.png differ diff --git a/projects/app/public/chrome_extension/fastgpt_agent/img/fullScreen.png b/projects/app/public/chrome_extension/fastgpt_agent/img/fullScreen.png new file mode 100644 index 000000000..5fa0187d9 Binary files /dev/null and b/projects/app/public/chrome_extension/fastgpt_agent/img/fullScreen.png differ diff --git a/projects/app/public/chrome_extension/fastgpt_agent/img/setting.png b/projects/app/public/chrome_extension/fastgpt_agent/img/setting.png new file mode 100644 index 000000000..b91d958e8 Binary files /dev/null and b/projects/app/public/chrome_extension/fastgpt_agent/img/setting.png differ diff --git a/projects/app/public/chrome_extension/fastgpt_agent/manifest.json b/projects/app/public/chrome_extension/fastgpt_agent/manifest.json new file mode 100644 index 000000000..6e5f0e5bd --- /dev/null +++ b/projects/app/public/chrome_extension/fastgpt_agent/manifest.json @@ -0,0 +1,42 @@ +{ + "manifest_version": 3, + "name": "ChatBot Extension", + "version": "1.1", + "description": "A ChatBot", + "permissions": [ + "storage", + "notifications", + "tabs", + "activeTab", + "scripting", + "webRequest" + ], + "host_permissions": [ + "" + ], + "background": { + "service_worker": "src/background.js" + }, + "action": { + "default_popup": "src/popup.html", + "default_icon": { + "16": "img/favicon32.png", + "48": "img/favicon32.png", + "128": "img/favicon32.png" + } + }, + "icons": { + "16": "img/favicon32.png", + "32": "img/favicon32.png", + "48": "img/favicon32.png", + "128": "img/favicon32.png" + }, + "content_scripts": [ + { + "js": [ + "src/content.js" + ], + "matches": [""] + } + ] +} \ No newline at end of file diff --git a/projects/app/public/chrome_extension/fastgpt_agent/src/background.js b/projects/app/public/chrome_extension/fastgpt_agent/src/background.js new file mode 100644 index 000000000..5f6d3ca9c --- /dev/null +++ b/projects/app/public/chrome_extension/fastgpt_agent/src/background.js @@ -0,0 +1,51 @@ +let requestInterceptor = null; + +chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { + if (message.action === "startRequestInterception") { + const botSrc = message.chatbotSrc; + console.log("src", botSrc); + const urlObj = new URL(botSrc); + const domain = urlObj.host; + const searchParams = urlObj.searchParams; + const frameShareId = searchParams.get('shareId') || ''; + let frameChatId = searchParams.get('chatId') || ''; + + // 移除已有的拦截器(如果存在) + if (requestInterceptor) { + chrome.webRequest.onBeforeRequest.removeListener(requestInterceptor); + } + + requestInterceptor = function (details) { + if (details.frameId !== -1 + && details.url.includes(domain) + && details.url.includes("chat/completions")) { + console.log("Intercepted request from chatbot-iframe:", details); + if (details.requestBody.raw) { + let decoder = new TextDecoder("utf-8"); + let postData = decoder.decode(new Uint8Array(details.requestBody.raw[0].bytes)); + try { + let postDataObj = JSON.parse(postData); + let shareId = postDataObj.shareId; + let chatId = postDataObj.chatId; + + if (frameChatId !== chatId && frameShareId === shareId) { + chrome.storage.local.set({["shareId"]: shareId}); + chrome.storage.local.set({["chatId"]: chatId}); + frameChatId = chatId; + console.log(`Stored shareId: ${shareId} and chatId: ${chatId} to localStorage.`); + } + } catch (error) { + console.error("Error parsing postData:", error); + } + } + } + return {}; + }; + + chrome.webRequest.onBeforeRequest.addListener( + requestInterceptor, + { urls: [""] }, + ["requestBody"] + ); + } +}); diff --git a/projects/app/public/chrome_extension/fastgpt_agent/src/content.js b/projects/app/public/chrome_extension/fastgpt_agent/src/content.js new file mode 100644 index 000000000..fbf487aaa --- /dev/null +++ b/projects/app/public/chrome_extension/fastgpt_agent/src/content.js @@ -0,0 +1,533 @@ +chrome.storage.local.get(["showChatBot"], function (result) { + if (result.showChatBot === undefined || result.showChatBot) { + const chatBtnId = 'fastgpt-chatbot-button'; + const chatWindowId = 'fastgpt-chatbot-window'; + const chatWindowWrapperId = 'fastgpt-chatbot-wrapper'; + const defaultOpen = false; + const canDrag = true; + const MessageIcon = + `data:image/svg+xml;base64,PHN2ZyB0PSIxNjkwNTMyNzg1NjY0IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjQxMzIiIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIj48cGF0aCBkPSJNNTEyIDMyQzI0Ny4wNCAzMiAzMiAyMjQgMzIgNDY0QTQxMC4yNCA0MTAuMjQgMCAwIDAgMTcyLjQ4IDc2OEwxNjAgOTY1LjEyYTI1LjI4IDI1LjI4IDAgMCAwIDM5LjA0IDIyLjRsMTY4LTExMkE1MjguNjQgNTI4LjY0IDAgMCAwIDUxMiA4OTZjMjY0Ljk2IDAgNDgwLTE5MiA0ODAtNDMyUzc3Ni45NiAzMiA1MTIgMzJ6IG0yNDQuOCA0MTZsLTM2MS42IDMwMS43NmExMi40OCAxMi40OCAwIDAgMS0xOS44NC0xMi40OGw1OS4yLTIzMy45MmgtMTYwYTEyLjQ4IDEyLjQ4IDAgMCAxLTcuMzYtMjMuMzZsMzYxLjYtMzAxLjc2YTEyLjQ4IDEyLjQ4IDAgMCAxIDE5Ljg0IDEyLjQ4bC01OS4yIDIzMy45MmgxNjBhMTIuNDggMTIuNDggMCAwIDEgOCAyMi4wOHoiIGZpbGw9IiM0ZTgzZmQiIHAtaWQ9IjQxMzMiPjwvcGF0aD48L3N2Zz4=`; + const CloseIcon = + 'data:image/svg+xml;base64,PHN2ZyB0PSIxNjkwNTM1NDQxNTI2IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjA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O9cXdfqQhAlSVx96e3FAxZ1uc+RuOUHg9/BuRFEQRLz969Rp5S8/xBwJQiSMJreCLgTBEm8jUjtfFwKgiS1h9JT9W4FQRJPY1I3F9eCIEndwfRSuXtBkMTLqNTMI4QgSFJzOD1UHUYQJPEwLvVyCCUIktQbUOuKwwmCJNYjU2v9kIIgSa0htaw2rCBIYjk2ddYOLQiS1BlUq0rDC4IkVqNTY90UgiBJjWG1qDKNIEhiMT7510wlCJLkH9hXV5hOECR59QjlXi+lIEiSe2hfWV1aQZDklWOUd63UgihIYvorf3nH0G9l6QU5WRI3v9fkd6RyZVZCkJMl4eeEcjnwsJoygpwoCbsIguQlcMZj3zz8ZmzeDvmqrNQOckEvlQRBfA2xZjYlBRG+3eIaRHMinZ27rCACSRDE2RBrplNakAVJuEDXnEaH5y4vyIQkyOFwgLVTQpArwg8eB8fbKu1JdHp+BLnRmP0u1++/eHqYi9MZSp0WgqRuL8VJCSCIlCDxqQkgSOr2UpyUAIJICRKfmgCCpG4vxUkJIIiUIPGpCSBI6vZSnJQAgkgJEp+aAIKkbi/FSQkgiJQg8akJIEjq9lKclACCSAkSn5oAgqRuL8VJCSCIlCDxqQkgSOr2UpyUAIJICRKfmgCCpG4vxUkJIIiUIPGpCSBI6vZSnJQAgkgJEp+aAIKkbi/FSQkgiJQg8akJIEjq9lKclACCSAkSn5oAgqRuL8VJCSCIlCDxqQkgSOr2UpyUAIJICRKfmgCCpG4vxUkJIIiUIPGpCfwNgHnv2EHR6KQAAAAASUVORK5CYII='; + const ChatBtn = document.createElement('div'); + ChatBtn.id = chatBtnId; + ChatBtn.style.cssText = + 'position: fixed; bottom: 30px; right: 60px; width: 40px; height: 40px; cursor: pointer; z-index: 2147483647; transition: 0;'; + + const ChatBtnDiv = document.createElement('img'); + ChatBtnDiv.src = defaultOpen ? CloseIcon : MessageIcon; + ChatBtnDiv.setAttribute('width', '100%'); + ChatBtnDiv.setAttribute('height', '100%'); + ChatBtnDiv.draggable = false; + + const iframeWrapper = document.createElement('div'); + iframeWrapper.id = chatWindowWrapperId; + iframeWrapper.style.cssText = + '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: 80px; right: 60px; max-width: 90vw; min-width: 10vw; max-height: 85vh; min-height: 15vh; border-radius: 0.75rem; display: flex; z-index: 2147483647; overflow: hidden; left: unset; background-color: #F3F4F6;'; + iframeWrapper.style.visibility = defaultOpen ? 'unset' : 'hidden'; + + const iframe = document.createElement('iframe'); + iframe.referrerPolicy = 'no-referrer'; + iframe.allow = 'microphone'; + iframe.title = 'FastGPT Chat Window'; + iframe.id = chatWindowId; + iframe.style.cssText = 'border: none; width: 100%; height: 100%;'; + + iframeWrapper.appendChild(iframe); + + const fullscreenBtn = document.createElement('img'); + fullscreenBtn.src = FullscreenIcon; + fullscreenBtn.style.position = 'absolute'; + fullscreenBtn.style.background = 'none'; + fullscreenBtn.style.border = 'none'; + fullscreenBtn.style.cursor = 'pointer'; + fullscreenBtn.id = 'fullscreenBtn'; + fullscreenBtn.style.width = '35px'; + + fullscreenBtn.addEventListener('click', function () { + const botSrc = iframe.src; + if (botSrc) { + window.open(botSrc, '_blank'); + } + }); + + const switchBtn = document.createElement('img'); + switchBtn.src = SwitchIcon; + switchBtn.style.position = 'absolute'; + switchBtn.style.background = 'none'; + switchBtn.style.border = 'none'; + switchBtn.style.cursor = 'pointer'; + switchBtn.id = 'switchBtn'; + switchBtn.style.width = '35px'; + + switchBtn.addEventListener('click', function () { + chrome.storage.local.get(["configs", "chatbotSrc",], function (result) { + const configs = result.configs || []; + // 创建或更新列表容器 + let listWrapper = document.getElementById('configList'); + if (listWrapper) { + iframeWrapper.removeChild(listWrapper); + return; + } + listWrapper = document.createElement('div'); + listWrapper.id = 'configList'; + listWrapper.className = 'ant-dropdown-menu'; + listWrapper.style.position = 'absolute'; + listWrapper.style.zIndex = '2147483647'; + listWrapper.style.backgroundColor = '#fff'; + listWrapper.style.border = '1px solid #ccc'; + listWrapper.style.borderRadius = '4px'; + listWrapper.classList.add('ant-dropdown', 'ant-dropdown-placement-bottomRight'); + const switchBtnRect = switchBtn.getBoundingClientRect(); + const iframeWrapperRect = iframeWrapper.getBoundingClientRect(); + const switchBtnOffsetRight = iframeWrapperRect.right - switchBtnRect.right; + const switchBtnOffsetTop = switchBtnRect.top - iframeWrapperRect.top; + // 确保listWrapper存在并调整其位置 + listWrapper.style.right = switchBtnOffsetRight + 'px'; + listWrapper.style.top = (switchBtnOffsetTop + switchBtn.offsetHeight) + 'px'; + listWrapper.style.padding = '5px'; + // 显示所有chatbot名称 + configs.forEach((config) => { + const item = document.createElement('div'); + item.textContent = config.name; + item.className = 'ant-dropdown-menu-item'; // 使用 Ant Design 的类名 + item.style.cursor = 'pointer'; + item.style.padding = '5px 16px'; + item.style.borderRadius = '4px'; + // 设置默认样式 + item.style.position = 'relative'; + item.style.lineHeight = '22px'; + item.style.color = '#606266'; + item.style.fontSize = '14px'; + item.style.whiteSpace = 'nowrap'; + item.style.textAlign = 'left'; + item.style.boxSizing = 'border-box'; + item.style.background = '#fff'; + item.style.borderBottomColor = '#e8eaec'; + item.style.borderBottomStyle = 'solid'; + item.style.borderBottomWidth = '1px'; + + // 设置选中样式 + if (config.url === result.chatbotSrc) { + item.style.color = '#1890ff'; + item.style.fontWeight = 'bold'; + item.style.background = '#e6f7ff'; + } + + // 为每个列表项添加点击事件监听器 + item.addEventListener('click', function () { + // 更新样式,移除其他项的蓝色 + const items = listWrapper.querySelectorAll('.ant-dropdown-menu-item'); + items.forEach((i) => { + i.style.color = '#606266'; + i.style.fontWeight = 'normal'; + i.style.background = '#fff'; + }); + // 设置当前项为蓝色 + item.style.color = '#1890ff'; + item.style.fontWeight = 'bold'; + item.style.background = '#e6f7ff'; + + // 更新chatbotSrc的值 + chrome.storage.local.set({chatbotSrc: config.url}, function () { + console.log('Updated chatbotSrc:', config.url); + }); + + // 更新iframe的src + loadChatBotIframe(document.getElementById(chatWindowWrapperId)); + }); + + listWrapper.appendChild(item); + }); + + // 将列表容器添加到body中或确保它已经存在 + if (!iframeWrapper.contains(listWrapper)) { + iframeWrapper.appendChild(listWrapper); + } + }); + }); + + document.body.appendChild(iframeWrapper); + + let chatBtnDragged = false; + let chatBtnDown = false; + let chatBtnMouseX; + let chatBtnMouseY; + + ChatBtn.addEventListener('click', function () { + if (chatBtnDragged) { + chatBtnDragged = false; + return; + } + const chatWindow = document.getElementById(chatWindowWrapperId); + if (!chatWindow) return; + + const visibilityVal = chatWindow.style.visibility; + if (visibilityVal === 'hidden') { + ChatBtnDiv.src = CloseIcon; + loadChatBotIframe(chatWindow); + } else { + chatWindow.style.visibility = 'hidden'; + const tmpBtn = document.getElementById('fullscreenBtn'); + const tmpBtn1 = document.getElementById('switchBtn'); + const tmpEl = document.getElementById('configList'); + if (tmpBtn) { + chatWindow.removeChild(tmpBtn); + } + if (tmpBtn1) { + chatWindow.removeChild(tmpBtn1); + } + if (tmpEl) { + chatWindow.removeChild(tmpEl); + } + ChatBtnDiv.src = MessageIcon; + } + }); + + function loadChatBotIframe(chatWindow) { + chrome.storage.local.get(["chatbotSrc", "shareId", "chatId", "fastUID", "chatBotWidth", "chatBotHeight"], function (result) { + let botSrc = result.chatbotSrc; + if (!botSrc || botSrc === 'about:blank' || botSrc === '') { + console.log("Can't find botSrc"); + iframe.src = 'data:text/html;charset=utf-8,没有配置机器人地址'; + chatWindow.style.visibility = 'unset'; + return; + } + let fastUID = result.fastUID; + if (!fastUID || fastUID === '') { + fastUID = generateUUID(); + chrome.storage.local.set({ + fastUID: fastUID + }); + } + chrome.runtime.sendMessage({ + action: "startRequestInterception", + chatbotSrc: botSrc + }); + console.log('fastUID:', fastUID); + botSrc = botSrc + "&authToken=" + fastUID; + if (botSrc.includes(result.shareId)) { + botSrc = botSrc + "&chatId=" + result.chatId; + } + if (result.chatBotWidth && result.chatBotHeight) { + chatWindow.style.width = result.chatBotWidth + 'px'; + chatWindow.style.height = result.chatBotHeight + 'px'; + } else { + chatWindow.style.width = '400px'; + chatWindow.style.height = '700px'; + } + iframe.src = 'about:blank'; + iframe.onload = function () { + chatWindow.style.visibility = 'unset'; + } + adjustIframePosition(chatWindow); + enableResize(iframeWrapper); + setTimeout(() => { + iframe.src = botSrc; + iframe.onload = function () { + if (parseInt(chatWindow.style.width, 10) >= 900) { + fullscreenBtn.style.top = '13px'; + fullscreenBtn.style.right = '60px'; + switchBtn.style.top = '13px'; + switchBtn.style.right = '90px'; + } else { + fullscreenBtn.style.top = '6px'; + fullscreenBtn.style.right = '50px'; + switchBtn.style.top = '6px'; + switchBtn.style.right = '80px'; + } + chatWindow.appendChild(fullscreenBtn); + chatWindow.appendChild(switchBtn); + const tmpEl = document.getElementById('configList'); + if (tmpEl) { + chatWindow.removeChild(tmpEl); + } + }; + }, 100); + }); + } + + ChatBtn.addEventListener('mousedown', (e) => { + e.stopPropagation(); + + if (!chatBtnMouseX && !chatBtnMouseY) { + chatBtnMouseX = e.clientX; + chatBtnMouseY = e.clientY; + } + + chatBtnDown = true; + }); + + window.addEventListener('mousemove', throttle(handleMouseMove, 16)); // 60fps + window.addEventListener('mouseup', handleMouseUp); + + function handleMouseMove(e) { + e.stopPropagation(); + if (!canDrag || !chatBtnDown) return; + + chatBtnDragged = true; + const transformX = e.clientX - chatBtnMouseX; + const transformY = e.clientY - chatBtnMouseY; + + ChatBtn.style.transform = `translate3d(${transformX}px, ${transformY}px, 0)`; + + adjustIframePosition(document.getElementById(chatWindowWrapperId)); + } + + function handleMouseUp(e) { + chatBtnDown = false; + adjustIframePosition(document.getElementById(chatWindowWrapperId)); + + window.removeEventListener('mousemove', handleMouseMove); + } + + ChatBtn.appendChild(ChatBtnDiv); + document.body.appendChild(ChatBtn); + + function generateUUID() { + const randomString = 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function () { + const randomHex = (Math.random() * 16) | 0; + return randomHex.toString(16); + }); + + const timestamp = Date.now().toString(16); + + const extraRandom = (Math.random() * 1e16).toString(16); + + return `${randomString}-${timestamp}-${extraRandom}`; + } + + function adjustIframePosition(chatWindow) { + const chatBtnRect = ChatBtn.getBoundingClientRect(); + const chatBtnWidth = chatBtnRect.width; + const chatBtnHeight = chatBtnRect.height; + const chatBtnLeft = chatBtnRect.left; + const chatBtnTop = chatBtnRect.top; + + const screenWidth = window.innerWidth; + const screenHeight = window.innerHeight; + + const iframeWidth = parseInt(chatWindow.style.width, 10); + const iframeHeight = parseInt(chatWindow.style.height, 10); + + const iframeTopLeft = {x: chatBtnLeft - iframeWidth, y: chatBtnTop - iframeHeight}; + const iframeTopRight = {x: chatBtnLeft + chatBtnWidth, y: chatBtnTop - iframeHeight}; + const iframeBottomLeft = {x: chatBtnLeft - iframeWidth, y: chatBtnTop + chatBtnHeight}; + const iframeBottomRight = {x: chatBtnLeft + chatBtnWidth, y: chatBtnTop + chatBtnHeight}; + + let bestPosition = iframeTopLeft; + let bestDistance = Infinity; + + const positions = [iframeTopLeft, iframeTopRight, iframeBottomLeft, iframeBottomRight]; + positions.forEach(position => { + const distance = Math.sqrt(Math.pow(position.x, 2) + Math.pow(position.y, 2)); + + if (position.x + iframeWidth > screenWidth) { + position.x = screenWidth - iframeWidth; + } + if (position.x < 0) { + position.x = 0; + } + if (position.y + iframeHeight > screenHeight) { + position.y = screenHeight - iframeHeight; + } + if (position.y < 0) { + position.y = 0; + } + + if (distance < bestDistance) { + bestPosition = position; + bestDistance = distance; + } + }); + + chatWindow.style.left = `${bestPosition.x}px`; + chatWindow.style.top = `${bestPosition.y}px`; + } + + function throttle(func, limit) { + let lastFunc; + let lastRan; + return function () { + const context = this; + const args = arguments; + if (!lastRan) { + func.apply(context, args); + lastRan = Date.now(); + } else { + clearTimeout(lastFunc); + lastFunc = setTimeout(function () { + if ((Date.now() - lastRan) >= limit) { + func.apply(context, args); + lastRan = Date.now(); + } + }, limit - (Date.now() - lastRan)); + } + } + } + + function enableResize(iframeWrapper) { + let isResizing = false; + let lastDownX = 0; + let lastDownY = 0; + let resizeDirection = ''; + + // 创建八个调整大小的句柄 + const handles = ['nw-resize', 'ne-resize', 'sw-resize', 'se-resize', 'n-resize', 's-resize', 'w-resize', 'e-resize']; + const directions = ['tl', 'tr', 'bl', 'br', 't', 'b', 'l', 'r']; + + handles.forEach((cursorType, index) => { + const handle = createResizeHandle(cursorType); + iframeWrapper.appendChild(handle); + positionHandle(handle, directions[index]); + handle.addEventListener('mousedown', (e) => startResizing(e, directions[index])); + }); + + function createResizeHandle(cursorType) { + const handle = document.createElement('div'); + handle.style.width = '15px'; + handle.style.height = '15px'; + handle.style.background = 'transparent'; + handle.style.position = 'absolute'; + handle.style.cursor = cursorType; + return handle; + } + + function positionHandle(handle, direction) { + switch (direction) { + case 'tl': + handle.style.top = '0'; + handle.style.left = '0'; + break; + case 'tr': + handle.style.top = '0'; + handle.style.right = '0'; + break; + case 'bl': + handle.style.bottom = '0'; + handle.style.left = '0'; + break; + case 'br': + handle.style.bottom = '0'; + handle.style.right = '0'; + break; + case 't': + handle.style.top = '0'; + handle.style.left = '50%'; + handle.style.transform = 'translateX(-50%)'; + handle.style.width = `${iframeWrapper.offsetWidth - 30}px`; + handle.style.height = '3px'; + break; + case 'b': + handle.style.bottom = '0'; + handle.style.left = '50%'; + handle.style.transform = 'translateX(-50%)'; + handle.style.width = `${iframeWrapper.offsetWidth - 30}px`; + handle.style.height = '3px'; + break; + case 'l': + handle.style.top = '50%'; + handle.style.left = '0'; + handle.style.transform = 'translateY(-50%)'; + handle.style.width = '3px'; + handle.style.height = `${iframeWrapper.offsetHeight - 30}px`; + break; + case 'r': + handle.style.top = '50%'; + handle.style.right = '0'; + handle.style.transform = 'translateY(-50%)'; + handle.style.width = '3px'; + handle.style.height = `${iframeWrapper.offsetHeight - 30}px`; + break; + } + } + + function startResizing(e, direction) { + isResizing = true; + lastDownX = e.clientX; + lastDownY = e.clientY; + resizeDirection = direction; + iframeWrapper.style.pointerEvents = 'none'; // 禁用 iframe 的鼠标事件 + e.preventDefault(); + } + + document.addEventListener('mousemove', throttle(handleResizeMouseMove, 50)); + document.addEventListener('mouseup', handleResizeMouseUp); + + function handleResizeMouseMove(e) { + if (!isResizing) return; + + const offsetX = e.clientX - lastDownX; + const offsetY = e.clientY - lastDownY; + requestAnimationFrame(() => { + switch (resizeDirection) { + case 'tl': + iframeWrapper.style.width = `${iframeWrapper.offsetWidth - offsetX}px`; + iframeWrapper.style.height = `${iframeWrapper.offsetHeight - offsetY}px`; + iframeWrapper.style.left = `${iframeWrapper.offsetLeft + offsetX}px`; + iframeWrapper.style.top = `${iframeWrapper.offsetTop + offsetY}px`; + break; + case 'tr': + iframeWrapper.style.width = `${iframeWrapper.offsetWidth + offsetX}px`; + iframeWrapper.style.height = `${iframeWrapper.offsetHeight - offsetY}px`; + iframeWrapper.style.top = `${iframeWrapper.offsetTop + offsetY}px`; + break; + case 'bl': + iframeWrapper.style.width = `${iframeWrapper.offsetWidth - offsetX}px`; + iframeWrapper.style.height = `${iframeWrapper.offsetHeight + offsetY}px`; + iframeWrapper.style.left = `${iframeWrapper.offsetLeft + offsetX}px`; + break; + case 'br': + iframeWrapper.style.width = `${iframeWrapper.offsetWidth + offsetX}px`; + iframeWrapper.style.height = `${iframeWrapper.offsetHeight + offsetY}px`; + break; + case 't': + iframeWrapper.style.height = `${iframeWrapper.offsetHeight - offsetY}px`; + iframeWrapper.style.top = `${iframeWrapper.offsetTop + offsetY}px`; + break; + case 'b': + iframeWrapper.style.height = `${iframeWrapper.offsetHeight + offsetY}px`; + break; + case 'l': + iframeWrapper.style.width = `${iframeWrapper.offsetWidth - offsetX}px`; + iframeWrapper.style.left = `${iframeWrapper.offsetLeft + offsetX}px`; + break; + case 'r': + iframeWrapper.style.width = `${iframeWrapper.offsetWidth + offsetX}px`; + break; + } + lastDownX = e.clientX; + lastDownY = e.clientY; + if (parseInt(iframeWrapper.style.width, 10) >= 900) { + fullscreenBtn.style.top = '13px'; + fullscreenBtn.style.right = '60px'; + switchBtn.style.top = '13px'; + switchBtn.style.right = '90px'; + } else { + fullscreenBtn.style.top = '6px'; + fullscreenBtn.style.right = '50px'; + switchBtn.style.top = '6px'; + switchBtn.style.right = '80px'; + } + }); + } + + function handleResizeMouseUp() { + console.log('handleResizeMouseUp'); + if (isResizing) { + isResizing = false; // 将 isResizing 重置为 false + enableResize(iframeWrapper); + iframeWrapper.style.pointerEvents = 'auto'; // 恢复 iframe 的鼠标事件 + chrome.storage.local.set({ + chatBotWidth: iframeWrapper.offsetWidth, + chatBotHeight: iframeWrapper.offsetHeight + }); + } + } + } + } +}); diff --git a/projects/app/public/chrome_extension/fastgpt_agent/src/popup.html b/projects/app/public/chrome_extension/fastgpt_agent/src/popup.html new file mode 100644 index 000000000..f69f9bfe5 --- /dev/null +++ b/projects/app/public/chrome_extension/fastgpt_agent/src/popup.html @@ -0,0 +1,108 @@ + + + + + Chatbot Extension + + + + + +
+ + + + + +
+ + + + + diff --git a/projects/app/public/chrome_extension/fastgpt_agent/src/popup.js b/projects/app/public/chrome_extension/fastgpt_agent/src/popup.js new file mode 100644 index 000000000..32e11baf4 --- /dev/null +++ b/projects/app/public/chrome_extension/fastgpt_agent/src/popup.js @@ -0,0 +1,66 @@ +document.addEventListener('DOMContentLoaded', function () { + const chatbotIframe = document.getElementById('chatbot-iframe'); + const fullScreenBtn = document.getElementById('fullScreen'); + const configBtn = document.getElementById('config-btn'); + const overlay = document.getElementById('configOverlay'); + configBtn.addEventListener('click', function () { + window.location.href = 'setting.html' + }); + + // 监听 chatbotIframe 加载完成事件 + chatbotIframe.addEventListener('load', function() { + // 当 iframe 加载完成后显示按钮 + fullScreenBtn.style.display = 'inline-block'; + configBtn.style.display = 'inline-block'; + }); + chrome.storage.local.get(["chatbotSrc", "shareId", "chatId", "fastUID"]).then((result) => { + const botSrc = result.chatbotSrc; + let fastUID = result.fastUID; + if (!fastUID || fastUID === '') { + fastUID = generateUUID(); + chrome.storage.local.set({ + fastUID: fastUID + }); + } + console.log('fastUID is', fastUID); + console.log("chatbotSrc is " + botSrc); + console.log("shareId is " + result.shareId); + console.log("chatId is " + result.chatId); + chatbotIframe.src = result.chatbotSrc + "&authToken=" + fastUID; + if (!botSrc || botSrc === 'about:blank' || botSrc === '') { + overlay.style.display = 'flex'; + } else { + if (botSrc.includes(result.shareId)) { + chatbotIframe.src = chatbotIframe.src + "&chatId=" + result.chatId; + console.log('chatbotIframe.src', chatbotIframe.src); + } + overlay.style.display = 'none'; + chrome.runtime.sendMessage({ + action: "startRequestInterception", + chatbotSrc: botSrc + }); + } + }); + + fullScreenBtn.addEventListener('click', function () { + const iframe = document.getElementById('chatbot-iframe'); + if (iframe) { + const iframeSrc = iframe.src; + console.log('fullScreenSrc', iframeSrc) + chrome.tabs.create({url: iframeSrc}); + } + }); +}); + +function generateUUID() { + const randomString = 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, function () { + const randomHex = (Math.random() * 16) | 0; + return randomHex.toString(16); + }); + + const timestamp = Date.now().toString(16); + + const extraRandom = (Math.random() * 1e16).toString(16); + + return `${randomString}-${timestamp}-${extraRandom}`; +} diff --git a/projects/app/public/chrome_extension/fastgpt_agent/src/setting.html b/projects/app/public/chrome_extension/fastgpt_agent/src/setting.html new file mode 100644 index 000000000..d906659e2 --- /dev/null +++ b/projects/app/public/chrome_extension/fastgpt_agent/src/setting.html @@ -0,0 +1,382 @@ + + + + + + 配置项管理器 + + + + + +
+

ChatBot配置

+

页面机器人:

+
+
+ + +
+ +
+ + +
+ +
+ + +
+
+ +

机器人地址:

+ + + + + + + + + + +
名称地址操作选择BOT
+ + + \ No newline at end of file diff --git a/projects/app/public/chrome_extension/fastgpt_agent/src/setting.js b/projects/app/public/chrome_extension/fastgpt_agent/src/setting.js new file mode 100644 index 000000000..d2f68d537 --- /dev/null +++ b/projects/app/public/chrome_extension/fastgpt_agent/src/setting.js @@ -0,0 +1,283 @@ +let chatbotSrc = ''; +let configs = []; + +document.addEventListener('DOMContentLoaded', async function () { + const storageData = await chrome.storage.local.get(["chatbotSrc", "configs", "showChatBot", "chatBotWidth", "chatBotHeight"]); + chatbotSrc = storageData.chatbotSrc || ''; + configs = storageData.configs || []; + const showChatBot = storageData.showChatBot === undefined ? true : storageData.showChatBot; + const chatBotWidth = storageData.chatBotWidth || 400; + const chatBotHeight = storageData.chatBotHeight || 700; + + await loadConfigs(configs, chatbotSrc); + + document.getElementById('addConfigButton').addEventListener('click', handleAddButtonClick); + document.getElementById('startChatButton').addEventListener('click', () => window.location.href = 'popup.html'); + + // 监听开关和输入框变化事件 + const showChatBotSwitch = document.getElementById('showChatBotSwitch'); + const chatBotWidthInput = document.getElementById('chatBotWidthInput'); + const chatBotHeightInput = document.getElementById('chatBotHeightInput'); + + showChatBotSwitch.addEventListener('change', () => handleShowChatBotChange(showChatBotSwitch.checked)); + chatBotWidthInput.addEventListener('change', () => handleChatBotWidthChange(chatBotWidthInput.value)); + chatBotHeightInput.addEventListener('change', () => handleChatBotHeightChange(chatBotHeightInput.value)); + + // 初始化开关和输入框的值 + showChatBotSwitch.checked = showChatBot; + chatBotWidthInput.value = chatBotWidth; + chatBotHeightInput.value = chatBotHeight; + +}); + +async function loadConfigs(configs, chatbotSrc) { + const configList = document.getElementById('configList'); + configList.innerHTML = ''; + configs.forEach(config => { + const row = createConfigRow(config, chatbotSrc); + configList.appendChild(row); + }); +} + +function createConfigRow(config, chatbotSrc) { + const row = document.createElement('tr'); + row.innerHTML = ` + ${config.name} + ${config.url} + + + | + + + + + + `; + row.querySelector('.editButton').addEventListener('click', () => handleEditButtonClick(row, config)); + row.querySelector('.deleteButton').addEventListener('click', () => handleDeleteButtonClick(row, config)); + row.querySelector('.selectBot').addEventListener('change', () => handleSelectBotChange(config.url)); + return row; +} + + +async function handleDeleteButtonClick(row, config) { + const configList = document.getElementById('configList'); + const index = Array.from(configList.children).indexOf(row); + const selectedUrl = config.url; + if (selectedUrl === chatbotSrc) { + chatbotSrc = 'about:blank'; + await updateStorage('chatbotSrc', chatbotSrc); + await updateStorage('chatId', ''); + await updateStorage('shareId', ''); + } + configs.splice(index, 1); + await updateStorage('configs', configs); + row.remove(); +} + +async function handleSelectBotChange(url) { + chatbotSrc = url; + await updateStorage('chatbotSrc', chatbotSrc); + updateSelectedRadioButton(); +} + +function updateSelectedRadioButton() { + document.querySelectorAll('.selectBot').forEach(radio => { + if (radio.closest('tr').querySelector('td:nth-child(2)').textContent === chatbotSrc) { + radio.checked = true; + } else { + radio.checked = false; + } + }); +} + + +function showError(message) { + const errorMsg = document.getElementById('errorMsg'); + errorMsg.textContent = message; + errorMsg.style.opacity = 1; // 显示错误消息 + + // 设置一个定时器,在5秒后隐藏错误消息 + setTimeout(() => { + errorMsg.style.opacity = 0; // 隐藏错误消息 + }, 3000); +} + + +async function updateStorage(key, value) { + try { + await chrome.storage.local.set({[key]: value}); + console.log(`${key} 已更新: ${value}`); + } catch (error) { + console.error(`更新 ${key} 出错:`, error); + } +} + +function showEditControls(row, isNewConfig = false, config = null) { + const nameCell = row.querySelector('td:nth-child(1)'); + const urlCell = row.querySelector('td:nth-child(2)'); + const name = isNewConfig ? '' : config.name; + const url = isNewConfig ? '' : config.url; + + nameCell.innerHTML = ``; + urlCell.innerHTML = ``; + + const iconContainer = document.createElement('div'); + iconContainer.style.display = 'flex'; + iconContainer.style.flexDirection = 'column'; + iconContainer.style.position = 'absolute'; + iconContainer.style.top = '15px'; + iconContainer.style.left = '-5px'; + + const confirmIcon = document.createElement('span'); + confirmIcon.textContent = '√'; + confirmIcon.style.color = 'green'; + confirmIcon.style.cursor = 'pointer'; + confirmIcon.style.margin = '0 0 10px 0'; + confirmIcon.classList.add('icon-hover'); + confirmIcon.classList.add('confirmButton'); + confirmIcon.addEventListener('click', () => { + handleConfirmButtonClick(row, isNewConfig); + }); + + const cancelIcon = document.createElement('span'); + cancelIcon.textContent = 'X'; + cancelIcon.style.color = 'red'; + cancelIcon.style.cursor = 'pointer'; + cancelIcon.classList.add('icon-hover'); + cancelIcon.classList.add('cancelButton') + cancelIcon.addEventListener('click', () => { + handleCancelButtonClick(row, isNewConfig); + }); + + iconContainer.appendChild(confirmIcon); + iconContainer.appendChild(cancelIcon); + + const cell = row.querySelector('td:nth-child(3)'); + cell.insertBefore(iconContainer, cell.firstChild); +} + +function handleAddButtonClick() { + const newRow = document.createElement('tr'); + newRow.innerHTML = ` + + + + + + + `; + document.getElementById('configList').appendChild(newRow); + showEditControls(newRow, true); +} + +function handleEditButtonClick(row, config) { + disableAllEditButtons(); + showEditControls(row, false, config); +} + +function isConfigUnique(name, url, index) { + return !configs.some((config, i) => + i !== index && (config.name === name || config.url === url) + ); +} + +function checkConfig(name, url, index) { + let errorMsg = ''; + if (!name || !url) { + errorMsg = '名称和地址都是必填项。'; + } else if (!isConfigUnique(name, url, index)) { + errorMsg = '名称或地址不能重复。'; + } + return errorMsg; +} + +function handleConfirmButtonClick(row, isNewConfig = false) { + const name = row.querySelector('.editName').value; + const url = row.querySelector('.editUrl').value; + const index = isNewConfig ? -1 : Array.from(document.getElementById('configList').children).indexOf(row); + const errorMsg = checkConfig(name, url, index); + if (errorMsg) { + showError(errorMsg); + return; + } + + if (isNewConfig) { + const newConfig = {name, url}; + configs.push(newConfig); + updateStorage('configs', configs); + + } else { + const config = configs[index]; + config.name = name; + if (config.url === chatbotSrc) { + chatbotSrc = url; + updateStorage('chatId', ''); + updateStorage('shareId', ''); + chrome.runtime.sendMessage({ + action: "startRequestInterception", + chatbotSrc: chatbotSrc + }); + } + config.url = url; + updateStorage('configs', configs); + updateStorage('chatbotSrc', chatbotSrc); + } + loadConfigs(configs, chatbotSrc); + row.remove(); + enableAllEditButtons(); +} + + +function handleCancelButtonClick(row, isNewConfig = false) { + if (isNewConfig) { + row.remove(); + } else { + const index = Array.from(document.getElementById('configList').children).indexOf(row); + const config = configs[index]; + row.querySelector('td:nth-child(1)').textContent = config.name; + row.querySelector('td:nth-child(2)').textContent = config.url; + row.querySelector('.editButton').disabled = false; + // 移除编辑模式下的确认和取消按钮 + const iconContainer = row.querySelector('td:nth-child(3) > div'); + if (iconContainer) { + iconContainer.remove(); + } + } + enableAllEditButtons(); +} + +async function handleShowChatBotChange(showChatBot) { + await updateStorage('showChatBot', showChatBot); +} + +async function handleChatBotWidthChange(width) { + const parsedWidth = parseInt(width); + if (!isNaN(parsedWidth)) { + await updateStorage('chatBotWidth', parsedWidth); + } +} + +async function handleChatBotHeightChange(height) { + const parsedHeight = parseInt(height); + if (!isNaN(parsedHeight)) { + await updateStorage('chatBotHeight', parsedHeight); + } +} + +function disableAllEditButtons() { + const allEditButtons = document.querySelectorAll('.editButton'); + allEditButtons.forEach(button => { + button.disabled = true; + }); +} + +function enableAllEditButtons() { + const allEditButtons = document.querySelectorAll('.editButton'); + allEditButtons.forEach(button => { + button.disabled = false; + }); +} \ No newline at end of file