Greasy Fork is available in English.

翻译插件——去除换行

add a new button for removing '\n'

您查看的为 2020-10-07 05:03:31 UTC 提交的版本。查看 最新版本

// ==UserScript==
// @name         翻译插件——去除换行
// @namespace    https://greasyfork.org/zh-CN/scripts/390059-%E7%BF%BB%E8%AF%91%E6%8F%92%E4%BB%B6-%E5%8E%BB%E9%99%A4%E6%8D%A2%E8%A1%8C
// @version      1.2.1
// @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/*
// @icon         https://translate.google.cn/favicon.ico
// @grant        none
// @run-at       document-end
// ==/UserScript==

const BAIDU_FANYI = "fanyi.baidu.com";
const GOOGLE_TRANSLATE = "translate.google.com";
const GOOGLE_TRANSLATE_CN = "translate.google.cn";
const YOUDAO_FANYI = "fanyi.youdao.com";

const format = function() {
    const getId = {
        [BAIDU_FANYI]: "baidu_translate_input",
        [GOOGLE_TRANSLATE]: "source",
        [GOOGLE_TRANSLATE_CN]: "source",
        [YOUDAO_FANYI]: "inputOriginal"
    }
    const host = window.location.host;
    const id = getId[host];
    var 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;
};

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

function createGoogleButton(txt) {
    // Create new button
    txt = txt || "格式化"
    var buttonHtml = "<div class='tlid-input-button input-button header-button tlid-input-button-docs documents-icon' role='tab' tabindex='-1'><div class='text'>" + txt + "</div></div>"
    var buttonDom = parseDom(buttonHtml);
    buttonDom.addEventListener("click", format, false);
    return buttonDom;
}

function createBaiduButton(txt) {
    txt = txt || "格式化"
    var css = "text-align: center; margin-left: 14px; width: 106px; height: 30px; line-height: 30px; font-size: 14px; color: #4395ff; letter-spacing: 2px; background-color: #f9f9f9; border: 1px solid #4395ff; border-radius: 3px";
    var new_button = parseDom(`<a href="javascript:" style="${css}">${txt}</a>`);
    new_button.onclick = format;
    return new_button;
}

function createYoudaoButton(txt) {
    txt = txt || "格式化"
    var new_button = parseDom(`<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 = format;
    return new_button;
}

(function main() {
    var host = window.location.host;
    var container = null;
    var new_button = null;
    if(host == GOOGLE_TRANSLATE_CN || host == GOOGLE_TRANSLATE){
        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 == BAIDU_FANYI) {
        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 == YOUDAO_FANYI) {
        new_button = createYoudaoButton();
        container = document.querySelector("body > div.fanyi > div.fanyi__operations > div.fanyi__operations--left");
    }
    container.appendChild(new_button);
})();