Greasy Fork is available in English.

ChatGPT Custom Shortcuts

Add/replace some keyboard shortcuts for ChatGPT.

  1. // ==UserScript==
  2. // @name ChatGPT Custom Shortcuts
  3. // @namespace http://tampermonkey.net/
  4. // @version 2024-05-14
  5. // @description Add/replace some keyboard shortcuts for ChatGPT.
  6. // @author David
  7. // @match https://chat.openai.com/*
  8. // @match https://chatgpt.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=openai.com
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. document.addEventListener('keydown', function(event) {
  18. // Focus on textarea when pressing Ctrl + /
  19. if (event.ctrlKey && event.key === '/') {
  20. // Prevent default action and stop propagation
  21. event.preventDefault();
  22. event.stopPropagation();
  23.  
  24. document.getElementById('prompt-textarea').focus();
  25. return;
  26. }
  27.  
  28. // Check if the focused element is the textarea
  29. if (document.activeElement.id === 'prompt-textarea') {
  30. const textarea = document.getElementById('prompt-textarea');
  31.  
  32. // create newline (Sending Shift + Enter) when pressing Enter alone
  33. if (event.key === 'Enter' && !event.ctrlKey && !event.shiftKey) {
  34. // Prevent default action and stop propagation
  35. event.preventDefault();
  36. event.stopPropagation();
  37.  
  38. const value = textarea.value;
  39. const start = textarea.selectionStart;
  40. const end = textarea.selectionEnd;
  41.  
  42. textarea.value = value.substring(0, start) + '\n' + value.substring(end);
  43. textarea.selectionStart = textarea.selectionEnd = start + 1;
  44. return;
  45. }
  46.  
  47. // Click the send button when pressing Ctrl + Enter or Shift + Enter
  48. if ((event.ctrlKey || event.shiftKey) && event.key === 'Enter') {
  49. // Prevent default action and stop propagation
  50. event.preventDefault();
  51. event.stopPropagation();
  52.  
  53. // Copy all text in the textarea to clipboard
  54. textarea.select();
  55. document.execCommand('copy');
  56.  
  57. // click send button
  58. // document.querySelector('[data-testid="send-button"]').click();
  59. const sendButton = document.querySelector('button.mb-1.mr-1.flex.h-8.w-8.items-center.justify-center.rounded-full.bg-black.text-white');
  60. sendButton.click();
  61. }
  62. }
  63.  
  64. // Prevent the default action of Ctrl + R
  65. if (event.ctrlKey && event.key === 'r') {
  66. // Prevent default action and stop propagation
  67. event.preventDefault();
  68. event.stopPropagation();
  69. }
  70.  
  71. }, true); // Using capturing phase
  72.  
  73. })();