Insert Predefined Text

Inserts predefined text into a text box when the user presses enter or clicks okay

  1. ```javascript
  2. // ==UserScript==
  3. // @name Insert Predefined Text
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1
  6. // @description Inserts predefined text into a text box when the user presses enter or clicks okay
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Set the predefined text here
  15. var predefinedText = "This is the predefined text.";
  16.  
  17. // Add an event listener for keydown events
  18. document.addEventListener('keydown', function(event) {
  19. // Check if the key pressed was enter
  20. if (event.key === 'Enter') {
  21. // Get the active element (the one currently selected by the cursor)
  22. var activeElement = document.activeElement;
  23.  
  24. // Check if the active element is a text box
  25. if (activeElement.tagName === 'TEXTAREA' || (activeElement.tagName === 'INPUT' && activeElement.type === 'text')) {
  26. // Insert the predefined text into the text box with a space before it
  27. activeElement.value += ' ' + predefinedText;
  28. }
  29. }
  30. });
  31. })();
  32. ```