增加function approval button
This commit is contained in:
@@ -112,71 +112,58 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
// 滚动到最新消息
|
// 滚动到最新消息
|
||||||
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
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){
|
socket.on('request_function', function(data){
|
||||||
try {
|
try {
|
||||||
console.log("request_function", data);
|
console.log("request_function", data);
|
||||||
// 统一数据格式为数组
|
if (typeof data === 'string') {
|
||||||
const requests = typeof data === 'string' ? JSON.parse(data) : data;
|
data = JSON.parse(data);
|
||||||
const requestArray = Array.isArray(requests) ? requests : [requests];
|
}
|
||||||
|
if (typeof data === 'object') {
|
||||||
|
data = [data];
|
||||||
|
}
|
||||||
cleanupExpiredApprovals();// 先清理一次过期记录
|
cleanupExpiredApprovals();// 先清理一次过期记录
|
||||||
|
for (let i = 0; i < data.length; i++) {
|
||||||
const chatbox = getChatbox();
|
const element = data[i];
|
||||||
|
const funcName = element.data.name;
|
||||||
// 批量处理所有请求
|
|
||||||
requestArray.forEach(element => {
|
if (activeApprovals.hasOwnProperty(funcName)) {
|
||||||
const approveMessage = createApproveMessage(element);
|
// 如果存在,找到并移除之前的元素
|
||||||
chatbox.appendChild(approveMessage);
|
const existingElements = document.querySelectorAll(`.approve-message[data-function="${funcName}"]`);
|
||||||
});
|
existingElements.forEach(el => el.remove());
|
||||||
|
}
|
||||||
scrollChatboxToBottom();
|
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="bi bi-check-circle-fill"></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;
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error handling request_function:", error);
|
console.log(error)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
socket.on('next_chapter', function (data) {
|
socket.on('next_chapter', function (data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user