fanyi

翻译库

Acest script nu ar trebui instalat direct. Aceasta este o bibliotecă pentru alte scripturi care este inclusă prin directiva meta a // @require https://update.greasyfork.org/scripts/503008/1471344/fanyi.js

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

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         fanyi
// @namespace    fireloong
// @version      0.0.16
// @description  翻译库
// @author       Itsky71
// @grant        none
// @license      MIT
// ==/UserScript==

let doc_version = window.location.pathname.split('/')[2];

if(doc_version === 'current') {
    doc_version = '7.1';
} else if(doc_version === '5.x') {
    doc_version = '5.4';
}

function fanyi(translates, id, dev = false, delay = false){
    let doc;
    if(typeof id === 'number'){
        switch(id){
            case 1:
                doc = '.toctree a,.ui-heading > h1,.ui-prose > .section p,.ui-prose > .section li,.ui-prose > .section h2>a,.ui-prose > .section h3>a,.ui-prose > .section h4>a,.ui-prose > .section dl>dt,.ui-prose > .section dl > dd,.hljs-comment';
                break;
            case 2:
                doc = '.ui-heading > h1,.ui-prose .section > h2 > a,.ui-prose p,.pagination-item > a,.pagination-item > span,.ui-prose li';
                break;
            default:
                break;
        }
    } else {
        doc = id;
    }
    
    const myFunc = (bb) => {
        $(bb).html(translates[$(bb).text()]);
    };

    let n = 0;
    let jsonStr = '';
    $(doc).each(function(i,v){
        if(translates.hasOwnProperty($(this).text())) {
            if(typeof delay === 'number'){
                setTimeout(myFunc, delay, this);
            } else {
                $(this).html(translates[$(this).text()]);
            }
        } else if(dev) {
            console.log(n,v,$(this).text());
            jsonStr += '\''+$(this).text().replaceAll('\\','\\\\').replaceAll('\'','\\\'').replaceAll("\n",'\\n')+'\': \'\','+"\n";
            n++;
        }
    });
    if(dev){
        console.info(jsonStr);
    }
}

// 精准查找,并翻译
function fanyi_jing(translates){
    $.each(translates, function(k,v){
        $(k).html(function(){
            //const regex = new RegExp(v[0], 'g');
            return $(this).html().replace(v[0], v[1]);
        });
    });
}