// ==UserScript== // @name Sync between Sexy.AI and SillyTavern // @namespace http://tampermonkey.net/ // @version 2.2 // @description Enhanced integration between SillyTavern and Sexy.AI // @author You // @match https://sexy.ai/workflow* // @match https://staticui.sexy.ai/* // @match http://ducninh.top:8000/* // @match http://127.0.0.1:8000/* // @match http://*/*:8000/* // @grant GM_setValue // @grant GM_getValue // @grant unsafeWindow // @downloadURL none // ==/UserScript== (function() { 'use strict'; console.log("Script started on URL:", window.location.href); const isSexyAI = window.location.href.includes('sexy.ai') || window.location.href.includes('staticui.sexy.ai'); const isSillyTavern = window.location.href.includes(':8000'); // Utility Functions function createStyledButton(text, onClick, isFixed = false) { const button = document.createElement('button'); button.textContent = text; let baseStyle = ` padding: 5px 8px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; font-size: 12px; opacity: 0.8; transition: opacity 0.3s; z-index: 9999; `; if(isFixed) { baseStyle += ` position: fixed; right: 20px; `; } else { baseStyle += ` margin-left: 5px; `; } button.style.cssText = baseStyle; button.addEventListener('mouseover', () => button.style.opacity = '1'); button.addEventListener('mouseout', () => button.style.opacity = '0.8'); button.addEventListener('click', onClick); return button; } // SexyAI Implementation if (isSexyAI) { if (window.location.href.includes('staticui.sexy.ai')) { const promptButton = createStyledButton('Get Prompt', () => { const prompt = GM_getValue('st_prompt', null); if (prompt) { const positiveInput = document.querySelector('textarea') || document.querySelector('input[type="text"]'); if (positiveInput) { positiveInput.value = prompt; const event = new Event('input', { bubbles: true }); positiveInput.dispatchEvent(event); GM_setValue('st_prompt', null); alert('Prompt added!'); } else { console.error('Available inputs:', document.querySelectorAll('input, textarea')); alert('Input field not found.'); } } else { alert('No prompt found. Copy from SillyTavern first.'); } }, true); promptButton.style.top = '80px'; document.body.appendChild(promptButton); } document.addEventListener('click', (e) => { if (e.target.tagName === 'IMG') { const markdownUrls = [`![alt-text](${e.target.src})`]; console.log('Saving image URL:', markdownUrls[0]); // Debug log GM_setValue('sexyai_images', markdownUrls.join('\n')); alert('Image copied! Switch to SillyTavern tab.'); } }, true); } else if (isSillyTavern) { let isMonitoring = false; // Monitor Button Implementation function addMonitorButton() { const extensionsButton = document.querySelector('#extensionsMenuButton'); if (extensionsButton && !document.querySelector('#monitor_button')) { const monitorButton = document.createElement('div'); monitorButton.id = 'monitor_button'; monitorButton.className = 'fa-solid fa-eye menu_button'; monitorButton.title = 'Monitor Messages'; monitorButton.style.cssText = ` display: flex; cursor: pointer; opacity: 0.7; margin: 0 5px; font-size: 18px; `; monitorButton.addEventListener('click', () => { isMonitoring = !isMonitoring; monitorButton.style.color = isMonitoring ? '#4CAF50' : ''; monitorButton.style.opacity = isMonitoring ? '1' : '0.7'; if (isMonitoring) { processExistingMessages(); alert('Message monitoring activated!'); } else { // Remove existing buttons when monitoring is disabled document.querySelectorAll('.button-container').forEach(container => { container.remove(); }); alert('Message monitoring deactivated!'); } }); extensionsButton.parentNode.insertBefore(monitorButton, extensionsButton.nextSibling); } } function editMessageAndAddImage(messageNode, markdownUrls) { return new Promise((resolve, reject) => { try { console.log('Starting edit process...'); // Debug log // Click edit button const editButton = messageNode.querySelector('.mes_edit'); if (!editButton) { throw new Error('Edit button not found'); } console.log('Found edit button, clicking...'); // Debug log editButton.click(); // Wait for textarea to appear and update it setTimeout(() => { const textarea = document.getElementById('curEditTextarea'); if (!textarea) { reject(new Error('Textarea not found')); return; } console.log('Found textarea, updating value...'); // Debug log // Update textarea value textarea.value = textarea.value + '\n' + markdownUrls; textarea.dispatchEvent(new Event('input', { bubbles: true })); // Wait for value to be set then click confirm setTimeout(() => { const confirmButton = messageNode.querySelector('.mes_edit_done'); if (!confirmButton) { reject(new Error('Confirm button not found')); return; } console.log('Found confirm button, clicking...'); // Debug log confirmButton.click(); resolve(); }, 500); }, 500); } catch (error) { reject(error); } }); } function addButtonsToMessage(messageNode) { if (!isMonitoring) return; if (!messageNode || !messageNode.querySelector) return; const messageText = messageNode.querySelector('.mes_text'); if (!messageText) return; // Avoid adding buttons multiple times if (messageText.querySelector('.button-container')) return; const text = messageText.innerHTML; const hasImagePrompt = text.match(/image###([^#]+)###/); if (hasImagePrompt) { const buttonContainer = document.createElement('div'); buttonContainer.className = 'button-container'; buttonContainer.style.display = 'flex'; buttonContainer.style.gap = '5px'; buttonContainer.style.marginTop = '5px'; const syncButton = createStyledButton('📥 Sync Image', async () => { console.log('Sync button clicked'); // Debug log try { const markdownUrls = GM_getValue('sexyai_images', null); console.log('Retrieved markdownUrls:', markdownUrls); // Debug log if (!markdownUrls) { alert('No images found. Click an image in Sexy.AI first.'); return; } await editMessageAndAddImage(messageNode, markdownUrls); GM_setValue('sexyai_images', null); console.log('Image sync completed successfully'); // Debug log alert('Images added successfully!'); } catch (error) { console.error('Error in sync process:', error); alert('Error: ' + error.message); } }); const sendPromptButton = createStyledButton('📤 Send Prompt', () => { const text = messageText.textContent; const match = text.match(/image###([^#]+)###/); if (match) { const prompt = match[1].trim(); GM_setValue('st_prompt', prompt); messageText.innerHTML = messageText.innerHTML.replace(/(image###[^#]+)###/g, ''); alert('Prompt copied! Click "Get Prompt" in Sexy.AI tab'); } else { alert('No valid prompt found. Message should contain image###prompt###'); } }); buttonContainer.appendChild(syncButton); buttonContainer.appendChild(sendPromptButton); messageText.appendChild(buttonContainer); } } // Process existing messages function processExistingMessages() { const messages = document.getElementsByClassName('mes'); Array.from(messages).forEach(addButtonsToMessage); } // Initialize monitor button setInterval(addMonitorButton, 2000); // Watch for new messages const observer = new MutationObserver((mutations) => { if (!isMonitoring) return; mutations.forEach((mutation) => { mutation.addedNodes.forEach((node) => { if (node.nodeType === 1) { if (node.classList?.contains('mes')) { addButtonsToMessage(node); } const mesElements = node.getElementsByClassName('mes'); Array.from(mesElements).forEach(addButtonsToMessage); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); } })();