Fix Home, End, Page Up and Page Down keys for Superpower ChatGPT and OpenAI broken chats

For some Superpower ChatGPT and OpenAI chats the Home, End, Page Up and Page Down keys stop working, this script fixes the problem

  1. // ==UserScript==
  2. // @name Fix Home, End, Page Up and Page Down keys for Superpower ChatGPT and OpenAI broken chats
  3. // @description For some Superpower ChatGPT and OpenAI chats the Home, End, Page Up and Page Down keys stop working, this script fixes the problem
  4. // @author NWP
  5. // @namespace https://greasyfork.org/users/877912
  6. // @version 0.6
  7. // @license MIT
  8. // @match https://chat.openai.com/*
  9. // @match https://chatgpt.com/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15. document.addEventListener('keydown', function (event) {
  16. if (['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName)) {
  17. return;
  18. }
  19.  
  20. const scrollableContainer = Array.from(document.querySelectorAll('div')).find(div => /^react-scroll-to-bottom--css-\S+$/.test(div.className)) || // OpenAI
  21. document.querySelector('#conversation-inner-div'); // Superpower ChatGPT
  22.  
  23. if (!scrollableContainer) {
  24. console.error("No scrollable container found.");
  25. return;
  26. }
  27.  
  28. switch (event.key) {
  29. case 'Home':
  30. case 'End':
  31. scrollToEnds(event, scrollableContainer);
  32. break;
  33. case 'PageUp':
  34. case 'PageDown':
  35. scrollByPage(event, scrollableContainer);
  36. break;
  37. }
  38. });
  39.  
  40. function scrollToEnds(event, container) {
  41. event.preventDefault();
  42. const position = event.key === 'Home' ? 0 : container.scrollHeight;
  43. container.scrollTo({ top: position, behavior: 'instant' });
  44. }
  45.  
  46. function scrollByPage(event, container) {
  47. event.preventDefault();
  48. const amount = event.key === 'PageUp' ? -container.clientHeight * 0.75 : container.clientHeight * 0.75;
  49. container.scrollBy({ top: amount, behavior: 'instant' });
  50. }
  51. })();