// ==UserScript== // @name LaTeX Copier // @namespace http://tampermonkey.net/ // @version 1.5.1 // @description Copy selected text from wiki with equations in LaTeX format // @author Jie-Qiao // @match *://*.wikipedia.org/* // @match *://*.stackexchange.com/* // @match *://alejandroschuler.github.io/* // @match *://*.zhihu.com/* // @grant none // @license MIT // @downloadURL none // ==/UserScript== function getTextContentWithReplacements(url,node) { let text = ''; if (node && node.childNodes) { node.childNodes.forEach(child => { const nodeType = child.nodeType; const nodeName = child.nodeName.toLowerCase(); // Default behavior for text nodes if (child.nodeType === Node.TEXT_NODE) { text += child.textContent; } if (nodeType === Node.ELEMENT_NODE){ // URL-specific processing rules if (url.includes('wikipedia.org')) { if (nodeName === 'span') { if (child.querySelectorAll('math').length > 0) { text += '$' + child.getElementsByTagName('math')[0].getAttribute('alttext') + '$'; } else if (child.querySelectorAll('img').length > 0) { text += '$' + child.querySelectorAll('img')[0].getAttribute('alt') + '$'; } } } else if (url.includes('stackexchange.com') || url.includes('zhihu.com')) { if (nodeName === 'span') { if (child.getElementsByTagName('script').length > 0) { text += '$' + child.getElementsByTagName('script')[0].textContent + '$'; } } if (nodeName === 'script') { text += '$' + child.textContent + '$'; } } else if (url.includes('alejandroschuler.github.io')) { if (nodeName === 'span') { if (child.getElementsByTagName('annotation').length > 0) { text += '$' + child.getElementsByTagName('annotation')[0].textContent + '$'; } } } } // For other elements, recurse into their children if (child.nodeType === Node.ELEMENT_NODE && !['span', 'script', 'math', 'img'].includes(child.nodeName.toLowerCase())) { text += getTextContentWithReplacements(url,child); } }); } return text; } (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 }); let previousSelectedText = ''; document.addEventListener('mouseup', function(event) { const selectedText = window.getSelection().toString().trim(); // Only show the button if the selected text has changed if (selectedText && selectedText !== previousSelectedText) { const x = event.pageX + 5; const y = event.pageY + 5; button.style.left = `${x}px`; button.style.top = `${y}px`; button.style.display = 'block'; previousSelectedText = selectedText; // Update the previous selection } else { button.style.display = 'none'; previousSelectedText = ''; // Reset previous selection if no text is selected } }); // 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 = getTextContentWithReplacements(url,c); navigator.clipboard.writeText(text); } })();