Fix <code> bug for Edge translator

把网页中的所有<code>标签替换成同样式<span>,以修复Edge内置翻译器bug

当前为 2024-01-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Fix <code> bug for Edge translator
  3. // @name:zh-CN Fix <code> bug for Edge translator
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.0
  6. // @description Replace <code> tags with styled <span> to fix the bug of Edge's translator.
  7. // @description:zh-CN 把网页中的所有<code>标签替换成同样式<span>,以修复Edge内置翻译器bug
  8. // @author yqs112358
  9. // @license MIT
  10. // @match *://*/*
  11. // @grant none
  12. // @run-at document-end
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // Copy styles from one element to another
  19. function copyStyles(source, target) {
  20. const computedStyle = window.getComputedStyle(source);
  21. for (let key of computedStyle) {
  22. target.style[key] = computedStyle[key];
  23. }
  24. }
  25.  
  26. // Replace a single <code> tag with a styled <span>
  27. function replaceCodeToSpan(node) {
  28. if (node.tagName === 'CODE') {
  29. const span = document.createElement('span');
  30. span.textContent = node.textContent;
  31. // Copy all computed styles from <code> to <span>
  32. copyStyles(node, span);
  33. node.parentNode.replaceChild(span, node);
  34. }
  35. }
  36.  
  37. // Process a node and its child for <code> tags
  38. function processNodeAndChild(node) {
  39. if (node.nodeType === 1) { // Element node
  40. replaceCodeToSpan(node);
  41. node.querySelectorAll('code').forEach(replaceCodeToSpan);
  42. }
  43. }
  44.  
  45. ////////////////////////////////////////////////////////
  46.  
  47. // Replace <code> at startup
  48. document.querySelectorAll('code').forEach(replaceCodeToSpan);
  49.  
  50. // Observe DOM changes and replace new-generated <code> if needed
  51. const observer = new MutationObserver(function(mutations) {
  52. mutations.forEach(function(mutation) {
  53. mutation.addedNodes.forEach(processNodeAndChild);
  54. });
  55. });
  56. observer.observe(document.body, {
  57. childList: true,
  58. subtree: true
  59. });
  60. })();

QingJ © 2025

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