COC2 翻译分段修复

修复 Google 翻译导致段落合并的问题

目前为 2024-10-15 提交的版本。查看 最新版本

// ==UserScript==
// @name        COC2 翻译分段修复
// @namespace   http://tampermonkey.net/
// @version     2024-10-10
// @description 修复 Google 翻译导致段落合并的问题
// @author      LinHQ
// @match       *://*/*
// @run-at      context-menu
// @license     AGPL-v3
// ==/UserScript==

(function () {
  'use strict';
  new MutationObserver((li, ob) => {
    try {
      li.forEach((mu) => {
        if (mu.type !== 'childList') return
        const nodes = mu.addedNodes
        nodes.forEach(node => {
          // 翻译会为 br 处理换行
          let t
          if (node.nodeType === 3) {
            t = node.parentNode
          } else if (node?.querySelector('.mainText')) {
            t = node?.querySelector('.mainText')
          }

          if (!t?.matches('.mainText')) return

          ob.disconnect()
          t.innerHTML = t.innerHTML?.replaceAll('\n', '<br>') ?? t.innerHTML
          ob.observe(document, {
            subtree: true,
            childList: true
          })
        })
      })
    } catch (e) {
      console.warn('Script Error!', e)
    }
  }).observe(document, {
    subtree: true,
    childList: true
  })
})();