Edge Translator Fix Code/Kbd Tag

Fix Edge Translator bug with code and kbd tags

  1. // ==UserScript==
  2. // @name Edge Translator Fix Code/Kbd Tag
  3. // @name:en Edge Translator Fix Code/Kbd Tag
  4. // @name:vi Sửa lỗi dịch các thẻ code và kbd trong Edge Translator
  5. // @name:zh-CN Edge翻译器代码和键盘标签修复
  6. // @name:ru Исправление тегов code и kbd в Edge Translator
  7. // @namespace http://tampermonkey.net/
  8. // @version 1.1
  9. // @description Fix Edge Translator bug with code and kbd tags
  10. // @description:en Fix Edge Translator bug with code and kbd tags
  11. // @description:vi Sửa lỗi dịch các thẻ code và kbd
  12. // @description:zh-CN 修复Edge翻译器代码和键盘标签的翻译错误
  13. // @description:ru Исправление ошибок перевода тегов code и kbd в Edge Translator
  14. // @author Yuusei
  15. // @match *://*/*
  16. // @license GPL-3.0-only
  17. // @compatible chrome
  18. // @compatible edge
  19. // @compatible firefox
  20. // @copyright 2024, Yuusei
  21. // @grant none
  22. // ==/UserScript==
  23.  
  24. (function () {
  25. 'use strict';
  26.  
  27. let isTranslationActive = false;
  28.  
  29. function replaceTagToSpan(node) {
  30. if ((node.tagName === 'CODE' || node.tagName === 'KBD') && node.nodeType === 1 && node.children.length === 0) {
  31. const spanNode = document.createElement('span');
  32. const computedStyle = window.getComputedStyle(node);
  33.  
  34. const requiredStyles = ['background-color', 'border-radius', 'border', 'box-shadow', 'color', 'display', 'font-size', 'font-family', 'font-weight', 'line-height', 'padding', 'margin', 'color', 'white-space'];
  35.  
  36. requiredStyles.forEach(style => {
  37. spanNode.style[style] = computedStyle.getPropertyValue(style);
  38. });
  39.  
  40. spanNode.innerHTML = node.innerHTML;
  41.  
  42. if (node.tagName === 'KBD') {
  43. spanNode.style.whiteSpace = 'nowrap';
  44. spanNode.style.width = 'auto';
  45. spanNode.style.maxWidth = '100%';
  46. }
  47.  
  48. node.parentNode.replaceChild(spanNode, node);
  49. }
  50. }
  51.  
  52. function processNodeAndChild(node) {
  53. if (node.nodeType === 1) {
  54. node.querySelectorAll('code, kbd').forEach(replaceTagToSpan);
  55. }
  56. }
  57.  
  58. const titleObserver = new MutationObserver(function (mutations) {
  59. mutations.forEach(function (mutation) {
  60. if (mutation.type === 'attributes' && mutation.attributeName === '_msttexthash') {
  61. const isCurrentlyTranslated = mutation.target.hasAttribute('_msttexthash');
  62. if (isCurrentlyTranslated && !isTranslationActive) {
  63. isTranslationActive = true;
  64. processNodeAndChild(document.body);
  65.  
  66. const contentObserver = new MutationObserver(function (mutations) {
  67. mutations.forEach(function (mutation) {
  68. if (mutation.type === 'childList' || mutation.type === 'characterData') {
  69. if (mutation.target.querySelector && (mutation.target.querySelector('code') || mutation.target.querySelector('kbd'))) {
  70. processNodeAndChild(mutation.target);
  71. }
  72. }
  73. });
  74. });
  75.  
  76. contentObserver.observe(document.body, {
  77. childList: true,
  78. subtree: true,
  79. characterData: true,
  80. });
  81.  
  82. const stopTranslationObserver = new MutationObserver(function (stopMutations) {
  83. stopMutations.forEach(function (stopMutation) {
  84. if (!mutation.target.hasAttribute('_msttexthash')) {
  85. contentObserver.disconnect();
  86. stopTranslationObserver.disconnect();
  87. isTranslationActive = false;
  88. titleObserver.disconnect();
  89. }
  90. });
  91. });
  92.  
  93. stopTranslationObserver.observe(mutation.target, {
  94. attributes: true,
  95. attributeFilter: ['_msttexthash'],
  96. });
  97. }
  98. }
  99. });
  100. });
  101.  
  102. if (document.readyState === 'loading') {
  103. document.addEventListener('DOMContentLoaded', initObserver);
  104. } else {
  105. initObserver();
  106. }
  107.  
  108. function initObserver() {
  109. const titleTag = document.querySelector('head > title');
  110. if (titleTag) {
  111. titleObserver.observe(titleTag, {
  112. attributes: true,
  113. });
  114. }
  115. }
  116. })();

QingJ © 2025

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