google翻译实用工具

自动移除google翻译原文中的换行符(替换为空格)

当前为 2020-08-20 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         google translate utils
// @name:zh-CN   google翻译实用工具
// @namespace    https://github.com/tabedit/tamperMonkey
// @version      0.4
// @description  auto remove line break for google translate(replaced with space)
// @description:zh-CN 自动移除google翻译原文中的换行符(替换为空格)
// @author       tabedit
// @include     http*://translate.google.*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // all element and other variable
    var textarea = document.getElementById('source'),
    container = document.querySelector('body > div.container > div.frame > div.page.tlid-homepage.homepage.translate-text > div.input-button-container'),
    customButton = document.createElement('div'),
    replaceOn = false;

    // function for replace word
    function replaceWord(){
        if (typeof replaceWord.innerFunc === 'undefined'){
            replaceWord.innerFunc = function (){
                textarea.value = textarea.value.replace(/-[\r\n]/g,'').replace(/[\r\n]/g,' ');
            }
        }
        setTimeout(replaceWord.innerFunc, 200);
    }

    // button for switch whether turn replace on
    customButton.addEventListener('click',function(event){
        if(!replaceOn){
            replaceOn = true;
            customButton.style.backgroundColor='#E4ECFA';
            textarea.addEventListener('paste', replaceWord);
            replaceWord();
        }else{
            replaceOn = false;
            customButton.style.backgroundColor='#FAFAFA';
            textarea.removeEventListener('paste', replaceWord);
        }
    });
    customButton.className = 'tlid-input-button input-button header-button tlid-input-button-docs';
    customButton.appendChild(document.createElement('div'));
    customButton.style.paddingLeft = '16px';
    customButton.childNodes[0].innerHTML = '自动替换换行';
    customButton.childNodes[0].className = 'text';
    container.appendChild(customButton);
})();