ChatGPT equations to Microsoft Word (Katex to MathML)

Add a copy button to ChatGPT equations for copying to Microsoft Word

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ChatGPT equations to Microsoft Word (Katex to MathML)
// @namespace    https://chatgpt.com/
// @version      1.0
// @description  Add a copy button to ChatGPT equations for copying to Microsoft Word
// @author       Bui Quoc Dung
// @match        https://chatgpt.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Function to add the copy button
    function addCopyButton(mathElement, mathML) {
        if (!mathML) return;

        // Check if the button already exists
        if (mathElement.parentElement.querySelector(".copy-mathml-btn")) {
            return;
        }

        // Create a wrapper to ensure proper positioning
        let wrapper = document.createElement("div");
        wrapper.style.display = "inline-flex";
        wrapper.style.alignItems = "center";
        wrapper.style.marginLeft = "10px";

        // Create the copy button
        let copyButton = document.createElement("button");
        copyButton.textContent = "C";
        copyButton.classList.add("copy-mathml-btn");
        copyButton.style.cursor = "pointer";
        copyButton.style.padding = "5px 10px";
        copyButton.style.border = "1px solid #ccc";
        copyButton.style.borderRadius = "5px";
        copyButton.style.background = "#f8f8f8";
        copyButton.style.fontSize = "15px";
        copyButton.style.marginLeft = "10px"; // Ensure space between equation and button

        // Handle click event to copy MathML
        copyButton.addEventListener("click", function () {
            navigator.clipboard.writeText(mathML).catch(err => {
                console.error("Copy failed:", err);
            });
        });

        // Wrap the equation and button together
        mathElement.parentElement.style.display = "inline-flex";
        wrapper.appendChild(copyButton);
        mathElement.parentElement.appendChild(wrapper);
    }

    // Find and process KaTeX formulas on the page
    function processKatexElements() {
        let katexElements = document.querySelectorAll(".katex-mathml");

        for (let el of katexElements) {
            let mathML = el.innerHTML.trim(); // Get MathML content
            addCopyButton(el, mathML);
        }
    }

    // Run when the page loads
    function init() {
        console.log("MathML Copy Button Activated!");
        processKatexElements();

        // Observe DOM changes to detect new formulas
        let observer = new MutationObserver(processKatexElements);
        observer.observe(document.body, { childList: true, subtree: true });
    }

    // Start the script
    init();
})();