MathML Copy Button

Adds a button that copies MathML code to the clipboard.

  1. // ==UserScript==
  2. // @name MathML Copy Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Adds a button that copies MathML code to the clipboard.
  6. // @author Remakker
  7. // @match https://p2t.breezedeus.com/*
  8. // @grant GM_setClipboard
  9. // @licence MIT
  10. // ==/UserScript==
  11.  
  12. (() => {
  13. 'use strict';
  14.  
  15. const copyMathML = () => {
  16. const inputElement = document.querySelector('mjx-assistive-mml');
  17. if (!inputElement) return console.error("Cannot find the element containing MathML code!");
  18. GM_setClipboard(inputElement.innerHTML);
  19. };
  20.  
  21. const applyStylesFromCSSRule = (targetElem, selector) => {
  22. Array.from(document.styleSheets).forEach(sheet => {
  23. let rules;
  24. try {
  25. rules = sheet.cssRules;
  26. } catch (e) {
  27. return;
  28. }
  29. if (!rules) return;
  30. Array.from(rules).forEach(rule => {
  31. if (rule.type === CSSRule.STYLE_RULE && rule.selectorText === selector) {
  32. for (let i = 0; i < rule.style.length; i++) {
  33. const propertyName = rule.style[i];
  34. const value = rule.style.getPropertyValue(propertyName);
  35. const priority = rule.style.getPropertyPriority(propertyName);
  36. targetElem.style.setProperty(propertyName, value, priority);
  37. }
  38. }
  39. });
  40. });
  41. };
  42.  
  43. const addCopyButton = () => {
  44. const originalButton = document.querySelector('#copyAsin');
  45. const newContainer = document.querySelector('#box');
  46. if (!originalButton || !newContainer) return console.error('Required element(s) not found');
  47. const newButton = originalButton.cloneNode(true);
  48. newButton.id = 'newButton';
  49. newButton.removeAttribute('data-clipboard-text');
  50. newButton.addEventListener("click", copyMathML);
  51. const specificSelector = ".Index_indexBox__s5lfQ .Index_index__GsKUn .Index_content__IDt4D > .Index_textAreaBox__ne32- .Index_copy__k5Mir";
  52. applyStylesFromCSSRule(newButton, specificSelector);
  53. newContainer.appendChild(newButton);
  54. };
  55.  
  56. window.addEventListener('load', addCopyButton);
  57. })();