You.com KaTeX Copy Handler

Clean copy of KaTeX expressions without duplicates or extra line breaks

  1. // ==UserScript==
  2. // @name You.com KaTeX Copy Handler
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Clean copy of KaTeX expressions without duplicates or extra line breaks
  6. // @author Assistant
  7. // @match *://*.you.com/*
  8. // @grant none
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=you.com
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to extract clean text from a KaTeX element
  17. function extractKaTeXText(element) {
  18. // Get the LaTeX source from annotation if available
  19. const annotation = element.querySelector('.katex-mathml annotation[encoding="application/x-tex"]');
  20. if (annotation) {
  21. return annotation.textContent;
  22. }
  23.  
  24. // Fallback: extract from visible content
  25. const baseElements = element.querySelectorAll('.katex-html .base .mord, .katex-html .base .mopen, .katex-html .base .mclose');
  26. return Array.from(baseElements).map(el => el.textContent).join('');
  27. }
  28.  
  29. // Function to extract text from a range, handling both KaTeX and regular text
  30. function extractTextFromRange(range) {
  31. const fragment = range.cloneContents();
  32. const nodes = Array.from(fragment.childNodes);
  33. let result = '';
  34.  
  35. nodes.forEach(node => {
  36. if (node.nodeType === Node.ELEMENT_NODE) {
  37. // Check if the node is a KaTeX element
  38. if (node.classList.contains('katex')) {
  39. result += extractKaTeXText(node);
  40. } else {
  41. result += node.textContent; // Regular element
  42. }
  43. } else if (node.nodeType === Node.TEXT_NODE) {
  44. result += node.textContent; // Regular text
  45. }
  46. });
  47.  
  48. return result;
  49. }
  50.  
  51. // Handle copy event
  52. document.addEventListener('copy', function (e) {
  53. const selection = window.getSelection();
  54. if (!selection.rangeCount) return;
  55.  
  56. const range = selection.getRangeAt(0);
  57.  
  58. // Extract text from the range, handling both KaTeX and regular text
  59. const cleanText = extractTextFromRange(range);
  60.  
  61. // Override the clipboard data
  62. e.preventDefault();
  63. e.clipboardData.setData('text/plain', cleanText);
  64. });
  65.  
  66.  
  67.  
  68. // Log that script is active
  69. console.log('KaTeX Copy Handler active');
  70. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址