ChatGPT Native Injector (No Focus)

Inject postMessage into ChatGPT without stealing focus

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

  1. // ==UserScript==
  2. // @name ChatGPT Native Injector (No Focus)
  3. // @description Inject postMessage into ChatGPT without stealing focus
  4. // @match https://chatgpt.com/*
  5. // @version 0.0.1.20250512162359
  6. // @namespace https://greasyfork.org/users/1435046
  7. // ==/UserScript==
  8.  
  9. (function() {
  10. 'use strict';
  11.  
  12. // 1. Cache the native setter for .value
  13. const valueSetter = Object.getOwnPropertyDescriptor(
  14. HTMLTextAreaElement.prototype, 'value'
  15. ).set;
  16.  
  17. window.addEventListener('message', event => {
  18. if (typeof event.data !== 'string') return;
  19. const text = event.data.trim();
  20. if (!text) return;
  21.  
  22. // 2. Find ChatGPT’s hidden textarea (it mirrors the ProseMirror editor)
  23. const ta = document.querySelector('textarea.text-token-text-primary');
  24. if (!ta) return;
  25.  
  26. // 3. Update its value without focusing
  27. valueSetter.call(ta, text);
  28. ta.dispatchEvent(new InputEvent('input', { bubbles: true }));
  29.  
  30. // 4. Click Send if available
  31. const sendBtn = document.getElementById('composer-submit-button');
  32. if (sendBtn && !sendBtn.disabled) {
  33. sendBtn.click();
  34. }
  35. });
  36. })();