🏠 返回首頁 

Greasy Fork is available in English.

Исправление тегов code и kbd в Edge Translator

Исправление ошибок перевода тегов code и kbd в Edge Translator

  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. (function () {
  24. 'use strict';
  25. let isTranslationActive = false;
  26. function replaceTagToSpan(node) {
  27. if ((node.tagName === 'CODE' || node.tagName === 'KBD') && node.nodeType === 1 && node.children.length === 0) {
  28. const spanNode = document.createElement('span');
  29. const computedStyle = window.getComputedStyle(node);
  30. const requiredStyles = ['background-color', 'border-radius', 'border', 'box-shadow', 'color', 'display', 'font-size', 'font-family', 'font-weight', 'line-height', 'padding', 'margin', 'color', 'white-space'];
  31. requiredStyles.forEach(style => {
  32. spanNode.style[style] = computedStyle.getPropertyValue(style);
  33. });
  34. spanNode.innerHTML = node.innerHTML;
  35. if (node.tagName === 'KBD') {
  36. spanNode.style.whiteSpace = 'nowrap';
  37. spanNode.style.width = 'auto';
  38. spanNode.style.maxWidth = '100%';
  39. }
  40. node.parentNode.replaceChild(spanNode, node);
  41. }
  42. }
  43. function processNodeAndChild(node) {
  44. if (node.nodeType === 1) {
  45. node.querySelectorAll('code, kbd').forEach(replaceTagToSpan);
  46. }
  47. }
  48. const titleObserver = new MutationObserver(function (mutations) {
  49. mutations.forEach(function (mutation) {
  50. if (mutation.type === 'attributes' && mutation.attributeName === '_msttexthash') {
  51. const isCurrentlyTranslated = mutation.target.hasAttribute('_msttexthash');
  52. if (isCurrentlyTranslated && !isTranslationActive) {
  53. isTranslationActive = true;
  54. processNodeAndChild(document.body);
  55. const contentObserver = new MutationObserver(function (mutations) {
  56. mutations.forEach(function (mutation) {
  57. if (mutation.type === 'childList' || mutation.type === 'characterData') {
  58. if (mutation.target.querySelector && (mutation.target.querySelector('code') || mutation.target.querySelector('kbd'))) {
  59. processNodeAndChild(mutation.target);
  60. }
  61. }
  62. });
  63. });
  64. contentObserver.observe(document.body, {
  65. childList: true,
  66. subtree: true,
  67. characterData: true,
  68. });
  69. const stopTranslationObserver = new MutationObserver(function (stopMutations) {
  70. stopMutations.forEach(function (stopMutation) {
  71. if (!mutation.target.hasAttribute('_msttexthash')) {
  72. contentObserver.disconnect();
  73. stopTranslationObserver.disconnect();
  74. isTranslationActive = false;
  75. titleObserver.disconnect();
  76. }
  77. });
  78. });
  79. stopTranslationObserver.observe(mutation.target, {
  80. attributes: true,
  81. attributeFilter: ['_msttexthash'],
  82. });
  83. }
  84. }
  85. });
  86. });
  87. if (document.readyState === 'loading') {
  88. document.addEventListener('DOMContentLoaded', initObserver);
  89. } else {
  90. initObserver();
  91. }
  92. function initObserver() {
  93. const titleTag = document.querySelector('head > title');
  94. if (titleTag) {
  95. titleObserver.observe(titleTag, {
  96. attributes: true,
  97. });
  98. }
  99. }
  100. })();