ChatGPT Preset Text Button

Adds a button on chatgpt.com to insert preset text into the input area

  1. // ==UserScript==
  2. // @name ChatGPT Preset Text Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.6
  5. // @description Adds a button on chatgpt.com to insert preset text into the input area
  6. // @author
  7. // @match https://chatgpt.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Create the button
  16. var btn = document.createElement("button");
  17. btn.innerHTML = "Insert Preset Text";
  18. // Preset text content
  19. var presetText = "This is the preset text.";
  20.  
  21. // Set button styles
  22. btn.style.position = "fixed";
  23. btn.style.top = "50%";
  24. btn.style.right = "0px";
  25. btn.style.transform = "translateY(-50%)";
  26. btn.style.padding = "10px 15px";
  27. btn.style.backgroundColor = "#FFFFFF"; // White background
  28. btn.style.color = "#000000"; // Black text
  29. btn.style.fontSize = "14px";
  30. btn.style.fontWeight = "normal";
  31. btn.style.border = "1px solid #000000";
  32. btn.style.borderRadius = "5px 0 0 5px";
  33. btn.style.cursor = "pointer";
  34. btn.style.zIndex = "9999";
  35. btn.style.boxShadow = "0 0 5px rgba(0,0,0,0.3)";
  36.  
  37. // Add the button to the page
  38. document.body.appendChild(btn);
  39.  
  40. // Button click event
  41. btn.addEventListener("click", function() {
  42. // Get the input area element
  43. var inputDiv = document.getElementById("prompt-textarea");
  44. if (inputDiv && inputDiv.contentEditable === "true") {
  45. // Insert the preset text into the input area
  46. inputDiv.innerHTML = '<p>' + presetText + '</p>';
  47.  
  48. // Move the cursor to the end of the text
  49. var range = document.createRange();
  50. var sel = window.getSelection();
  51. range.selectNodeContents(inputDiv);
  52. range.collapse(false);
  53. sel.removeAllRanges();
  54. sel.addRange(range);
  55.  
  56. // Trigger input event to notify any listeners of the change
  57. var event = new Event('input', { bubbles: true });
  58. inputDiv.dispatchEvent(event);
  59. } else {
  60. alert("Input area not found!");
  61. }
  62. });
  63. })();