Greasy Fork

Greasy Fork is available in English.

arxiv公式复制/arxiv latex copy

Click to copy equation in arxiv html

当前为 2024-07-01 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         arxiv公式复制/arxiv latex copy
// @namespace    http://tampermonkey.net/
// @version      0.8
// @description  Click to copy equation in arxiv html
// @author       Jas0nG
// @include      /^https?://(.*\.)?arxiv\.org/.*/
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict'; // 严格模式

    // 创建一个 <style> 元素,用于添加 CSS 动画和提示框样式
    const el = document.createElement('style');
    // 定义 CSS 动画 @keyframes aniclick 和提示框样式
    el.innerText = `
        @keyframes aniclick{0%{background:#03A9F400}20%{background:#03A9F47F}100%{background:#03A9F400}}
        .tooltip {
            position: absolute;
            background-color: #333;
            color: #fff;
            padding: 5px;
            border-radius: 5px;
            font-size: 12px;
            z-index: 1000;
            opacity: 0;
            transition: opacity 0.3s;
            pointer-events: none;
        }
        .ltx_eqn, .ltx_Math {
            position: relative; /* 确保 title 正常显示 */
        }
    `;
    // 将 <style> 元素添加到文档的 <head> 中
    document.head.appendChild(el);

    // 创建一个提示框元素
    const tooltip = document.createElement('div');
    tooltip.className = 'tooltip';
    tooltip.textContent = '点击即可复制公式';
    document.body.appendChild(tooltip);

    // 清除动画效果的函数
    const clearAnimation = function () {
        this.style.animation = '';
    }

    // 显示提示框
    const showTooltip = function (e) {
        const rect = e.target.getBoundingClientRect();
        tooltip.style.left = `${rect.left + window.pageXOffset}px`;
        tooltip.style.top = `${rect.top + window.pageYOffset - tooltip.offsetHeight - 5}px`;
        tooltip.style.opacity = 1;
    }

    // 隐藏提示框
    const hideTooltip = function () {
        tooltip.style.opacity = 0;
    }

    // 当页面加载完成时执行
    window.addEventListener('load', function() {
        // 定义点击事件处理函数
        const copyTex = function () {
            // 获取公式的 LaTeX 代码
            const tex = this.getAttribute('alttext') || this.textContent;
            // 将公式文本复制到剪贴板
            navigator.clipboard.writeText('$$' + tex + '$$').then(() => {
                console.log('Copied: ', tex);
            }).catch(err => {
                console.error('Failed to copy: ', err);
            });
            // 添加动画效果
            this.style.animation = 'aniclick .4s';
        }

        // 获取所有公式元素
        const eqs = document.querySelectorAll('.ltx_eqn, .ltx_Math');
        // 为每个公式元素添加点击事件处理函数和动画结束事件处理函数
        for (let i = 0; i < eqs.length; i++) {
            eqs[i].onclick = copyTex;
            eqs[i].addEventListener('animationend', clearAnimation);
            eqs[i].addEventListener('mouseover', showTooltip);
            eqs[i].addEventListener('mouseout', hideTooltip);
        }
    });
})();