fanyi

翻译库

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/503008/1471344/fanyi.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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]);
        });
    });
}