Greasy Fork 还支持 简体中文。

Minimal ChatGPT Message Logger + Injector + Submitter

Listen for postMessage from parent, log to console, enter it into the chat input (preserving newlines), 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 (preserving newlines), and submit
  4. // @match https://chatgpt.com/*
  5. // @version 0.0.1.20250513083706
  6. // @namespace https://greasyfork.org/users/1435046
  7. // ==/UserScript==
  8.  
  9. (function () {
  10. 'use strict';
  11.  
  12. window.addEventListener('message', function (event) {
  13. // 1) 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. // 2) only strings from postMessage
  21. if (typeof event.data !== 'string') return;
  22.  
  23. // 3) find the ProseMirror editor
  24. const composer = document.querySelector('.ProseMirror');
  25. if (!composer) return;
  26.  
  27. console.log('Injecting message:', event.data);
  28.  
  29. // 4) build HTML paragraphs for each line
  30. const lines = event.data.split('\n');
  31. const html = lines
  32. .map(line => `<p>${line
  33. .replace(/&/g, '&amp;')
  34. .replace(/</g, '&lt;')
  35. .replace(/>/g, '&gt;')}</p>`)
  36. .join('');
  37.  
  38. // 5) inject and notify ProseMirror
  39. composer.focus();
  40. composer.innerHTML = html;
  41. composer.dispatchEvent(new InputEvent('input', { bubbles: true }));
  42.  
  43. // 6) wait for the send button to enable, then click it
  44. const observer = new MutationObserver((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. observer.observe(document, {
  53. childList: true,
  54. subtree: true,
  55. attributes: true,
  56. attributeFilter: ['disabled']
  57. });
  58. });
  59. })();