Latex_Automatic Formatting

Typesetting the contents of the clipboard

当前为 2024-12-09 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Latex_Automatic Formatting
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Typesetting the contents of the clipboard
// @author       Mozikiy
// @match        http://annot.xhanz.cn/project/*/*
// @icon         https://www.latex-project.org/favicon.ico
// @license      GNU GPLv3
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 定义标点转换的函数
    function convertPunctuation(text) {
        return text
            .replace(/(?<=[^a-zA-Z0-9]) /g, '$\\underline { \\hspace{1cm} }$')
            .replace(/,/g, ', ')
            .replace(/\./g, '. ')
            .replace(/,/g, ', ')
            .replace(/。/g, '. ')
            .replace(/&gt;/g, '>')
            .replace(/&lt;/g, '<')
            .replace(/λ/g, '$\\lambda$')
            .replace(/α/g, '$\\alpha$')
            .replace(/β/g, '$\\beta$')
            .replace(/γ/g, '$\\gamma$')
            .replace(/ρ/g, '$\\rho$')
            .replace(/σ/g, '$\\sigma$')
            .replace(/δ/g, '$\\delta$')
            .replace(/φ/g, '$\\varphi$')
            .replace(/:/g, ': ')
            .replace(/⋯/g, '\\cdots')
            .replace(/x,/g, '$x$,')
            .replace(/\|/g, '\\vert')
            .replace(/(/g, '(')
            .replace(/)/g, ')')
            .replace(/[/g, '[')
            .replace(/]/g, ']')
            .replace(/C02/g, '$CO_2$')
            .replace(/H2O/g, '$H_2O$')
            .replace(/CO2/g, '$CO_2$')
            .replace(/H20/g, '$H_2O$')
            .replace(/,,/g, ', ')
            .replace(/\.\./g, '. ')
            .replace(/, ,/g, ', ')
            .replace(/\. \./g, '. ');
    }

    // 检测剪贴板复制事件并处理内容
    document.addEventListener('copy', async function(event) {
        try {
            // 获取剪贴板内容
            const clipboardText = await navigator.clipboard.readText();

            // 处理剪贴板内容
            const processedText = clipboardText
                .split('\n')
                .map(line => convertPunctuation(line))
                .join('\n');

            // 写回剪贴板
            await navigator.clipboard.writeText(processedText);
            console.log("Processed text written back to clipboard!");
        } catch (error) {
            console.error("Error processing clipboard text:", error);
        }
    });

    console.log("Latex_Automatic Formatting script initialized!");
})();