GTranslate Helper

format the paste from pdf.

目前为 2018-07-14 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GTranslate Helper
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2.2
  5. // @description format the paste from pdf.
  6. // @author els_angel
  7. // @match *://translate.google.com/*
  8. // @grant none
  9. // ==/UserScript==
  10. var source = document.getElementById('source');
  11. function handlePaste (e) {
  12. let clipboardData, pastedData;
  13.  
  14. // Stop data actually being pasted into div
  15. e.stopPropagation();
  16. e.preventDefault();
  17.  
  18. // Get pasted data via clipboard API
  19. clipboardData = e.clipboardData || window.clipboardData;
  20. pastedData = clipboardData.getData('Text');
  21. let lines = pastedData.split('\n');
  22. let lLen = lines.length;
  23. let out = "";
  24. if(lLen === 1){
  25. out = pastedData.replace(/[\r\n]+/g," ");
  26. }else{
  27. let maxLen = -1;
  28. for(let i=0;i<lLen;i++){
  29. let liLen = lines[i].length;
  30. if(liLen>maxLen){
  31. maxLen = liLen;
  32. }
  33. }
  34. let xLen = maxLen*3/4;
  35. for(let i=0;i<lLen;i++){
  36. let liLen = lines[i].length;
  37. let rLine = lines[i].replace(/[\r\n]+/g,"");
  38. let e = rLine.charAt(rLine.length-1);
  39. if(liLen<=xLen&&(e==='.'||e===':'||e==='?'||e==='!')){
  40. out+=rLine+"\n";
  41. }else{
  42. out+=rLine+" ";
  43. }
  44. }
  45. }
  46. // Do whatever with pasteddata
  47. let start = source.selectionStart;
  48. // obtain the index of the last selected character
  49. let finish = source.selectionEnd;
  50. let str = source.value;
  51. let final = str.substring(0,start)+out+str.substring(finish,str.length);
  52. // obtain the selected text
  53. source.value=final;
  54. source.focus();
  55. source.selectionEnd = start + out.length;
  56. }
  57. (function() {
  58. 'use strict';
  59.  
  60. // Your code here...
  61. source.addEventListener('paste', handlePaste);
  62. })();
  63.  
  64.  
  65.  
  66.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址