Minimal ChatGPT Message Logger + Injector + Submitter

Listen for postMessage from parent, log to console, enter it into the chat input, and submit

当前为 2025-05-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Minimal ChatGPT Message Logger + Injector + Submitter
  3. // @description Listen for postMessage from parent, log to console, enter it into the chat input, and submit
  4. // @match https://chatgpt.com/*
  5. // @version 0.0.1.20250526031102
  6. // @namespace https://greasyfork.org/users/1435046
  7. // ==/UserScript==
  8.  
  9. (function () {
  10. 'use strict';
  11.  
  12. window.addEventListener('message', function (event) {
  13.  
  14. // Handle search button clicks
  15. if (event.data && event.data.type === 'searchButtonClicked') {
  16. const searchBtn1 = document.querySelector('[data-testid="composer-button-search"]');
  17.  
  18. const searchButton2 = document.getElementById('system-hint-button');
  19.  
  20. if (searchBtn1) {
  21. searchBtn1.click();
  22. console.log
  23. return;
  24. }
  25.  
  26. if (searchButton2) {
  27. searchButton2.focus(); // Sometimes needed to simulate a real user
  28. searchButton2.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }));
  29.  
  30.  
  31.  
  32. for (const div of document.querySelectorAll('[id^="radix-"] > div > div')) {
  33. if (div.textContent.trim() === 'Search the web') {
  34. div.click();
  35. break;
  36. }
  37. }
  38.  
  39. };
  40. return;
  41. }
  42.  
  43. if (event.data && event.data.type === 'reasonButtonClicked') {
  44. const reasonBtn1 = document.querySelector('[data-testid="composer-button-reason"]');
  45.  
  46. const reasonButton2 = document.getElementById('system-hint-button');
  47.  
  48. if (reasonBtn1) reasonBtn1.click();
  49.  
  50. if (reasonButton2) {
  51. reasonButton2.focus(); // Sometimes needed to simulate a real user
  52. reasonButton2.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }));
  53.  
  54. document.querySelector('[id^="radix-"] > div > div:nth-of-type(5)').click()
  55. }
  56. return;
  57. }
  58.  
  59. if (event.data && event.data.type === 'newChatButtonClicked') {
  60. // Click the "New chat" anchor link
  61. const newChatLink = document.querySelector('a[aria-label="New chat"][href="/"]');
  62. if (newChatLink) newChatLink.click();
  63. return;
  64. }
  65.  
  66. if (event.data.type !== 'prompt') return;
  67.  
  68. // Locate ProseMirror composer
  69. const composer = document.querySelector('.ProseMirror');
  70. if (!composer) return;
  71.  
  72. console.log('Injecting message:', event.data.content);
  73.  
  74. // Focus and inject the text
  75. composer.focus();
  76.  
  77. const lines = event.data.content.split('\n');
  78. const html = lines
  79. .map(line => `<p>${line
  80. .replace(/&/g, '&amp;')
  81. .replace(/</g, '&lt;')
  82. .replace(/>/g, '&gt;')}</p>`)
  83. .join('');
  84. composer.innerHTML = html;
  85.  
  86. composer.dispatchEvent(new InputEvent('input', { bubbles: true }));
  87.  
  88. // Observe DOM changes until the send button is enabled
  89. const observer = new MutationObserver(function (mutations, obs) {
  90. const sendBtn = document.querySelector('[data-testid="send-button"]');
  91. if (sendBtn && !sendBtn.disabled) {
  92. obs.disconnect();
  93. console.log('Submitting message');
  94. sendBtn.click();
  95. }
  96. });
  97.  
  98. observer.observe(document, {
  99. childList: true,
  100. subtree: true,
  101. attributes: true,
  102. attributeFilter: ['disabled']
  103. });
  104. });
  105. })();