Google Translate replace

Replace the translated string. Github:https://github.com/Ahaochan/Tampermonkey. Star and fork is welcome.

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

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