Wiki LaTeX Copier

Copy selected text from wiki with equations in LaTeX format

目前為 2024-08-14 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Wiki LaTeX Copier
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Copy selected text from wiki with equations in LaTeX format
// @author       Jie-Qiao
// @match        *://*.wikipedia.org/*
// @match        *://*.stackexchange.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create the button element
    const button = document.createElement('button');
    button.textContent = 'Copy';
    button.style.position = 'absolute';
    button.style.display = 'none';
    button.style.zIndex = '1000';

    // Append the button to the body
    document.body.appendChild(button);

    // Function to handle button click
    button.addEventListener('click', function() {
        const selectedText = window.getSelection();
        if (selectedText) {
            myfunction(selectedText);
        }
        button.style.display = 'none'; // Hide the button after click
    });

    // Function to show the button when text is selected
    document.addEventListener('mouseup', function(event) {
        const selectedText = window.getSelection().toString().trim();
        if (selectedText) {
            const x = event.pageX;
            const y = event.pageY;
            button.style.left = `${x}px`;
            button.style.top = `${y}px`;
            button.style.display = 'block';
        } else {
            button.style.display = 'none';
        }
    });

    // Custom function to process the selected text
    function myfunction(selection) {
        let url = window.location.href
        let range = selection.getRangeAt(0);
        //console.log('Text selected:', selection.toString()); // Log selected text
        let c=range.cloneContents();
        let text=""
        var node;
        for (var i=0;i<c.childNodes.length;i++)
            {
                node=c.childNodes[i]
                if (node.nodeType === Node.TEXT_NODE) {
                    text+=node.textContent
                }
                if (url.includes('wikipedia.org')) {
                    // If the node is a span, handle it with specific code
                    if (node.nodeType === Node.ELEMENT_NODE && node.nodeName.toLowerCase() === 'span') {
                        text+= '$' + node.getElementsByTagName('math')[0].getAttribute('alttext') + '$';
                        // Add your specific code for span elements here
                    }
                }
                else if(url.includes('stackexchange.com')){
                    // If the node is a span, handle it with specific code

                    if (node.nodeType === Node.ELEMENT_NODE && node.nodeName.toLowerCase() === 'span') {
                        if (node.getElementsByTagName('script').length>0){
                            text+= '$' + node.getElementsByTagName('script')[0].textContent + '$';
                        }
                    }
                    if (node.nodeType === Node.ELEMENT_NODE && node.nodeName.toLowerCase() === 'script') {
                            text+= '$' + node.textContent + '$';
                    }
                }
            }
        // console.log(text);
        navigator.clipboard.writeText(text);
    }
})();