MathML Copy Button

Adds a button that copies MathML code to the clipboard.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         MathML Copy Button
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Adds a button that copies MathML code to the clipboard.
// @author       Remakker
// @match        https://p2t.breezedeus.com/*
// @grant        GM_setClipboard
// @licence     MIT
// ==/UserScript==

(() => {
    'use strict';

    const copyMathML = () => {
        const inputElement = document.querySelector('mjx-assistive-mml');
        if (!inputElement) return console.error("Cannot find the element containing MathML code!");
        GM_setClipboard(inputElement.innerHTML);
    };

    const applyStylesFromCSSRule = (targetElem, selector) => {
        Array.from(document.styleSheets).forEach(sheet => {
            let rules;
            try {
                rules = sheet.cssRules;
            } catch (e) {
                return;
            }
            if (!rules) return;
            Array.from(rules).forEach(rule => {
                if (rule.type === CSSRule.STYLE_RULE && rule.selectorText === selector) {
                    for (let i = 0; i < rule.style.length; i++) {
                        const propertyName = rule.style[i];
                        const value = rule.style.getPropertyValue(propertyName);
                        const priority = rule.style.getPropertyPriority(propertyName);
                        targetElem.style.setProperty(propertyName, value, priority);
                    }
                }
            });
        });
    };

    const addCopyButton = () => {
        const originalButton = document.querySelector('#copyAsin');
        const newContainer = document.querySelector('#box');
        if (!originalButton || !newContainer) return console.error('Required element(s) not found');
        const newButton = originalButton.cloneNode(true);
        newButton.id = 'newButton';
        newButton.removeAttribute('data-clipboard-text');
        newButton.addEventListener("click", copyMathML);
        const specificSelector = ".Index_indexBox__s5lfQ .Index_index__GsKUn .Index_content__IDt4D > .Index_textAreaBox__ne32- .Index_copy__k5Mir";
        applyStylesFromCSSRule(newButton, specificSelector);
        newContainer.appendChild(newButton);
    };

    window.addEventListener('load', addCopyButton);
})();