Minimal ChatGPT Message Logger + Injector + Submitter

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

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

  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.20250513084659
  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.  
  32. const lines = event.data.split('\n');
  33. const html = lines
  34. .map(line => `<p>${line
  35. .replace(/&/g, '&amp;')
  36. .replace(/</g, '&lt;')
  37. .replace(/>/g, '&gt;')}</p>`)
  38. .join('');
  39. composer.innerHTML = html;
  40.  
  41. composer.dispatchEvent(new InputEvent('input', { bubbles: true }));
  42.  
  43. // Observe DOM changes until the send button is enabled
  44. const observer = new MutationObserver(function (mutations, obs) {
  45. const sendBtn = document.querySelector('[data-testid="send-button"]');
  46. if (sendBtn && !sendBtn.disabled) {
  47. obs.disconnect();
  48. console.log('Submitting message');
  49. sendBtn.click();
  50. }
  51. });
  52.  
  53. observer.observe(document, {
  54. childList: true,
  55. subtree: true,
  56. attributes: true,
  57. attributeFilter: ['disabled']
  58. });
  59. });
  60. })();