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

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

  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.3
  7. // @license MIT
  8. // @match https://chat.openai.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14. document.addEventListener('keydown', function (event) {
  15. if (['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName)) {
  16. return;
  17. }
  18.  
  19. const scrollableContainer = Array.from(document.querySelectorAll('div')).find(div => /^react-scroll-to-bottom--css-\S+$/.test(div.className)) || // OpenAI
  20. document.querySelector('#conversation-inner-div'); // Superpower ChatGPT
  21.  
  22. if (!scrollableContainer) {
  23. console.error("No scrollable container found.");
  24. return;
  25. }
  26.  
  27. switch (event.key) {
  28. case 'Home':
  29. case 'End':
  30. scrollToEnds(event, scrollableContainer);
  31. break;
  32. case 'PageUp':
  33. case 'PageDown':
  34. scrollByPage(event, scrollableContainer);
  35. break;
  36. }
  37. });
  38.  
  39. function scrollToEnds(event, container) {
  40. event.preventDefault();
  41. const position = event.key === 'Home' ? 0 : container.scrollHeight;
  42. container.scrollTo({ top: position, behavior: 'instant' });
  43. }
  44.  
  45. function scrollByPage(event, container) {
  46. event.preventDefault();
  47. const amount = event.key === 'PageUp' ? -container.clientHeight * 0.75 : container.clientHeight * 0.75;
  48. container.scrollBy({ top: amount, behavior: 'instant' });
  49. }
  50. })();