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

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

当前为 2024-02-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Fix Home, End, Page Up and Page Down keys for OpenAI broken chats
  3. // @description For some 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.1
  7. // @license MIT
  8. // @match https://chat.openai.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. document.addEventListener('keydown', function(event) {
  16. const scrollableContainer = Array.from(document.querySelectorAll('div')).find(div => /^react-scroll-to-bottom--css-\S+$/.test(div.className));
  17.  
  18. if (scrollableContainer) {
  19. if (event.key === 'Home') {
  20. event.preventDefault();
  21. scrollableContainer.scroll(0, 0);
  22. } else if (event.key === 'End') {
  23. event.preventDefault();
  24. scrollableContainer.scroll(0, scrollableContainer.scrollHeight);
  25. } else if (event.key === 'PageUp') {
  26. event.preventDefault();
  27. const scrollAmount = scrollableContainer.clientHeight * 0.75;
  28. scrollableContainer.scrollBy(0, -scrollAmount);
  29. } else if (event.key === 'PageDown') {
  30. event.preventDefault();
  31. const scrollAmount = scrollableContainer.clientHeight * 0.75;
  32. scrollableContainer.scrollBy(0, scrollAmount);
  33. }
  34. } else {
  35. console.error("No scrollable container found with the specified class pattern.");
  36. }
  37. });
  38. })();