智能划词翻译 by xinggsf

划词翻译,使用有道词典api

目前为 2022-01-27 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 智能划词翻译 by xinggsf
  3. // @namespace https://gf.qytechs.cn/zh-CN/users/869804
  4. // @version 0.0.1
  5. // @description 划词翻译,使用有道词典api
  6. // @author zhaowenlu xinggsf
  7. // @include http*
  8. // @run-at document-start
  9. // @connect dict.youdao.com
  10. // @grant GM_xmlhttpRequest
  11. // ==/UserScript==
  12.  
  13. 'use strict';
  14. // const youdaoUrl = 'http://dict.youdao.com/jsonapi?xmlVersion=5.1&dicts={"count":99,"dicts":[["ec"]]}&jsonversion=2&q=';
  15. const youdaoUrl = 'http://dict.youdao.com/jsonapi?jsonversion=2&client=mobile&dicts=%7B%22count%22%3A99%2C%22dicts%22%3A%5B%5B%22ec%22%2C%22ce%22%2C%22newcj%22%2C%22newjc%22%2C%22kc%22%2C%22ck%22%2C%22fc%22%2C%22cf%22%2C%22multle%22%2C%22jtj%22%2C%22pic_dict%22%2C%22tc%22%2C%22ct%22%2C%22typos%22%2C%22special%22%2C%22tcb%22%2C%22baike%22%2C%22lang%22%2C%22simple%22%2C%22wordform%22%2C%22exam_dict%22%2C%22ctc%22%2C%22web_search%22%2C%22auth_sents_part%22%2C%22ec21%22%2C%22phrs%22%2C%22input%22%2C%22wikipedia_digest%22%2C%22ee%22%2C%22collins%22%2C%22ugc%22%2C%22media_sents_part%22%2C%22syno%22%2C%22rel_word%22%2C%22longman%22%2C%22ce_new%22%2C%22le%22%2C%22newcj_sents%22%2C%22blng_sents_part%22%2C%22hh%22%5D%2C%5B%22ugc%22%5D%2C%5B%22longman%22%5D%2C%5B%22newjc%22%5D%2C%5B%22newcj%22%5D%2C%5B%22web_trans%22%5D%2C%5B%22fanyi%22%5D%5D%7D&keyfrom=mdict.7.2.0.android&model=honor&mid=5.6.1&imei=659135764921685&vendor=wandoujia&screen=1080x1800&ssid=superman&network=wifi&abtest=2&xmlVersion=5.1&q='
  16. const googleUrl = 'https://translate.google.cn/translate_a/single?client=gtx&dt=t&dt=bd&dj=1&source=input&hl=zh-CN&sl=auto&tl=';
  17. const reHZ = /^[\u4E00-\u9FA5\uFF00-\uFF20\u3000-\u301C]/;
  18.  
  19. const countOfWord = s => s ? s.split(/\s+/).length : 0;
  20. const isChina = s => reHZ.test(s);
  21. // 翻译结果面板
  22. class TranslateTip {
  23. constructor() {
  24. const div = document.createElement('div');
  25. div.hidden = true;
  26. div.setAttribute('style',
  27. `position:absolute!important;
  28. font-size:13px!important;
  29. overflow:auto!important;
  30. background:#fff!important;
  31. font-family:sans-serif,Arial!important;
  32. font-weight:normal!important;
  33. text-align:left!important;
  34. color:#000!important;
  35. padding:0.5em 1em!important;
  36. line-height:1.5em!important;
  37. border-radius:5px!important;
  38. border:1px solid #ccc!important;
  39. box-shadow:4px 4px 8px #888!important;
  40. max-width:350px!important;
  41. max-height:216px!important;
  42. z-index:2147483647!important;`
  43. );
  44. document.documentElement.appendChild(div);
  45. //点击了翻译内容面板,不再创建翻译图标
  46. div.addEventListener('mouseup', e => e.stopPropagation());
  47. this._tip = div;
  48. }
  49. showText(text) { //显示翻译文本
  50. this._tip.innerHTML = text;
  51. this._tip.hidden = !1;
  52. }
  53. hide() {
  54. this._tip.innerHTML = '';
  55. this._tip.hidden = true;
  56. }
  57. pop(ev) {
  58. this._tip.style.top = ev.pageY + 'px';
  59. //面板最大宽度为350px
  60. this._tip.style.left = (ev.pageX + 350 <= document.body.clientWidth ?
  61. ev.pageX : document.body.clientWidth - 350) + 'px';
  62. }
  63. }
  64. const tip = new TranslateTip();
  65.  
  66. class Icon {
  67. constructor() {
  68. const icon = document.createElement('span');
  69. icon.hidden = true;
  70. icon.innerHTML = `<svg style="margin:4px !important;" "width="24" height="24" viewBox="0 0 768 768">
  71. <path d="M672 640.5v-417c0-18-13.5-31.5-31.5-31.5h-282l37.5 129h61.5v-33h34.5v33h115.5v33h-40.5c-10.5 40.5-33 79.5-61.5 112.5l87 85.5-22.5 24-87-85.5-28.5 28.5 25.5 88.5-64.5 64.5h225c18 0 31.5-13.5 31.5-31.5zM447 388.5c7.5 15 19.5 34.5 36 54 39-46.5 49.5-88.5 49.5-88.5h-127.5l10.5 34.5h31.5zM423 412.5l19.5 70.5 18-16.5c-15-16.5-27-34.5-37.5-54zM355.5 339c0-7.381-0.211-16.921-3-22.5h-126v49.5h70.5c-4.5 19.5-24 48-67.5 48-42 0-76.5-36-76.5-78s34.5-78 76.5-78c24 0 39 10.5 48 19.5l3 1.5 39-37.5-3-1.5c-24-22.5-54-34.5-87-34.5-72 0-130.5 58.5-130.5 130.5s58.5 130.5 130.5 130.5c73.5 0 126-52.5 126-127.5zM640.5 160.5c34.5 0 63 28.5 63 63v417c0 34.5-28.5 63-63 63h-256.5l-31.5-96h-225c-34.5 0-63-28.5-63-63v-417c0-34.5 28.5-63 63-63h192l28.5 96h292.5z" style="fill:#3e84f4;"></svg>`;
  72. icon.setAttribute('style',
  73. `width:32px!important;
  74. height:32px!important;
  75. background:#fff!important;
  76. border-radius:50%!important;
  77. box-shadow:4px 4px 8px #888!important;
  78. position:absolute!important;
  79. z-index:2147483647!important;`
  80. );
  81. document.documentElement.appendChild(icon);
  82. //拦截二个鼠标事件,以防止选中的文本消失
  83. icon.addEventListener('mousedown', e => e.preventDefault(), true);
  84. icon.addEventListener('mouseup', ev => ev.preventDefault(), true);
  85. icon.addEventListener('click', ev => {
  86. if (ev.ctrlKey) navigator.clipboard.readText()
  87. .then(text => {
  88. this.queryText(text.trim(),ev);
  89. })
  90. .catch(err => {
  91. console.error('Failed to read clipboard contents: ', err);
  92. });
  93. else {
  94. const text = window.getSelection().toString().trim().replace(/\s{2,}/g, ' ');
  95. this.queryText(text,ev);
  96. }
  97. });
  98. this._icon = icon;
  99. }
  100. pop(ev) {
  101. const icon = this._icon;
  102. icon.style.top = ev.pageY + 12 + 'px';
  103. icon.style.left = ev.pageX + 'px';
  104. icon.hidden = !1;
  105. setTimeout(this.hide.bind(this), 2e3);
  106. }
  107. hide() {
  108. this._icon.hidden = true;
  109. }
  110. queryText(text,ev) {
  111. if (text) {
  112. this._icon.hidden = true;
  113. tip.pop(ev);
  114. //const url = isChina(text) ? googleUrl +'en&q=' : countOfWord(text) == 1 ? youdaoUrl : googleUrl +'zh-CN&q=';
  115. const url = youdaoUrl;
  116. ajax(url, text);
  117. }
  118. }
  119. }
  120. const icon = new Icon();
  121.  
  122. document.addEventListener('mouseup', function(e) {
  123. var text = window.getSelection().toString().trim();
  124. if (!text) {
  125. icon.hide();
  126. tip.hide();
  127. }
  128. else icon.pop(e);
  129. });
  130.  
  131. function ajax(url, text) {
  132. const isYd = url.includes('//dict.youdao.');
  133. url += encodeURIComponent(text);
  134. console.log(url + "\n" + text);
  135. GM_xmlhttpRequest({
  136. method: 'GET',
  137. responseType: 'json',
  138. url: url,
  139. onload: function(res) {
  140. //isYd ? youdao(res.response, text) : google(res.response);
  141. youdao(res.response, text);
  142. },
  143. onerror: function(res) {
  144. console.log(res);
  145. tip.showText("连接失败\nURL: "+ url);
  146. }
  147. });
  148. }
  149.  
  150. function youdao(rst, text) {
  151. if (!rst) {
  152. //ajax(googleUrl + 'zh-CN&q=', text);
  153. return;
  154. }
  155. let html = '';
  156. if(!!rst.fanyi){
  157. html += rst.fanyi.tran;
  158. tip.showText(html);
  159. return;
  160. }
  161. if(!!rst.ce){
  162. let trsce = rst.ce.word[0].trs;
  163. html += trsce.map(el => el.tr[0].l.i.map(w => w["#text"]).join(' ')).join('<br />');
  164. tip.showText(html);
  165. return;
  166. }
  167. if(!!rst.ec){
  168. const { trs, ukphone, usphone, phone } = rst.ec.word[0];
  169. if (!!ukphone && ukphone.length > 0) {
  170. html = `<span style="color:#9E9E9E !important;">英[${ukphone}]</span>`;
  171. }
  172. if (!!usphone && usphone.length > 0) {
  173. html += `<span style="color:#9E9E9E !important;">美[${usphone}]</span>`;
  174. }
  175. if (html.length > 0) {
  176. html += '<br />';
  177. } else if (!!phone && phone.length > 0) {
  178. html += `<span style="color:#9E9E9E !important;">[${phone}]</span>`;
  179. }
  180. html += trs.map(el => el.tr[0].l.i[0]).join('<br />');
  181. tip.showText(html);
  182. return;
  183. }
  184. tip.showText(text);
  185. }
  186.  
  187. function google(rst) {
  188. console.log(rst)
  189. let k, html = '';
  190. for (k of rst.sentences) html += k.trans;
  191. tip.showText(html);
  192. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址