Minimal ChatGPT Message Logger + Injector + Submitter

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

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

  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.20250514133314
  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. if (event.data && event.data.type === 'reasonButtonClicked') {
  21. const reasonBtn = document.querySelector('[data-testid="composer-button-reason"]');
  22. if (reasonBtn) reasonBtn.click();
  23. return;
  24. }
  25.  
  26. // Only respond to string messages
  27. if (typeof event.data !== 'string') return;
  28.  
  29. // Locate ProseMirror composer
  30. const composer = document.querySelector('.ProseMirror');
  31. if (!composer) return;
  32.  
  33. console.log('Injecting message:', event.data);
  34.  
  35. // Focus and inject the text
  36. composer.focus();
  37.  
  38. const lines = event.data.split('\n');
  39. const html = lines
  40. .map(line => `<p>${line
  41. .replace(/&/g, '&amp;')
  42. .replace(/</g, '&lt;')
  43. .replace(/>/g, '&gt;')}</p>`)
  44. .join('');
  45. composer.innerHTML = html;
  46.  
  47. composer.dispatchEvent(new InputEvent('input', { bubbles: true }));
  48.  
  49. // Observe DOM changes until the send button is enabled
  50. const observer = new MutationObserver(function (mutations, obs) {
  51. const sendBtn = document.querySelector('[data-testid="send-button"]');
  52. if (sendBtn && !sendBtn.disabled) {
  53. obs.disconnect();
  54. console.log('Submitting message');
  55. sendBtn.click();
  56. }
  57. });
  58.  
  59. observer.observe(document, {
  60. childList: true,
  61. subtree: true,
  62. attributes: true,
  63. attributeFilter: ['disabled']
  64. });
  65. });
  66. })();