const SESSION_OBSERVER_KEY = 'enhancedChat_observerArray';
let observerArray = JSON.parse(sessionStorage.getItem(SESSION_OBSERVER_KEY)) || [];
const DEBOUNCE_WAIT = 300;
initializeObservers();
function captureMessages() {
const messageWrap = document.querySelector('.bubble-wrap.svelte-1e1jlin');
if (!messageWrap) {
console.error("Message wrap not found.");
return;
}
const messages = Array.from(messageWrap.querySelectorAll('.message-row')).map(div => ({
htmlContent: div.outerHTML,
type: div.classList.contains('user-row') ? 'user' : 'bot'
}));
mergeNewMessagesIntoObserverArray(messages);
storeChatHistory();
}
function mergeNewMessagesIntoObserverArray(newMessages) {
newMessages.forEach(msg => {
if (!observerArray.some(existingMsg => existingMsg.htmlContent === msg.htmlContent)) {
observerArray.push(msg);
}
});
console.log('Observer array updated:', observerArray);
}
function storeChatHistory() {
sessionStorage.setItem(SESSION_OBSERVER_KEY, JSON.stringify(observerArray));
console.clear();
console.log('Stored Observer State:', observerArray);
}
function saveChatHistoryToFile() {
if (typeof JSZip === 'undefined' || observerArray.length === 0) {
console.error('JSZip is not loaded or chat history is empty.');
return;
}
const zip = new JSZip();
zip.file('chat_history.txt', observerArray.map(msg => extractTextFromHTML(msg.htmlContent)).join('\n'));
zip.generateAsync({ type: 'blob' }).then(content => {
const blobURL = URL.createObjectURL(content);
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const a = document.createElement('a');
a.href = blobURL;
a.download = `chat_history_${timestamp}.zip`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
console.log('ZIP file created and downloaded successfully!');
});
}
function copyChatHistoryToClipboard() {
const chatContent = observerArray.map(msg => extractTextFromHTML(msg.htmlContent)).join('\n');
navigator.clipboard.writeText(chatContent).then(() => {
console.log('Observer chat copied to clipboard.');
}).catch(err => {
console.error('Failed to copy chat history:', err);
});
}
function extractTextFromHTML(htmlContent) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = htmlContent;
return tempDiv.textContent;
}
function copySpecificMessagesToClipboard(type) {
const filteredMessages = observerArray
.filter(msg => msg.type === type)
.map(msg => extractTextFromHTML(msg.htmlContent));
const messageString = filteredMessages.join('\n');
navigator.clipboard.writeText(messageString).then(() => {
console.log(`Copied ${type} messages to clipboard.`);
}).catch(err => {
console.error(`Failed to copy ${type} messages to clipboard:`, err);
});
}
function handleCommand() {
const inputBox = document.querySelector('textarea[data-testid="textbox"]');
if (inputBox) {
const command = inputBox.value.trim().toLowerCase();
switch (command) {
case 'sve':
saveChatHistoryToFile();
break;
case 'svr':
copyChatHistoryToClipboard();
break;
case 'svu':
copySpecificMessagesToClipboard('user');
break;
case 'svb':
copySpecificMessagesToClipboard('bot');
break;
default:
console.log("Unknown command.");
break;
}
inputBox.value = ''; // Clear input box after handling command
}
}
function initializeObservers() {
const messageWrap = document.querySelector('.bubble-wrap.svelte-1e1jlin');
if (messageWrap) {
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === "childList") {
captureMessagesDebounced();
}
});
});
observer.observe(messageWrap, { childList: true, subtree: true });
const inputBox = document.querySelector('textarea[data-testid="textbox"]');
if (inputBox) {
inputBox.addEventListener('input', handleCommand);
}
} else {
console.error("Message wrap not found, retrying...");
setTimeout(initializeObservers, 1000);
}
}
const captureMessagesDebounced = debounce(captureMessages, DEBOUNCE_WAIT);
function debounce(func, wait) {
let timeout;
return function (...args) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
}
})();
need to enwire console into handlablaments and remove from handlablaments these manual commands through original chat input.
need independent console to not activate if focused inside original input, only outside with "`" key.
To integrate the console functionality from the OverlayConsole script with the command handling in the EnhancedChatment script, while removing manual command input via the original chat input, we should modify and combine them. Here's how you can achieve this:
Remove the command handling from the EnhancedChatment script that's related to the original input field (textarea[data-testid="textbox"]).
Integrate the command handling logic into the OverlayConsole to execute commands directly from the custom console.
The modified and combined script can look like the following:
javascript
// ==UserScript==
// @name CombinedOverlayConsole
// @version 2.0
// @description Toggle a console overlay and manage chat history with commands
// @match *://yuntian-deng-chatgpt4.hf.space/*
// @grant GM_addStyle
// @require https://cdn.jsdelivr.net/npm/jszip@3.10.1/dist/jszip.min.js
// ==/UserScript==
/* global JSZip */
(function () {
'use strict';
const SESSION_OBSERVER_KEY = 'combinedOverlay_observerArray';
let observerArray = JSON.parse(sessionStorage.getItem(SESSION_OBSERVER_KEY)) || [];
const consoleOpen = false;
const consoleDiv = document.createElement('div');
// Set up the console overlay style
consoleDiv.id = 'consoleOverlay';
consoleDiv.style.cssText = `
position: fixed; bottom: 0; left: 0;
width: 100%; height: 200px; color: white; background: rgba(0, 0, 0, 0.9);
overflow-y: auto; z-index: 9999; display: none; font-family: monospace; padding: 10px;
`;
const inputBox = document.createElement('input');
inputBox.style.cssText = `
width: calc(100% - 20px); padding: 8px; margin-top: 10px; color: white;
background: #333; border: 1px solid #555; box-sizing: border-box;
`;
consoleDiv.appendChild(inputBox);
// Append consoleDiv to body
document.body.appendChild(consoleDiv);
function logMessage(message) {
const log = document.createElement('div');
log.textContent = message;
log.style.cssText = 'padding: 3px 0;'
consoleDiv.insertBefore(log, inputBox);
}
inputBox.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
handleConsoleCommand(inputBox.value);
inputBox.value = '';
}
});
function handleConsoleCommand(command) {
command = command.trim().toLowerCase();
switch (command) {
case 'sve':
saveChatHistoryToFile();
logMessage('Save chat history.');
break;
case 'svr':
copyChatHistoryToClipboard();
logMessage('Copy all chat history to clipboard.');
break;
case 'svu':
copySpecificMessagesToClipboard('user');
logMessage('Copy user messages to clipboard.');
break;
case 'svb':
copySpecificMessagesToClipboard('bot');
logMessage('Copy bot messages to clipboard.');
break;
default:
logMessage("Unknown command: " + command);
break;
}
}
window.addEventListener('keydown', function (e) {
if (e.key === '`' && document.activeElement !== inputBox) {
consoleOpen = !consoleOpen;
consoleDiv.style.display = consoleOpen ? 'block' : 'none';
if (consoleOpen) inputBox.focus();
}
});
logMessage("Console ready. Press ` to toggle.");
function initializeObservers() {
const messageWrap = document.querySelector('.bubble-wrap.svelte-1e1jlin');
if (messageWrap) {
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === "childList") {
captureMessagesDebounced();
}
});
});
observer.observe(messageWrap, { childList: true, subtree: true });
} else {
console.error("Message wrap not found, retrying...");
setTimeout(initializeObservers, 1000);
}
}
function captureMessages() {
const messageWrap = document.querySelector('.bubble-wrap.svelte-1e1jlin');
if (!messageWrap) {
console.error("Message wrap not found.");
return;
}
const messages = Array.from(messageWrap.querySelectorAll('.message-row')).map(div => ({
htmlContent: div.outerHTML,
type: div.classList.contains('user-row') ? 'user' : 'bot'
}));
mergeNewMessagesIntoObserverArray(messages);
storeChatHistory();
}
function mergeNewMessagesIntoObserverArray(newMessages) {
newMessages.forEach(msg => {
if (!observerArray.some(existingMsg => existingMsg.htmlContent === msg.htmlContent)) {
observerArray.push(msg);
}
});
console.log('Observer array updated:', observerArray);
}
function storeChatHistory() {
sessionStorage.setItem(SESSION_OBSERVER_KEY, JSON.stringify(observerArray));
console.log('Stored Observer State:', observerArray);
}
function saveChatHistoryToFile() {
if (typeof JSZip === 'undefined' || observerArray.length === 0) {
console.error('JSZip is not loaded or chat history is empty.');
return;
}
const zip = new JSZip();
zip.file('chat_history.txt', observerArray.map(msg => extractTextFromHTML(msg.htmlContent)).join('\n'));
zip.generateAsync({ type: 'blob' }).then(content => {
const blobURL = URL.createObjectURL(content);
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const a = document.createElement('a');
a.href = blobURL;
a.download = `chat_history_${timestamp}.zip`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
console.log('ZIP file created and downloaded successfully!');
});
}
function copyChatHistoryToClipboard() {
const chatContent = observerArray.map(msg => extractTextFromHTML(msg.htmlContent)).join('\n');
navigator.clipboard.writeText(chatContent).then(() => {
console.log('Observer chat copied to clipboard.');
}).catch(err => {
console.error('Failed to copy chat history:', err);
});
}
function extractTextFromHTML(htmlContent) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = htmlContent;
return tempDiv.textContent;
}
function copySpecificMessagesToClipboard(type) {
const filteredMessages = observerArray
.filter(msg => msg.type === type)
.map(msg => extractTextFromHTML(msg.htmlContent));
const messageString = filteredMessages.join('\n');
navigator.clipboard.writeText(messageString).then(() => {
console.log(`Copied ${type} messages to clipboard.`);
}).catch(err => {
console.error(`Failed to copy ${type} messages to clipboard:`, err);
});
}
const captureMessagesDebounced = debounce(captureMessages, 300);
function debounce(func, wait) {
let timeout;
return function (...args) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
}
initializeObservers();
})();
Key Changes
The command handling in the EnhancedChatment script now happens via the custom console in the OverlayConsole.
Removed dependency on the original chat input field for executing commands.
Commands are only processed when they are input in the custom overlay console.
Ensured the console is toggled independently and focuses only when not inside the original input field.
By following this approach, you integrate command handling directly into the overlay console ensuring a cleaner and more focused interface.