一键复制公式

Click to copy equation in Wikipedia and Zhihu

  1. // ==UserScript==
  2. // @name 一键复制公式
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7
  5. // @description Click to copy equation in Wikipedia and Zhihu
  6. // @author flaribbit
  7. // @match http://*.wikipedia.org/*
  8. // @match https://*.wikipedia.org/*
  9. // @match http://www.wikiwand.com/*
  10. // @match https://www.wikiwand.com/*
  11. // @match https://www.zhihu.com/question/*
  12. // @match https://zhuanlan.zhihu.com/p/*
  13. // @match https://blog.csdn.net/*/article/*
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. (function () {
  18. 'use strict';
  19. const host = document.location.host;
  20. const el = document.createElement('style');
  21. el.innerText = '@keyframes aniclick{0%{background:#03A9F400}20%{background:#03A9F47F}100%{background:#03A9F400}}';
  22. document.head.appendChild(el);
  23. const clearAnimation = function () {
  24. this.style.animation = '';
  25. }
  26. if (host.search('wikipedia') >= 0) {
  27. const copyTex = function () {
  28. navigator.clipboard.writeText('$' + this.alt + '$');
  29. this.style.animation = 'aniclick .4s';
  30. }
  31. const eqs = document.querySelectorAll('.mwe-math-fallback-image-inline, .mwe-math-fallback-image-display');
  32. for (let i = 0; i < eqs.length; i++) {
  33. eqs[i].onclick = copyTex;
  34. eqs[i].addEventListener('animationend', clearAnimation);
  35. eqs[i].title = '点击即可复制公式';
  36. }
  37. } else if (host.search('wikiwand') >= 0){
  38. const copyTex = function () {
  39. const tex = this.getElementsByTagName('math')[0].getAttribute("alttext");
  40. navigator.clipboard.writeText('$' + tex + '$');
  41. this.style.animation = 'aniclick .4s';
  42. }
  43. const check_equations = (mutationList, observer) => {
  44. const eqs = document.querySelectorAll('.mwe-math-element');
  45. for (let i = 0; i < eqs.length; i++) {
  46. eqs[i].onclick = copyTex;
  47. eqs[i].addEventListener('animationend', clearAnimation);
  48. eqs[i].title = '点击即可复制公式';
  49. }
  50. }
  51. const targetNode = document.getElementsByTagName('article')[0];
  52. const config = { attributes: false, childList: true, subtree: true };
  53. const observer = new MutationObserver(check_equations);
  54. observer.observe(targetNode, config);
  55. } else if (host.search('zhihu') >= 0) {
  56. const copyTex = function () {
  57. navigator.clipboard.writeText('$' + this.getAttribute('data-tex') + '$');
  58. this.style.animation = 'aniclick .4s';
  59. }
  60. const check_equations = function () {
  61. if (document.visibilityState == 'visible') {
  62. const eqs = document.querySelectorAll('.ztext-math');
  63. for (let i = 0; i < eqs.length; i++) {
  64. eqs[i].onclick = copyTex;
  65. eqs[i].addEventListener('animationend', clearAnimation);
  66. eqs[i].title = '点击即可复制公式';
  67. }
  68. }
  69. }
  70. if (document.location.href.search('question') >= 0) {
  71. setInterval(check_equations, 1000);
  72. } else {
  73. check_equations();
  74. }
  75. } else if (host.search('blog.csdn') >= 0) {
  76. const copyTex = function () {
  77. navigator.clipboard.writeText('$' + this.querySelector('annotation').textContent.trim() + '$');
  78. this.style.animation = 'aniclick .4s';
  79. }
  80. const eqs = document.querySelectorAll('.katex');
  81. for (let i = 0; i < eqs.length; i++) {
  82. eqs[i].onclick = copyTex;
  83. eqs[i].addEventListener('animationend', clearAnimation);
  84. eqs[i].title = '点击即可复制公式';
  85. }
  86. }
  87. })();

QingJ © 2025

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