Fix Edge Translator Inline Code (only github)

Fix Microsoft Edge translator inline <code> reorder bug and preserve original inline code text.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Fix Edge Translator Inline Code (only github)
// @name:zh-CN   修复 Edge 翻译器翻译github行内代码错位与误翻译
// @namespace    https://github.com/你的用户名
// @version      1.0.0
// @description  Fix Microsoft Edge translator inline <code> reorder bug and preserve original inline code text.
// @description:zh-CN 修复 Edge 全文翻译时行内 <code> 被移动到句尾、代码内容被错误翻译的问题。
// @author       你的名字
// @license      MIT
// @match        https://github.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    const WRAPPER_CLASS = 'edge-code-wrapper';
    const ORIGINAL_ATTR = 'data-edge-original-code';

    function processCode(code) {
        if (!(code instanceof HTMLElement)) {
            return;
        }

        if (code.tagName !== 'CODE') {
            return;
        }

        // 跳过真正的代码块
        if (code.closest('pre')) {
            return;
        }

        // 已处理
        if (
            code.parentElement &&
            code.parentElement.classList.contains(WRAPPER_CLASS)
        ) {
            return;
        }

        // 保存原始文本
        code.setAttribute(ORIGINAL_ATTR, code.textContent);

        // 包一层 span,避免 Edge 把 code 移到句尾
        const wrapper = document.createElement('span');
        wrapper.className = WRAPPER_CLASS;

        code.parentNode.insertBefore(wrapper, code);
        wrapper.appendChild(code);
    }

    function restoreCode(code) {
        if (!(code instanceof HTMLElement)) {
            return;
        }

        if (code.tagName !== 'CODE') {
            return;
        }

        const original = code.getAttribute(ORIGINAL_ATTR);

        if (original === null) {
            return;
        }

        // Edge 翻译器改了内容,就恢复
        if (code.textContent !== original) {
            code.textContent = original;
        }
    }

    function processRoot(root) {
        if (!(root instanceof Element)) {
            return;
        }

        if (root.matches('code')) {
            processCode(root);
            restoreCode(root);
        }

        root.querySelectorAll('code').forEach(code => {
            processCode(code);
            restoreCode(code);
        });
    }

    // 初始处理
    document.querySelectorAll('code').forEach(processCode);

    // 监听 Edge 翻译和 GitHub 动态更新
    const observer = new MutationObserver(mutations => {
        for (const mutation of mutations) {

            // 文本被翻译时
            if (mutation.type === 'characterData') {
                const parent = mutation.target.parentElement;

                if (parent && parent.tagName === 'CODE') {
                    restoreCode(parent);
                }
            }

            // DOM 被新增/替换时
            for (const node of mutation.addedNodes) {
                processRoot(node);
            }

            // 某些翻译实现会直接替换 code 子节点
            if (
                mutation.type === 'childList' &&
                mutation.target instanceof HTMLElement &&
                mutation.target.tagName === 'CODE'
            ) {
                restoreCode(mutation.target);
            }
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
        characterData: true
    });
})();