ChatGPT Custom Shortcuts

Add/replace some keyboard shortcuts for ChatGPT.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         ChatGPT Custom Shortcuts
// @namespace    http://tampermonkey.net/
// @version      2024-05-14
// @description  Add/replace some keyboard shortcuts for ChatGPT.
// @author       David
// @match        https://chat.openai.com/*
// @match        https://chatgpt.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=openai.com
// @grant        none
// @license MIT 
// ==/UserScript==

(function() {
  'use strict';

  document.addEventListener('keydown', function(event) {
    // Focus on textarea when pressing Ctrl + /
    if (event.ctrlKey && event.key === '/') {
      // Prevent default action and stop propagation
      event.preventDefault();
      event.stopPropagation();

      document.getElementById('prompt-textarea').focus();
      return;
    }

    // Check if the focused element is the textarea
    if (document.activeElement.id === 'prompt-textarea') {
      const textarea = document.getElementById('prompt-textarea');

      // create newline (Sending Shift + Enter) when pressing Enter alone
      if (event.key === 'Enter' && !event.ctrlKey && !event.shiftKey) {
        // Prevent default action and stop propagation
        event.preventDefault();
        event.stopPropagation();

        const value = textarea.value;
        const start = textarea.selectionStart;
        const end = textarea.selectionEnd;

        textarea.value = value.substring(0, start) + '\n' + value.substring(end);
        textarea.selectionStart = textarea.selectionEnd = start + 1;
        return;
      }

      // Click the send button when pressing Ctrl + Enter or Shift + Enter
      if ((event.ctrlKey || event.shiftKey) && event.key === 'Enter') {
        // Prevent default action and stop propagation
        event.preventDefault();
        event.stopPropagation();

        // Copy all text in the textarea to clipboard
        textarea.select();
        document.execCommand('copy');

        // click send button
        // document.querySelector('[data-testid="send-button"]').click();
        const sendButton = document.querySelector('button.mb-1.mr-1.flex.h-8.w-8.items-center.justify-center.rounded-full.bg-black.text-white');
        sendButton.click();
      }
    }

    // Prevent the default action of Ctrl + R
    if (event.ctrlKey && event.key === 'r') {
      // Prevent default action and stop propagation
      event.preventDefault();
      event.stopPropagation();
    }

  }, true); // Using capturing phase

})();