ChatGPT Keep-Alive

每隔30秒自动发送一次请求到ChatGPT,防止出现错误提示: "Something went wrong. If this issue persists, please contact us through our helper center at help.openai.com."

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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                ChatGPT Keep-Alive
// @description         每隔30秒自动发送一次请求到ChatGPT,防止出现错误提示: "Something went wrong. If this issue persists, please contact us through our helper center at help.openai.com."
// @version             0.2
// @author              eiyen
// @namespace           https://github.com/eiyen/ChatGPT-Keep-Alive
// @description:zh-CN   每隔30秒自动发送一次请求到ChatGPT,防止出现错误提示: "Something went wrong. If this issue persists, please contact us through our helper center at help.openai.com."
// @description:en      Automatically ping ChatGPT every 30 seconds to prevent the error message: "Something went wrong. If this issue persists, please contact us through our helper center at help.openai.com."
// @match               https://chat.openai.com/*
// @license             MIT
// ==/UserScript==

(() => {
  "use strict";

  // Helper function to check if user is on the chat page
  const isUserOnChatPage = () => {
    const navSelector = "nav>a.flex";
    const buttonSelector = "button.justify-center";

    return (
      document.querySelector(navSelector) ||
      document.querySelector(buttonSelector)
    );
  };

  // Function to ping ChatGPT API every 30 seconds
  const pingChatGPT = () => {
    const apiEndpoint = `/api/auth/session`;

    // Request session data from the API
    fetch(apiEndpoint)
      .then((response) => {
        if (!response.ok) {
          const currentTime = new Date().toLocaleString();
          throw new Error(`Network response was not ok. Error occurred at: ${currentTime}`);
        }
      })
      .catch((error) => {
        console.log(`Error: ${error}`);
      });
  };

  // Main function to initialize the script
  const main = () => {
    setInterval(() => {
      if (isUserOnChatPage()) {
        pingChatGPT();
      }
    }, 1000 * 30);
  };

  main();
})();