🏠 返回首頁 

Greasy Fork is available in English.

Google Translate replace

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

  1. // ==UserScript==
  2. // @name Google Translate replace
  3. // @name:zh-CN Google翻译 替换
  4. // @namespace https://github.com/Ahaochan/Tampermonkey
  5. // @version 0.0.3
  6. // @description Replace the translated string. Github:https://github.com/Ahaochan/Tampermonkey. Star and fork is welcome.
  7. // @description:zh-CN 对翻译后的字符串进行替换。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. jQuery(function ($) {
  15. 'use strict';
  16. // 1. 初始化之前设置的 pattern 和 replace
  17. let $div = $('<div class="tlid-input-button input-button header-button tlid-input-button-docs documents-icon" role="tab" tabindex="-1">' +
  18. '<div class="text">' +
  19. ' <input id="ahao-pattern" placeholder="pattern" style="width: 50px;"> >>' +
  20. ' <input id="ahao-replace" placeholder="replace" style="width: 50px; margin: 0 4px;">' +
  21. ' <span id="ahao-button">替换</span>' +
  22. '</div></div>');
  23. $('.tlid-input-button-container').append($div);
  24. GM.getValue('pattern').then(function (value) { $('#ahao-pattern').val(value); });
  25. GM.getValue('replace').then(function (value) { $('#ahao-replace').val(value); });
  26. // 2. 绑定替换事件
  27. $('#ahao-button').on('click', function () {
  28. let pattern = $('#ahao-pattern').val();
  29. let replace = $('#ahao-replace').val();
  30. GM.setValue('pattern', pattern);
  31. GM.setValue('replace', replace);
  32. $('.translation').find('span').each(function () {
  33. let $this = $(this);
  34. $this.text($this.text().replace(new RegExp(pattern, 'gm'), replace));
  35. });
  36. });
  37. });