Greasy Fork镜像 支持简体中文。

ChatGPT: Fix Enter Key on Small Sizes

Userscript changes the mobile interface by restoring Ctrl+Enter or Enter as Submit, even when opening small, thin windows in the desktop environment.

  1. // ==UserScript==
  2. // @name ChatGPT: Fix Enter Key on Small Sizes
  3. // @namespace cvladan.com
  4. // @version 1.1
  5. // @description Userscript changes the mobile interface by restoring Ctrl+Enter or Enter as Submit, even when opening small, thin windows in the desktop environment.
  6. // @author Vladan Colovic
  7. // @match *://chat.openai.com/*
  8. // @run-at document-end
  9. // @require https://cdn.jsdelivr.net/gh/chatgptjs/chatgpt.js@bdc3e03cc0b1fbcfcc886022d5690880aa40442c/dist/chatgpt-1.7.6.min.js
  10. // @grant none
  11. // @license MIT
  12. // @created 2023-06-03
  13. // @updated 2023-06-03
  14. // ==/UserScript==
  15.  
  16. const selSubmit = 'form > div > div > button';
  17. const selNewChat = 'nav > div > a, div.sticky > button:last-of-type';
  18. const enableDefaultModeOnMobile = false;
  19.  
  20. // I need to ensure that ChatGPT is fully loaded to check selector validity
  21. //
  22. (async () => {
  23. await chatgpt.isLoaded()
  24.  
  25. btnSubmit = document.querySelector(selSubmit);
  26. btnNewChat = document.querySelector(selNewChat);
  27.  
  28. inputArea = chatgpt.getTextarea();
  29. inputArea.addEventListener('keydown', handleKeydown, true);
  30. })();
  31.  
  32. // Ensure that ChatGPT is fully loaded to check selector validity
  33. //
  34. function clickMouseOn(button) {
  35. rect = button?.getBoundingClientRect();
  36. if (!rect) {
  37. console.info("Nothing currently at: " + cssSelector);
  38. return;
  39. }
  40.  
  41. var event = new MouseEvent('click', {
  42. view: window, bubbles: true, cancelable: true,
  43. clientX: rect.left + (rect.width / 2),
  44. clientY: rect.top + (rect.height / 2) });
  45.  
  46. button.dispatchEvent(event);
  47. }
  48.  
  49. // Ensure that ChatGPT is fully loaded to check selector validity
  50. //
  51. function skipActions(event) {
  52. event.preventDefault();
  53. event.stopPropagation();
  54. event.stopImmediatePropagation();
  55. return false;
  56. }
  57.  
  58. // Handler for all key events
  59. //
  60. // Attention is paid to:
  61. // - does not interfere with possibly other Enter-key combinations
  62. // - so that, for example Alt-Enter will still do what it does now
  63. // - if a selector is incorrectly specified, the key is not occupied
  64. //
  65. function handleKeydown(event) {
  66.  
  67. var ignoreOtherListeners = false;
  68.  
  69. const isEnter = (event.key === 'Enter');
  70. const modKey = event.ctrlKey | (event.shiftKey << 1) | (event.altKey << 2) | (event.metaKey << 3);
  71.  
  72.  
  73. if (enableDefaultModeOnMobile) {
  74.  
  75. if (isEnter && modKey === 0) { // force enter, even on mobile
  76. clickMouseOn(btnSubmit);
  77. skipActions(event);
  78. return false;
  79. }
  80.  
  81. return true;
  82. }
  83.  
  84. if (isEnter && modKey === 0) {
  85. ignoreOtherListeners = true;
  86. }
  87.  
  88. if (btnNewChat && isEnter && modKey === 3) { // console.log('New Chat');
  89. clickMouseOn(btnNewChat);
  90. ignoreOtherListeners = true;
  91. }
  92.  
  93. if (btnSubmit && isEnter && modKey === 1) { // console.log('Send');
  94. clickMouseOn(btnSubmit);
  95. ignoreOtherListeners = true;
  96. }
  97.  
  98. if (ignoreOtherListeners) {
  99. skipActions(event);
  100. return false;
  101. }
  102.  
  103. return true;
  104. };

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址