ChatGPT Ctrl+Enter Fix

Send ChatGPT messages using Ctrl+Enter on smaller screen

  1. // ==UserScript==
  2. // @name ChatGPT Ctrl+Enter Fix
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Send ChatGPT messages using Ctrl+Enter on smaller screen
  6. // @author nonepork
  7. // @license MIT
  8. // @match https://chatgpt.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function waitForElement(selector, callback, checkFrequencyInMs, timeoutInMs) {
  16. let startTimeInMs = Date.now();
  17. const interval = setInterval(function() {
  18. const element = document.querySelector(selector);
  19. if (element) {
  20. clearInterval(interval);
  21. callback(element);
  22. return;
  23. }
  24. if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs) {
  25. clearInterval(interval);
  26. console.log('Element not found within timeout period');
  27. return;
  28. }
  29. }, checkFrequencyInMs || 100);
  30. }
  31.  
  32. function sendMessage() {
  33. const sendButton = document.querySelector('button[data-testid="send-button"]');
  34. if (sendButton && !sendButton.disabled) {
  35. sendButton.click();
  36. return true;
  37. }
  38. return false;
  39. }
  40.  
  41. function setupCtrlEnterHandler() {
  42. document.addEventListener('keydown', function(event) {
  43. if (event.ctrlKey && event.key === 'Enter') {
  44. event.preventDefault();
  45.  
  46. if (sendMessage()) {
  47. console.log('Message sent with Ctrl+Enter');
  48. } else {
  49. console.log('Send button not found or disabled');
  50. waitForElement('button[data-testid="send-button"]', function(element) {
  51. if (element && !element.disabled) {
  52. element.click();
  53. console.log('Message sent with Ctrl+Enter (delayed)');
  54. }
  55. }, 100, 5000);
  56. }
  57. }
  58. });
  59. }
  60.  
  61. function initialize() {
  62. console.log('ChatGPT Ctrl+Enter Send script loaded');
  63. setupCtrlEnterHandler();
  64.  
  65. waitForElement('button[data-testid="send-button"]', function() {
  66. console.log('Send button found and ready');
  67. }, 100, 10000);
  68. }
  69.  
  70. initialize();
  71. })();