ChatGPT LaTeX Support

Support LaTeX for ChatGPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ChatGPT LaTeX Support
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  Support LaTeX for ChatGPT
// @author       Elcvise
// @license      MIT
// @match        https://chat.openai.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=openai.com
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js
// ==/UserScript==

"use strict";

function render_item(item) {
  renderMathInElement(item, {
    // customised options
    // • auto-render specific keys, e.g.:
    delimiters: [
      { left: "$$", right: "$$", display: true },
      { left: "$", right: "$", display: false },
      { left: "\\(", right: "\\)", display: false },
      { left: "\\[", right: "\\]", display: true },
    ],
    // • rendering keys, e.g.:
    throwOnError: false,
  });
  return true;
}

function check_and_render(item) {
  if (!item) return false;
  if (item?.classList?.contains("math-display")) return false;
  if (item?.classList?.contains("rendered")) return false;
  if (item?.classList?.contains("katex")) return false;
  render_item(item);
  item.classList.add("rendered");
  console.log("render_succeed");
  return true;
}

function rend_latest() {
  try {
    const list = document.getElementsByClassName("text-base");
    for (const ppp of list) {
      for (const text of ppp.querySelectorAll(
        ".markdown:not(.result-streaming)"
      )) {
        check_and_render(text);
      }
      for (const text of ppp.querySelectorAll(
        ".markdown.result-streaming > *:not(:last-child):not(pre), " +
          ".markdown.result-streaming > ol:last-child li:not(last-child), " +
          ".markdown.result-streaming > ul:last-child li:not(last-child)"
      )) {
        check_and_render(text);
      }
    }
  } catch (e) {
    console.warn(e);
  }
}

window.katex = katex;

window.rend_latest = rend_latest;

setInterval(() => {
  rend_latest();
}, 1000);