Minimal ChatGPT Message Logger + Injector + Submitter

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

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

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