Greasy Fork

翻译插件——去除换行

add a new button for removing '\n'

目前为 2019-09-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         翻译插件——去除换行
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  add a new button for removing '\n'
// @author       Kevin Chen
// @match        https://fanyi.baidu.com/*
// @match        http://fanyi.youdao.com/
// @match        https://translate.google.cn/*
// @match        https://translate.google.com/*
// @grant        none
// ==/UserScript==

function reload() {
    var txt = "";
    var id = "";
    var host = window.location.host;
    if( host == "fanyi.baidu.com" )
    {
       id = "baidu_translate_input";
    }
    if( host == "fanyi.youdao.com" )
    {
       id = "inputOriginal";
    }
    if( host == "translate.google.cn" || host == "translate.google.com" )
    {
       id = "source";
    }

    txt = document.getElementById(id).value;
    for (var i=0;i<txt.length;i++)
    {
        if(txt.indexOf("\n")){
            txt = txt.replace("\n"," ");
        }
    }
    document.getElementById(id).value = txt;
    setTimeout(location.reload,1);
};

function str2elem(some_html) {
    var d = document.createElement('div');
    d.innerHTML = some_html;
    return d.firstChild;
}

function createGoogleButton(txt) {
    // Create new button
    var new_button = document.createElement("div");
    new_button.setAttribute("class", "tlid-input-button input-button header-button tlid-input-button-docs documents-icon");
    new_button.setAttribute("role", "tab");
    new_button.setAttribute("tabindex", "-1");
    new_button.onclick = reload;
    // Create text of new button
    var text = document.createElement("div");
    text.setAttribute("class", "text");
    text.innerHTML = txt;
    new_button.appendChild(text);
    return new_button;
}

function createBaiduButton(txt) {
    var new_button = str2elem('<a href="javascript:" class="manual-trans-btn">' + txt + '</a>');
    new_button.style.textAlign = "center";
    new_button.style.height = "30px";
    new_button.style.lineHeight = "30px";
    new_button.style.fontSize = "14px";
    new_button.style.color = "#4395ff";
    new_button.style.backgroundColor = "#fff";
    new_button.style.border = "1px solid #4395ff";
    new_button.style.borderRadius = "3px";
    new_button.onclick = reload;
    return new_button;
}

function createYoudaoButton(txt) {
    var new_button = str2elem('<a class="fanyi__operations--man clog-js" data-clog="AT_BUTTON_CLICK" data-pos="web.i.top" id="transMan" href="javascript:;">' + txt + '</a>');
    new_button.onclick = reload;
    return new_button;
}

function main() {
    var host = window.location.host;
    var container = null;
    var new_button = null;
    if(host == "translate.google.cn" || host == "translate.google.com"){
        new_button = createGoogleButton("格式化");
        // Get container
        var container1 = document.querySelector("body > div.frame > div.page.tlid-homepage.homepage.translate-text > div.input-button-container > div")
        var container2 = document.querySelector("body > div.frame > div.page.tlid-homepage.homepage.translate-docs > div.input-button-container > div")
        container = container1 || container2;
    }
    else if (host == "fanyi.baidu.com") {
        new_button = createBaiduButton("格式化");
        container = document.querySelector("#main-outer > div > div > div.translate-wrap > div.trans-operation-wrapper.clearfix > div.trans-operation.clearfix");
    }
    else if (host == "fanyi.youdao.com") {
        new_button = createYoudaoButton("格式化");
        container = document.querySelector("body > div.fanyi > div.fanyi__operations > div.fanyi__operations--left");
    }
    container.appendChild(new_button);
}

setTimeout(main, 10)