Revit API 翻译

Translate Summary!

目前为 2020-02-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Revit API 翻译
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Translate Summary!
  6. // @author Zero
  7. // @match https://www.revitapidocs.com/*
  8. // @connect dict.youdao.com
  9. // @connect translate.google.cn
  10. // @grant GM_xmlhttpRequest
  11. // @require https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16. var $ = window.jQuery;
  17. var googleUrl = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&dt=bd&dj=1&source=input&hl=zh-CN&sl=auto&tl=';
  18.  
  19. addButton();
  20.  
  21. //页面变动事件
  22. $("#api-title").bind("DOMNodeInserted",function(){
  23. setTimeout(function(){
  24. addButton();
  25. },600);
  26. });
  27.  
  28. //添加翻译按钮
  29. function addButton(){
  30. //$(".descriptionColumn").append("<a id='btn_tran'>>>翻译</a>");
  31. //$("#btn_tran").on("click", function(){
  32. tranSummary();
  33. //});
  34.  
  35. }
  36.  
  37. //翻译进程
  38. function tranSummary () {
  39. var en_text = "";
  40.  
  41. $(".summary").each(function(){
  42. en_text += $(this).text() + "||";
  43. });
  44.  
  45. ajax2(googleUrl + 'zh-CN&q=', encodeURIComponent(en_text));
  46. }
  47.  
  48. function ajax2(url, text, data) {
  49. GM_xmlhttpRequest({
  50. method: 'GET',
  51. url: url + text,
  52. headers: { 'cookie': '' },
  53. data: data,
  54. onload: function (res) {
  55. googleTran(res.responseText);
  56. },
  57. onerror: function (res) {
  58. alert("连接失败");
  59. }
  60. });
  61.  
  62. }
  63.  
  64. // 谷歌翻译 引擎
  65. function googleTran(rst) {
  66. var json = JSON.parse(rst),
  67. html = '';
  68.  
  69. for (var i = 0; i < json.sentences.length; i++) {
  70. html += json.sentences[i].trans;
  71. }
  72. var cn_texts = html.split("||");
  73. cn_texts.forEach(function(text){
  74.  
  75. });
  76. $(".summary").each(function(i,item){
  77. var txt = "<div>" + cn_texts[i].replace(/^\s*|\s*$/g, '') + "</div>";
  78. $(this).append(txt);
  79. });
  80. }
  81. })();