ChatGPT Auto Conversation

Automatically continue conversation with ChatGPT, generating new questions based on previous responses.

  1. // ==UserScript==
  2. // @name ChatGPT Auto Conversation
  3. // @name:ru ChatGPT Авторазговор
  4. // @namespace https://github.com/radik097/GPTAutoResponce
  5. // @version 1.3
  6. // @description Automatically continue conversation with ChatGPT, generating new questions based on previous responses.
  7. // @description:ru Автоматическое продолжение диалога с ChatGPT, генерируя вопросы на основе последних строк ответа.
  8. // @author Your Name
  9. // @match *://chat.openai.com/*
  10. // @match *://chatgpt.com/*
  11. // @require https://cdn.jsdelivr.net/npm/@kudoai/chatgpt.js@3.3.5/dist/chatgpt.min.js
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM.setValue
  14. // @grant GM.getValue
  15. // @license MIT
  16. // @supportURL https://github.com/your-repo/issues
  17. // @compatible firefox Работает корректно
  18. // @compatible chrome Работает корректно
  19. // @incompatible edge Не тестировался
  20. // ==/UserScript==
  21.  
  22.  
  23. (async () => {
  24. const chatgpt = window.chatgpt;
  25.  
  26. if (!chatgpt) {
  27. console.error('ChatGPT.js library is not loaded!');
  28. return;
  29. }
  30.  
  31. // Utility function to wait for a new message
  32. async function waitForResponse() {
  33. return new Promise((resolve) => {
  34. new MutationObserver((mutations, observer) => {
  35. if (document.querySelector('[data-message-author-role="assistant"]')) {
  36. observer.disconnect();
  37. resolve();
  38. }
  39. }).observe(document.body, { childList: true, subtree: true });
  40. });
  41. }
  42.  
  43. // Function to extract the last two lines of the response
  44. function extractLastTwoLines(text) {
  45. const lines = text.split('\n').filter(line => line.trim() !== '');
  46. const lastTwoLines = lines.slice(-2).join(' ');
  47. return lastTwoLines.trim();
  48. }
  49.  
  50. // Function to generate the next question based on the last two lines
  51. function generateNextQuestion(lastTwoLines) {
  52. return `Как ты думаешь об этом: "${lastTwoLines}"?`;
  53. }
  54.  
  55. // Main conversation loop
  56. async function startAutoConversation() {
  57. console.log('Starting automatic conversation...');
  58. while (true) {
  59. // Wait for the assistant's response
  60. await waitForResponse();
  61.  
  62. // Wait an additional 2 seconds after the response is received
  63. await new Promise((resolve) => setTimeout(resolve, 2000));
  64.  
  65. // Get the latest response text
  66. const assistantMessage = [...document.querySelectorAll('[data-message-author-role="assistant"]')].pop();
  67. const responseText = assistantMessage?.innerText || '';
  68.  
  69. if (!responseText) {
  70. console.error('Failed to retrieve assistant response.');
  71. break;
  72. }
  73.  
  74. // Extract the last two lines
  75. const lastTwoLines = extractLastTwoLines(responseText);
  76. console.log(`Last two lines: ${lastTwoLines}`);
  77.  
  78. // Generate the next question
  79. const nextQuestion = generateNextQuestion(lastTwoLines);
  80. console.log(`Next question: ${nextQuestion}`);
  81.  
  82. // Send the next question
  83. chatgpt.send(nextQuestion);
  84.  
  85. // Wait a few seconds before proceeding to avoid overwhelming the system
  86. await new Promise((resolve) => setTimeout(resolve, 4000));
  87. }
  88. }
  89.  
  90.  
  91.  
  92. // Wait for the ChatGPT page to load
  93. await chatgpt.isLoaded();
  94. console.log('ChatGPT page loaded.');
  95.  
  96. // Add a button to toggle automatic conversation
  97. const toggleButton = document.createElement('button');
  98. toggleButton.textContent = 'Start Auto Conversation';
  99. toggleButton.style.position = 'fixed';
  100. toggleButton.style.bottom = '20px';
  101. toggleButton.style.right = '20px';
  102. toggleButton.style.zIndex = '1000';
  103. toggleButton.style.padding = '10px';
  104. toggleButton.style.background = '#4CAF50';
  105. toggleButton.style.color = 'white';
  106. toggleButton.style.border = 'none';
  107. toggleButton.style.borderRadius = '5px';
  108. toggleButton.style.cursor = 'pointer';
  109.  
  110. document.body.appendChild(toggleButton);
  111.  
  112. let isRunning = false;
  113. toggleButton.addEventListener('click', () => {
  114. if (isRunning) {
  115. isRunning = false;
  116. toggleButton.textContent = 'Start Auto Conversation';
  117. console.log('Auto conversation stopped.');
  118. } else {
  119. isRunning = true;
  120. toggleButton.textContent = 'Stop Auto Conversation';
  121. startAutoConversation().catch((err) => {
  122. console.error('Error during auto conversation:', err);
  123. isRunning = false;
  124. toggleButton.textContent = 'Start Auto Conversation';
  125. });
  126. }
  127. });
  128. })();