Greasy Fork is available in English.

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

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

// ==UserScript==
// @name         替换<code>让谷歌翻译更准确
// @namespace    http://tampermonkey.net/
// @version      0.43
// @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
                    span.style.cssText = "background-color: #f1f1f1;";
                    const isWhiteText = getComputedStyle(code).color === 'rgb(255, 255, 255)'
                    span.style.color = "black";
                    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();
    }
}
)();