替换<code>让谷歌翻译更准确

谷歌翻译不能正确翻译带<code>的元素,更新修复某些code显示灰底白字看不清

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         替换<code>让谷歌翻译更准确
// @namespace    http://tampermonkey.net/
// @version      1.01
// @description  谷歌翻译不能正确翻译带<code>的元素,更新修复某些code显示灰底白字看不清
// @author       You
// @match        http*://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    const func = ()=>{
        setTimeout(()=>{
            Array.from(document.getElementsByTagName('code')).forEach(code=>{
                const isLongCode = code.offsetWidth > 200
                //document.documentElement.offsetWidth/3
                if (!isLongCode) {
                    const span = document.createElement('span')
                    span.textContent = code.textContent


                    // 获取code的计算样式
                    const codeStyle = window.getComputedStyle(code);

                    // 复制相关样式到span
                    span.style.backgroundColor = codeStyle.backgroundColor;
                    span.style.color = codeStyle.color;
                    span.style.padding = codeStyle.padding;
                    span.style.margin = codeStyle.margin;
                    span.style.border = codeStyle.border;
                    span.style.borderRadius = codeStyle.borderRadius;
                    span.style.fontFamily = codeStyle.fontFamily;
                    span.style.fontSize = codeStyle.fontSize;
                    span.style.fontWeight = codeStyle.fontWeight;
                    span.style.lineHeight = codeStyle.lineHeight;

                    // 替换code元素
                    code.parentNode.insertBefore(span, code)
                    code.parentElement.removeChild(code)
                }
            })

            console.log('替换<code>完成')

        }
                   , 1000)
    }

    func();

    //修改native以拦截popstate事件
    var pushState = history.pushState;
    history.pushState = function() {
        var ret = pushState.apply(history, arguments);
        window.dispatchEvent(new Event("pushstate"));
        window.dispatchEvent(new Event("locationchangefathom"));
        return ret;
    }

    window.addEventListener("popstate", function() {
        window.dispatchEvent(new Event("locationchangefathom"))
    });
    window.addEventListener("locationchangefathom", trackPageview)
    function trackPageview() {
        func();
    }
}
)();