Google翻译 替换

对翻译后的字符串进行替换。github:https://github.com/Ahaochan/Tampermonkey,欢迎star和fork。

目前为 2018-04-23 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Google Translate replace
  3. // @name:zh Google翻译 替换
  4. // @name:zh-cn Google翻译 替换
  5. // @name:zh-CN Google翻译 替换
  6. // @namespace https://github.com/Ahaochan/Tampermonkey
  7. // @version 0.0.2
  8. // @description Replace the translated string. Github:https://github.com/Ahaochan/Tampermonkey. Star and fork is welcome.
  9. // @description:zh 对翻译后的字符串进行替换。github:https://github.com/Ahaochan/Tampermonkey,欢迎star和fork。
  10. // @description:zh-cn 对翻译后的字符串进行替换。github:https://github.com/Ahaochan/Tampermonkey,欢迎star和fork。
  11. // @description:zh-CN 对翻译后的字符串进行替换。github:https://github.com/Ahaochan/Tampermonkey,欢迎star和fork。
  12. // @author Ahaochan
  13. // @include http*://translate.google.*
  14. // @grant GM.setValue
  15. // @grant GM.getValue
  16. // @require https://code.jquery.com/jquery-2.2.4.min.js
  17. // ==/UserScript==
  18.  
  19. $(function () {
  20. 'use strict';
  21.  
  22. // 1. 初始化之前设置的 pattern 和 replace
  23. var $div = $('<div class="gt-lang-sugg-message goog-inline-block je"' +
  24. ' style="float: right; padding: 2px">' +
  25. '<input id="ahao-pattern" placeholder="pattern" style="width: 50px;"> >>' +
  26. '<input id="ahao-replace" placeholder="replace" style="width: 50px; margin: 0 4px;">' +
  27. '<input id="ahao-button" type="button" class="jfk-button jfk-button-action" value="replace">' +
  28. '</div>');
  29. $('#gt-lang-right').append($div);
  30. GM.getValue('pattern').then(function (value) { $('#ahao-pattern').val(value); });
  31. GM.getValue('replace').then(function (value) { $('#ahao-replace').val(value); });
  32.  
  33. // 2. 绑定替换事件
  34. var $resultBox = $('#result_box');
  35. $('#ahao-button').on('click', function () {
  36. var pattern = $('#ahao-pattern').val();
  37. var replace = $('#ahao-replace').val();
  38. GM.setValue('pattern', pattern);
  39. GM.setValue('replace', replace);
  40.  
  41. var text = $resultBox.text();
  42. $resultBox.text(text.replace(new RegExp(pattern, 'gm'), replace));
  43. });
  44. });