您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Copy selected text from wiki with equations in LaTeX format
当前为
- // ==UserScript==
- // @name Wiki LaTeX Copier
- // @namespace http://tampermonkey.net/
- // @version 1.3
- // @description Copy selected text from wiki with equations in LaTeX format
- // @author Jie-Qiao
- // @match *://*.wikipedia.org/*
- // @match *://*.stackexchange.com/*
- // @match *://alejandroschuler.github.io/*
- // @grant none
- // @license MIT
- // ==/UserScript==
- function getTextContentWithReplacements(url,node) {
- let text = '';
- if (node && node.childNodes) {
- node.childNodes.forEach(child => {
- // URL-specific processing rules
- if (url.includes('wikipedia.org')) {
- if (child.nodeType === Node.ELEMENT_NODE && child.nodeName.toLowerCase() === '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 (child.nodeType === Node.ELEMENT_NODE) {
- child.querySelectorAll('img').forEach(img => {
- text += '$' + img.getAttribute('alt') + '$';
- });
- }
- } else if (url.includes('stackexchange.com')) {
- if (child.nodeType === Node.ELEMENT_NODE && child.nodeName.toLowerCase() === 'span') {
- if (child.getElementsByTagName('script').length > 0) {
- text += '$' + child.getElementsByTagName('script')[0].textContent + '$';
- }
- }
- if (child.nodeType === Node.ELEMENT_NODE && child.nodeName.toLowerCase() === 'script') {
- text += '$' + child.textContent + '$';
- }
- } else if (url.includes('alejandroschuler.github.io')) {
- if (child.nodeType === Node.ELEMENT_NODE && child.nodeName.toLowerCase() === 'span') {
- if (child.getElementsByTagName('annotation').length > 0) {
- text += '$' + child.getElementsByTagName('annotation')[0].textContent + '$';
- }
- }
- }
- // Default behavior for text nodes
- if (child.nodeType === Node.TEXT_NODE) {
- text += child.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
- });
- // 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 = getTextContentWithReplacements(url,c);
- // let text=""
- // var node;
- // console.log('Processed text:', textContent);
- // for (var i=0;i<c.childNodes.length;i++)
- // {
- // node=c.childNodes[i]
- // if (node.nodeType === Node.TEXT_NODE || node.nodeName.toLowerCase() === 'p' ) {
- // text+=node.textContent;
- // continue;
- // }
- // 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') {
- // if(node.querySelectorAll('math').length>0){
- // text+= '$' + node.getElementsByTagName('math')[0].getAttribute('alttext') + '$';
- // }else if(node.querySelectorAll('img').length>0){
- // text+= '$' + node.querySelectorAll('img')[0].getAttribute('alt') + '$';
- // }
- // // if(node.querySelectorAll('img').length>0){
- // // text+= '$' + node.querySelectorAll('img')[0].getAttribute('alt') + '$';
- // // }
- // // Add your specific code for span elements here
- // }
- // else if(node.nodeType === Node.ELEMENT_NODE){
- // for (var j=0;j<node.querySelectorAll('img').length;j++){
- // text+= '$' + node.querySelectorAll('img')[j].getAttribute('alt') + '$';
- // }
- // }
- // }
- // 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 + '$';
- // }
- // }else if (url.includes('alejandroschuler.github.io')){
- // if (node.nodeType === Node.ELEMENT_NODE && node.nodeName.toLowerCase() === 'span') {
- // if (node.getElementsByTagName('annotation').length>0){
- // text+= '$' + node.getElementsByTagName('annotation')[0].textContent + '$';
- // }
- // }
- // }
- // }
- // console.log(text);
- navigator.clipboard.writeText(text);
- }
- })();