LaTeX Copier

Copy selected text from wiki with equations in LaTeX format

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

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