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

ติดตั้งสคริปต์นี้?
สคริปต์ที่แนะนำของผู้เขียน

คุณอาจชื่นชอบ Keyboard shortcut for switching between Chat GPT 4o mini and Chat GPT 4o

ติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Fix Home, End, Page Up and Page Down keys for Superpower ChatGPT and OpenAI broken chats
// @description  For some Superpower ChatGPT and OpenAI chats the Home, End, Page Up and Page Down keys stop working, this script fixes the problem
// @author       NWP
// @namespace    https://greatest.deepsurf.us/users/877912
// @version      0.6
// @license      MIT
// @match        https://chat.openai.com/*
// @match        https://chatgpt.com/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  
  document.addEventListener('keydown', function (event) {
    if (['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName)) {
      return;
    }

    const scrollableContainer = Array.from(document.querySelectorAll('div')).find(div => /^react-scroll-to-bottom--css-\S+$/.test(div.className)) || // OpenAI
                                document.querySelector('#conversation-inner-div'); // Superpower ChatGPT

    if (!scrollableContainer) {
      console.error("No scrollable container found.");
      return;
    }

    switch (event.key) {
      case 'Home':
      case 'End':
        scrollToEnds(event, scrollableContainer);
        break;
      case 'PageUp':
      case 'PageDown':
        scrollByPage(event, scrollableContainer);
        break;
    }
  });

  function scrollToEnds(event, container) {
    event.preventDefault();
    const position = event.key === 'Home' ? 0 : container.scrollHeight;
    container.scrollTo({ top: position, behavior: 'instant' });
  }

  function scrollByPage(event, container) {
    event.preventDefault();
    const amount = event.key === 'PageUp' ? -container.clientHeight * 0.75 : container.clientHeight * 0.75;
    container.scrollBy({ top: amount, behavior: 'instant' });
  }
})();