Wiki LaTeX Copier

Copy selected text from wiki with equations in LaTeX format

目前为 2024-10-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Wiki LaTeX Copier
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.4
  5. // @description Copy selected text from wiki with equations in LaTeX format
  6. // @author Jie-Qiao
  7. // @match *://*.wikipedia.org/*
  8. // @match *://*.stackexchange.com/*
  9. // @match *://alejandroschuler.github.io/*
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. function getTextContentWithReplacements(url,node) {
  15. let text = '';
  16. if (node && node.childNodes) {
  17. node.childNodes.forEach(child => {
  18. const nodeType = child.nodeType;
  19. const nodeName = child.nodeName.toLowerCase();
  20. // URL-specific processing rules
  21. if (url.includes('wikipedia.org')) {
  22. if (nodeType === Node.ELEMENT_NODE && nodeName === 'span') {
  23. if (child.querySelectorAll('math').length > 0) {
  24. text += '$' + child.getElementsByTagName('math')[0].getAttribute('alttext') + '$';
  25. } else if (child.querySelectorAll('img').length > 0) {
  26. text += '$' + child.querySelectorAll('img')[0].getAttribute('alt') + '$';
  27. }
  28. } else if (nodeType === Node.ELEMENT_NODE) {
  29. child.querySelectorAll('img').forEach(img => {
  30. text += '$' + img.getAttribute('alt') + '$';
  31. });
  32. }
  33. } else if (url.includes('stackexchange.com')) {
  34. if (nodeType === Node.ELEMENT_NODE && nodeName === 'span') {
  35. if (child.getElementsByTagName('script').length > 0) {
  36. text += '$' + child.getElementsByTagName('script')[0].textContent + '$';
  37. }
  38. }
  39. if (nodeType === Node.ELEMENT_NODE && nodeName === 'script') {
  40. text += '$' + child.textContent + '$';
  41. }
  42. } else if (url.includes('alejandroschuler.github.io')) {
  43. if (nodeType === Node.ELEMENT_NODE && nodeName === 'span') {
  44. if (child.getElementsByTagName('annotation').length > 0) {
  45. text += '$' + child.getElementsByTagName('annotation')[0].textContent + '$';
  46. }
  47. }
  48. }
  49.  
  50. // Default behavior for text nodes
  51. if (child.nodeType === Node.TEXT_NODE) {
  52. text += child.textContent;
  53. }
  54.  
  55. // For other elements, recurse into their children
  56. if (child.nodeType === Node.ELEMENT_NODE && !['span', 'script', 'math', 'img'].includes(child.nodeName.toLowerCase())) {
  57. text += getTextContentWithReplacements(url,child);
  58. }
  59. });
  60. }
  61. return text;
  62. }
  63.  
  64. (function() {
  65. 'use strict';
  66.  
  67. // Create the button element
  68. const button = document.createElement('button');
  69. button.textContent = 'Copy';
  70. button.style.position = 'absolute';
  71. button.style.display = 'none';
  72. button.style.zIndex = '1000';
  73.  
  74. // Append the button to the body
  75. document.body.appendChild(button);
  76.  
  77. // Function to handle button click
  78. button.addEventListener('click', function() {
  79. const selectedText = window.getSelection();
  80. if (selectedText) {
  81. myfunction(selectedText);
  82. }
  83. button.style.display = 'none'; // Hide the button after click
  84. });
  85.  
  86. let previousSelectedText = '';
  87.  
  88. document.addEventListener('mouseup', function(event) {
  89. const selectedText = window.getSelection().toString().trim();
  90.  
  91. // Only show the button if the selected text has changed
  92. if (selectedText && selectedText !== previousSelectedText) {
  93. const x = event.pageX + 5;
  94. const y = event.pageY + 5;
  95. button.style.left = `${x}px`;
  96. button.style.top = `${y}px`;
  97. button.style.display = 'block';
  98. previousSelectedText = selectedText; // Update the previous selection
  99. } else {
  100. button.style.display = 'none';
  101. previousSelectedText = ''; // Reset previous selection if no text is selected
  102. }
  103. });
  104.  
  105.  
  106. // Custom function to process the selected text
  107. function myfunction(selection) {
  108. let url = window.location.href
  109. let range = selection.getRangeAt(0);
  110. //console.log('Text selected:', selection.toString()); // Log selected text
  111. let c=range.cloneContents();
  112. let text = getTextContentWithReplacements(url,c);
  113. navigator.clipboard.writeText(text);
  114. }
  115. })();