ar5iv tex copy

Click on any equation in ar5iv to copy the tex code to clipboard

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ar5iv tex copy
// @namespace    http://tampermonkey.net/
// @version      2023-12-25
// @description  Click on any equation in ar5iv to copy the tex code to clipboard
// @author       joelsleeba
// @match        https://ar5iv.labs.arxiv.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=arxiv.org
// @license      GNU AGPLv3 
// ==/UserScript==

(function() {
    'use strict';

    function handleBlockEquationClick(event) {
  event.stopPropagation();
  const equation = event.target.closest(".ltx_equation")

  if (equation) {
    const mathElements = equation.querySelectorAll("math");
    const altTexts = Array.from(mathElements).map(element => {
      const altText = element.getAttribute("alttext");
      return altText.replace(/\\displaystyle/g, ""); // Remove '\displaystyle'
    });
    const joinedAltText = altTexts.join(" ");
    console.log(joinedAltText);

    // Do something with the joined alt text here, e.g., display it in an alert:
    navigator.clipboard.writeText(joinedAltText)
    .then(() => {
      // Copying succeeded
      console.log("Alt text copied to clipboard");
    })
    .catch(err => {
      // Copying failed, handle the error
      console.error("Failed to copy alt text:", err);
    });
  } else {
    console.log("Clicked element is not within an .ltx_equation");
  }
}

function handleInlineEquationClick(event) {
  event.stopPropagation();
  const equation = event.target.closest("math")

  if (equation) {
    const altText = equation.getAttribute("alttext");
    console.log(altText);

    // Do something with the joined alt text here, e.g., display it in an alert:
    navigator.clipboard.writeText(altText)
    .then(() => {
      // Copying succeeded
      console.log("Alt text copied to clipboard");
    })
    .catch(err => {
      // Copying failed, handle the error
      console.error("Failed to copy alt text:", err);
    });
  } else {
    console.log("Clicked element is not within an .ltx_equation");
  }
}

// Attach the event listener to all descendants of .ltx_equationgroup
document.querySelectorAll(".ltx_equation *").forEach(element => {
  element.addEventListener("click", handleBlockEquationClick);
});

// // Attach the event listener to all descendants of .ltx_Math which are not themselves descendants of .ltx_equation
document.querySelectorAll(".ltx_Math[display='inline']:not(.ltx_equationgroup *) *").forEach(element => {
  element.addEventListener("click", handleInlineEquationClick);
});

})();