Zhihu Latex 公式复制

双击以复制 Latex 公式代码。

目前为 2023-05-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         Zhihu Latex 公式复制
// @name:en      Zhihu Latex Copy
// @namespace    http://tampermonkey.net/
// @version      0.1.3
// @description  双击以复制 Latex 公式代码。
// @description:en Double click to copy latex codes of formulas.
// @author       PRO
// @match        https://zhuanlan.zhihu.com/*
// @icon         http://zhihu.com/favicon.ico
// @grant        none
// @license      gpl-3.0
// ==/UserScript==

(function() {
    'use strict';
    let name = 'Zhihu Latex Copy';
    let toast = (s, error=false) => {
        if (error) {
            console.error(`[${name}] ${s}`);
        } else {
            console.log(`[${name}] ${s}`);
        }
    };
    if (window.invoke_toastify) {
        window.invoke_toastify();
        toast = (s, error = false) => {
            window.toast(`[${name}] ${s}`, error);
        };
    }
    let emojis = ["🥳", "😘", "😇", "😉"];
    let random_emoji = () => {
        return emojis[Math.floor(Math.random() * emojis.length)];
    };
    let nodes = document.querySelectorAll("span.ztext-math");
    nodes.forEach(node => {
        node.title = "Double click to copy: " + node.attributes.getNamedItem("data-tex").value;
        node.addEventListener("dblclick", e => {
            let latex = node.attributes.getNamedItem("data-tex").value;
            navigator.clipboard.writeText(latex);
            toast(random_emoji() + " Copied!");
        });
    });
})();