Google翻译 替换

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

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

  1. // ==UserScript==
  2. // @name Google翻译 替换
  3. // @name:ja Google Translate replace
  4. // @name:en Google Translate replace
  5. // @namespace https://github.com/Ahaochan/Tampermonkey
  6. // @version 0.0.1
  7. // @description 对翻译后的字符串进行替换。github:https://github.com/Ahaochan/Tampermonkey,欢迎star和fork。
  8. // @description:ja Replace the translated string. Github:https://github.com/Ahaochan/Tampermonkey. Star and fork is welcome.
  9. // @description:en Replace the translated string. Github:https://github.com/Ahaochan/Tampermonkey. Star and fork is welcome.
  10. // @author Ahaochan
  11. // @include *://translate.google.*
  12. // @grant GM_setValue
  13. // @grant GM_getValue
  14. // @require https://code.jquery.com/jquery-2.2.4.min.js
  15. // ==/UserScript==
  16.  
  17. (function ($) {
  18. 'use strict';
  19.  
  20. var pattern = GM_getValue('pattern', ' ');
  21. var replace = GM_getValue('replace', '_');
  22.  
  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;" value="'+pattern+'"> >>' +
  26. '<input id="ahao-replace" placeholder="replace" style="width: 50px; margin: 0 4px;" value="'+replace+'">' +
  27. '<input id="ahao-button" type="button" class="jfk-button jfk-button-action" value="replace">' +
  28. '</div>');
  29. var $resultBox = $('#result_box');
  30.  
  31. $div.find('#ahao-button').on('click', function () {
  32. var pattern = $div.find('#ahao-pattern').val();
  33. var replace = $div.find('#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.  
  41. $('#gt-lang-right').append($div);
  42. })(jQuery);