Minimal ChatGPT Message Logger + Injector + Submitter

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

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

  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.20250512174502
  6. // @namespace https://greasyfork.org/users/1435046
  7. // ==/UserScript==
  8.  
  9. (function () {
  10. 'use strict';
  11.  
  12. window.addEventListener('message', function (event) {
  13. // Handle search button clicks
  14. if (event.data && event.data.type === 'searchButtonClicked') {
  15. const searchBtn = document.querySelector('[data-testid="composer-button-search"]');
  16. if (searchBtn) searchBtn.click();
  17. return;
  18. }
  19.  
  20. // Only respond to string messages
  21. if (typeof event.data !== 'string') return;
  22.  
  23. // Locate ProseMirror composer
  24. const composer = document.querySelector('.ProseMirror');
  25. if (!composer) return;
  26.  
  27. console.log('Injecting message:', event.data);
  28.  
  29. // Focus and inject the text
  30. composer.focus();
  31. composer.innerText = event.data;
  32. composer.dispatchEvent(new InputEvent('input', { bubbles: true }));
  33.  
  34. // Observe DOM changes until the send button is enabled
  35. const observer = new MutationObserver(function (mutations, obs) {
  36. const sendBtn = document.querySelector('[data-testid="send-button"]');
  37. if (sendBtn && !sendBtn.disabled) {
  38. obs.disconnect();
  39. console.log('Submitting message');
  40. sendBtn.click();
  41. }
  42. });
  43.  
  44. observer.observe(document, {
  45. childList: true,
  46. subtree: true,
  47. attributes: true,
  48. attributeFilter: ['disabled']
  49. });
  50. });
  51. })();