Greasy Fork is available in English.

翻译插件——去除换行

在谷歌翻译、百度翻译、网易有道翻译的页面上增加了一个“格式化”按钮,用来移除从PDF等复制过来的文本中包含的回车符、换行符、"\n"

您查看的为 2020-10-07 05:03:32 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.3.0
// @description  在谷歌翻译、百度翻译、网易有道翻译的页面上增加了一个“格式化”按钮,用来移除从PDF等复制过来的文本中包含的回车符、换行符、"\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==

// Convert string to web element
function parseDom(arg) {
    var d = document.createElement('div');
    d.innerHTML = arg;
    return d.firstChild;
}

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

const FORMAT_CN = "格式化";
const FORMAT_EN = "Format";

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

// Create new button
function createButton(host, txt) {
    var new_button = null;
    switch(host) {
        case GOOGLE_TRANSLATE_CN:
        case GOOGLE_TRANSLATE:
            var FORMAT = host == GOOGLE_TRANSLATE_CN ? FORMAT_CN : FORMAT_EN;
            var buttonHtml = "<div class='tlid-input-button input-button header-button tlid-input-button-docs text-icon' role='tab' tabindex='-1'><div class='text'>" + FORMAT + "</div></div>"
            new_button = parseDom(buttonHtml);
            new_button.onclick = format;
            break;
        case BAIDU_FANYI:
            var baidu_fanyi_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";
            new_button = parseDom(`<a href="javascript:" style="${baidu_fanyi_css}">${FORMAT_CN}</a>`);
            new_button.onclick = function() {
                var translate_button = document.querySelector("#translate-button");
                format();
                translate_button.click();
            };
            break;
        case YOUDAO_FANYI:
            new_button = parseDom(`<a class="fanyi__operations--man clog-js" data-clog="AT_BUTTON_CLICK" data-pos="web.i.top" id="transMan" href="javascript:;"> ${FORMAT_CN} </a>`);
            new_button.onclick = function() {
                var translate_button = document.querySelector("#transMachine");
                format();
                translate_button.click();
            }
            break;
        default: break;
    }
    return new_button;
}

// Get container
function getContainer(host) {
    var container = null;
    switch(host) {
        case GOOGLE_TRANSLATE_CN:
        case GOOGLE_TRANSLATE:
            container = document.querySelector("body > div.container > div.frame > div.page.tlid-homepage.homepage.translate-text > div.input-button-container > div")
            break;
        case BAIDU_FANYI:
            container = document.querySelector("#main-outer > div > div > div.translate-wrap > div.trans-operation-wrapper.clearfix > div.trans-operation.clearfix");
            break;
        case YOUDAO_FANYI:
            container = document.querySelector("body > div.fanyi > div.fanyi__operations > div.fanyi__operations--left");
            break;
        default: break;
    }
    return container;
}

(function(){
    var host = window.location.host;
    var new_button = createButton(host);
    var container = getContainer(host);
    container.appendChild(new_button);
})();