ChatGPT

A utils for ChatGPT.com

  1. // ==UserScript==
  2. // @name ChatGPT
  3. // @namespace yelfive
  4. // @version 2025-03-12
  5. // @description A utils for ChatGPT.com
  6. // @author You
  7. // @match https://chatgpt.com/c/*
  8. // @match https://chatgpt.com/
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=chatgpt.com
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. document.head.insertAdjacentHTML('beforeend', /*css*/`<style>
  17. body main div[data-message-author-role="user"]>div.w-full>div.relative {
  18. background: #6d6dff;
  19. color: white;
  20. }
  21. </style>`)
  22.  
  23. function saveInput() {
  24. const dom = document.querySelector('#prompt-textarea p');
  25. if (!dom) return;
  26. if (!dom.textContent) return;
  27. const input = dom.textContent;
  28.  
  29. localStorage.setItem('__input', input);
  30. }
  31.  
  32. // setInterval(saveInput, 500);
  33.  
  34. let lastIndex = null;
  35.  
  36. function getHistory() {
  37. const list = [...document.querySelectorAll('[data-message-author-role="user"]')].map(d => d.querySelector('.whitespace-pre-wrap').textContent);
  38. const unsubmitted = localStorage.getItem('__input');
  39. if (unsubmitted && unsubmitted !== list[list.length - 1]) list.push(unsubmitted);
  40. return list;
  41. }
  42. document.addEventListener('keyup', (ev) => {
  43. if (ev.target.id !== 'prompt-textarea') return;
  44. let input;
  45.  
  46. const dom = document.querySelector('#prompt-textarea p');
  47. if (ev.code === 'ArrowUp') {
  48. const _his = getHistory();
  49. if (lastIndex === null) lastIndex = _his.length;
  50. if (lastIndex > 0) {
  51. input = _his[--lastIndex];
  52. } else {
  53. return;
  54. }
  55. } else if (ev.code === 'ArrowDown') {
  56. const _his = getHistory();
  57. if (lastIndex === null) lastIndex = _his.length;
  58. if (lastIndex < _his.length - 1) {
  59. input = _his[++lastIndex]
  60. } else {
  61. return;
  62. }
  63. } else{
  64. saveInput();
  65. }
  66.  
  67. if (input) dom.innerHTML = input;
  68. });
  69. })();