google翻译实用工具

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

当前为 2018-12-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name google translate utils
  3. // @name:zh-CN google翻译实用工具
  4. // @namespace https://github.com/tabedit/tamperMonkey
  5. // @version 0.3
  6. // @description auto remove line break for google translate(replaced with space)
  7. // @description:zh-CN 自动移除google翻译原文中的换行符(替换为空格)
  8. // @author tabedit
  9. // @include http*://translate.google.*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // all element and other variable
  17. var textarea = document.getElementById('source'),
  18. container = document.querySelector('body > div.frame > div.page.tlid-homepage.homepage.translate-text > div.input-button-container'),
  19. customButton = document.createElement('div'),
  20. replaceOn = false;
  21.  
  22. // function for replace word
  23. function replaceWord(){
  24. if (typeof replaceWord.innerFunc === 'undefined'){
  25. replaceWord.innerFunc = function (){
  26. textarea.value = textarea.value.replace(/-[\r\n]/g,'').replace(/[\r\n]/g,' ');
  27. }
  28. }
  29. setTimeout(replaceWord.innerFunc, 200);
  30. }
  31.  
  32. // button for switch whether turn replace on
  33. customButton.addEventListener('click',function(event){
  34. if(!replaceOn){
  35. replaceOn = true;
  36. customButton.style.backgroundColor='#E4ECFA';
  37. textarea.addEventListener('paste', replaceWord);
  38. replaceWord();
  39. }else{
  40. replaceOn = false;
  41. customButton.style.backgroundColor='#FAFAFA';
  42. textarea.removeEventListener('paste', replaceWord);
  43. }
  44. });
  45. customButton.className = 'tlid-input-button input-button header-button tlid-input-button-docs';
  46. customButton.appendChild(document.createElement('div'));
  47. customButton.style.paddingLeft = '16px';
  48. customButton.childNodes[0].innerHTML = '自动替换换行';
  49. customButton.childNodes[0].className = 'text';
  50. container.appendChild(customButton);
  51. })();