Google翻译 替换

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

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

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