function 提交
This commit is contained in:
@@ -112,58 +112,71 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
// 滚动到最新消息
|
||||
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
||||
});
|
||||
// 创建批准消息
|
||||
function createApproveMessage(element) {
|
||||
const funcName = element.data.name;
|
||||
|
||||
// 移除已存在的相同函数批准消息
|
||||
if (activeApprovals.hasOwnProperty(funcName)) {
|
||||
document.querySelectorAll(`.approve-message[data-function="${funcName}"]`).forEach(el => el.remove());
|
||||
}
|
||||
activeApprovals[funcName] = Date.now();
|
||||
|
||||
const requestMessage = document.createElement('div');
|
||||
requestMessage.className = 'chat-message server-message approve-message';
|
||||
requestMessage.setAttribute('data-function', funcName);
|
||||
|
||||
const description = document.createElement('div');
|
||||
const argsDescription = JSON.stringify(element.data.args);
|
||||
description.innerHTML = `<b>Function:</b> ${element.data.name}(${argsDescription || ''})`;
|
||||
|
||||
const approveButton = document.createElement('button');
|
||||
approveButton.innerHTML = '<i class="fas fa-check-circle"></i>';
|
||||
approveButton.data = element;
|
||||
approveButton.className = 'approve-button';
|
||||
approveButton.onclick = function (event) {
|
||||
socket.emit('message', JSON.stringify({ type: 'function', data: element }));
|
||||
event.currentTarget.disabled = true;
|
||||
event.currentTarget.classList.add('disabled');
|
||||
};
|
||||
|
||||
requestMessage.appendChild(description);
|
||||
requestMessage.appendChild(approveButton);
|
||||
|
||||
return requestMessage;
|
||||
}
|
||||
|
||||
// 获取聊天框元素
|
||||
function getChatbox() {
|
||||
return document.getElementById('chatbox');
|
||||
}
|
||||
|
||||
// 滚动聊天框到底部
|
||||
function scrollChatboxToBottom() {
|
||||
const chatbox = getChatbox();
|
||||
chatbox.scrollTop = chatbox.scrollHeight;
|
||||
}
|
||||
|
||||
socket.on('request_function', function(data){
|
||||
try {
|
||||
console.log("request_function", data);
|
||||
if (typeof data === 'string') {
|
||||
data = JSON.parse(data);
|
||||
}
|
||||
if (typeof data === 'object') {
|
||||
data = [data];
|
||||
}
|
||||
// 统一数据格式为数组
|
||||
const requests = typeof data === 'string' ? JSON.parse(data) : data;
|
||||
const requestArray = Array.isArray(requests) ? requests : [requests];
|
||||
|
||||
cleanupExpiredApprovals();// 先清理一次过期记录
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const element = data[i];
|
||||
const funcName = element.data.name;
|
||||
|
||||
if (activeApprovals.hasOwnProperty(funcName)) {
|
||||
// 如果存在,找到并移除之前的元素
|
||||
const existingElements = document.querySelectorAll(`.approve-message[data-function="${funcName}"]`);
|
||||
existingElements.forEach(el => el.remove());
|
||||
}
|
||||
activeApprovals[funcName] = Date.now();
|
||||
|
||||
const requestMessage = document.createElement('div');
|
||||
requestMessage.className = 'chat-message server-message approve-message';
|
||||
requestMessage.setAttribute('data-function', funcName);
|
||||
|
||||
const description = document.createElement('div');
|
||||
const argsDescription = JSON.stringify(element.data.args);
|
||||
if (argsDescription) {
|
||||
description.innerHTML = `<b>Function:</b> ${element.data.name}(${argsDescription})`;
|
||||
}else {
|
||||
description.innerHTML = `<b>Function:</b> ${element.data.name}()`;
|
||||
}
|
||||
|
||||
const approveButton = document.createElement('button');
|
||||
approveButton.innerHTML = '<i class="fas fa-check-circle-check"></i>';
|
||||
approveButton.data = element;
|
||||
approveButton.className = 'approve-button';
|
||||
approveButton.onclick = function (event) {
|
||||
console.log(element);
|
||||
socket.emit('message', JSON.stringify({ type: 'function', data: element }));
|
||||
event.currentTarget.disabled = true;
|
||||
event.currentTarget.classList.add('disabled');
|
||||
};
|
||||
requestMessage.appendChild(description);
|
||||
requestMessage.appendChild(approveButton);
|
||||
|
||||
document.getElementById('chatbox').appendChild(requestMessage);
|
||||
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
||||
}
|
||||
|
||||
|
||||
const chatbox = getChatbox();
|
||||
|
||||
// 批量处理所有请求
|
||||
requestArray.forEach(element => {
|
||||
const approveMessage = createApproveMessage(element);
|
||||
chatbox.appendChild(approveMessage);
|
||||
});
|
||||
|
||||
scrollChatboxToBottom();
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
console.error("Error handling request_function:", error);
|
||||
}
|
||||
});
|
||||
socket.on('next_chapter', function (data) {
|
||||
|
||||
Reference in New Issue
Block a user