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のページです。最新版はこちら

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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.4
// @license      MIT
// @match        https://chat.openai.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' });
  }
})();