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.20250526024910
  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) searchBtn1.click();
  21.  
  22. return;
  23.  
  24. if (searchButton2) {
  25. searchButton2.focus(); // Sometimes needed to simulate a real user
  26. searchButton2.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }));
  27.  
  28. document.querySelector('[id^="radix-"] > div > div:nth-of-type(2)').click()
  29.  
  30. };
  31. return;
  32. }
  33.  
  34. if (event.data && event.data.type === 'reasonButtonClicked') {
  35. const reasonBtn1 = document.querySelector('[data-testid="composer-button-reason"]');
  36.  
  37. const reasonButton2 = document.getElementById('system-hint-button');
  38.  
  39. if (reasonBtn1) reasonBtn1.click();
  40.  
  41. if (reasonButton2) {
  42. reasonButton2.focus(); // Sometimes needed to simulate a real user
  43. reasonButton2.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }));
  44.  
  45. document.querySelector('[id^="radix-"] > div > div:nth-of-type(5)').click()
  46. }
  47. return;
  48. }
  49.  
  50. if (event.data && event.data.type === 'newChatButtonClicked') {
  51. // Click the "New chat" anchor link
  52. const newChatLink = document.querySelector('a[aria-label="New chat"][href="/"]');
  53. if (newChatLink) newChatLink.click();
  54. return;
  55. }
  56.  
  57. if (event.data.type !== 'prompt') return;
  58.  
  59. // Locate ProseMirror composer
  60. const composer = document.querySelector('.ProseMirror');
  61. if (!composer) return;
  62.  
  63. console.log('Injecting message:', event.data.content);
  64.  
  65. // Focus and inject the text
  66. composer.focus();
  67.  
  68. const lines = event.data.content.split('\n');
  69. const html = lines
  70. .map(line => `<p>${line
  71. .replace(/&/g, '&amp;')
  72. .replace(/</g, '&lt;')
  73. .replace(/>/g, '&gt;')}</p>`)
  74. .join('');
  75. composer.innerHTML = html;
  76.  
  77. composer.dispatchEvent(new InputEvent('input', { bubbles: true }));
  78.  
  79. // Observe DOM changes until the send button is enabled
  80. const observer = new MutationObserver(function (mutations, obs) {
  81. const sendBtn = document.querySelector('[data-testid="send-button"]');
  82. if (sendBtn && !sendBtn.disabled) {
  83. obs.disconnect();
  84. console.log('Submitting message');
  85. sendBtn.click();
  86. }
  87. });
  88.  
  89. observer.observe(document, {
  90. childList: true,
  91. subtree: true,
  92. attributes: true,
  93. attributeFilter: ['disabled']
  94. });
  95. });
  96. })();