Greasy Fork

Greasy Fork is available in English.

BetterChatGPT

detects if chatgpt needs refresh, access to chatgpt while down, favicon based on status

当前为 2023-02-15 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           BetterChatGPT
// @namespace      https://github.com/optionsx
// @version        1.0.4
// @author         https://github.com/optionsx
// @description    detects if chatgpt needs refresh, access to chatgpt while down, favicon based on status
// @grant          GM_setClipboard
// @run-at         document-idle
// @match          https://chat.openai.com/*
// @icon           https://www.google.com/s2/favicons?sz=64&domain=openai.com
// @license MIT
// ==/UserScript==
// check out TheTerrasque extension: https://github.com/TheTerrasque/chatgpt-firefox-extension


let accessToken = "";
document.addEventListener('keydown', function (event) {
  if (event.key === ' ' && event.ctrlKey) {
    GM_setClipboard(accessToken, alert("accessToken Copied to clipboard!"));
  }
});
// access while down functionality
if (document.getElementsByClassName("text-3xl font-medium").length > 0) location.reload();

// sessionExpired? logout? functionality
(async () => {
  try {
    const resp = await fetch("https://chat.openai.com/api/auth/session");
    console.log(resp.status === 403 ? "Session Expired" : "Session Active");
    if (resp?.error !== "RefreshAccessTokenError") {
      accessToken = await resp?.json().then(data => data?.accessToken);
      return true
    }
    changeFavicon("red");
    alert("Session Expired. Logging out...");
    localStorage.removeItem("__Secure-next-auth.session-token");
    location.reload();
  } catch (e) {
    console.log(e);
  }
})();

// remove server-underload yellow warning functionality
const loophole = setInterval(() => {
  try {
    if (document.querySelector(".toast-root").dataset.state) {
      changeFavicon("yellow");
      document.querySelector(".toast-root").innerHTML = "";
      if (document.querySelector(".toast-root").innerHTML === "") clearInterval(loophole);
    }
  } catch { }
}, 0);


// reload if needed functionality && copy to clipboard
const triggerPoint = document.querySelector("textarea"); // "button.absolute" for submit button
triggerPoint.addEventListener("click", detectReload);
triggerPoint.addEventListener("mousemove", () => triggerPoint.focus());
async function detectReload() {
  try {
    const resp = await fetch("https://chat.openai.com/api/auth/session")
    console.log(resp?.status === 403 ? "reload" : "no reload");
    if (resp?.status !== 403) return "no reload"
    const prompt = document.querySelector("textarea").value;
    changeFavicon("yellow");
    prompt.length > 0 ? localStorage.setItem('prompt', prompt) : null;
    location.reload();
  } catch { }
}

// cache prompt functionality
setTimeout(() => {
  const prompt = localStorage.getItem('prompt');
  if (prompt) {
    document.querySelector("textarea").value = prompt
    localStorage.removeItem('prompt');
    document.querySelector("textarea").focus()
  }
}, 500);

const status = {
  yellow:
    "https://github.com/TheTerrasque/chatgpt-firefox-extension/blob/master/resources/favicon-32x32-yellow.png?raw=true",
  green:
    "https://github.com/TheTerrasque/chatgpt-firefox-extension/blob/master/resources/favicon-32x32.png?raw=true",
  red: "https://github.com/TheTerrasque/chatgpt-firefox-extension/blob/master/resources/favicon-32x32-red.png?raw=true",
  blue: "https://github.com/TheTerrasque/chatgpt-firefox-extension/blob/master/resources/favicon-32x32-blue.png?raw=true",
};

// change favicon color functionality
const changeFavicon = color => {
  const icons = document.querySelectorAll("head link[rel='icon']");
  for (let i = 0; i < icons.length; i++) {
    icons[i].href = status[color];
  }
};

// some styling functionality
const color = ["green", "black", "white", "yellow"];
setInterval(() => {
  try {
    const randomColor = color[Math.floor(Math.random() * color.length)];
    if (document.querySelector(".text-4xl").innerHTML !== `<a href="https://t.me/NimDev" class="text-4xl font-semibold text-center mt-6 sm:mt-[20vh] ml-auto mr-auto mb-10 sm:mb-16 flex gap-2 items-center justify-center">ChatGPT</a>`) document.querySelector(".text-4xl").innerHTML = `<a href="https://t.me/NimDev" class="text-4xl font-semibold text-center mt-6 sm:mt-[20vh] ml-auto mr-auto mb-10 sm:mb-16 flex gap-2 items-center justify-center">ChatGPT</a>`
    addCss(`.text-4xl { color: ${randomColor} !important; }`) // nice color right?
    if (document.querySelector("div.px-3").childNodes.length > 1) document.querySelector("div.px-3").childNodes[1].remove() // remove blah blah blah text which don't exist in plus version
    if (document.querySelector(".text-gray-800").childElementCount > 1) document.querySelector(".text-gray-800").lastChild.remove() // unnecessary block of text
    //if (document.querySelector(".text-4xl").textContent === "ChatGPT") document.querySelector(".text-4xl").textContent = 'ChadGPT' // xD

  }
  catch (e) { console.log(e); }
}, 1500);
function addCss(cssString) {
  var head = document.getElementsByTagName('head')[0];
  var newCss = document.createElement('style');
  newCss.type = "text/css";
  newCss.innerHTML = cssString;
  head.appendChild(newCss);
}