iciba

Translate any text selected into a tooltip

  1. // ==UserScript==
  2. // @id iciba-greasemonkey-reverland-2025-4-9
  3. // @name iciba
  4. // @name:zh-CN 金山词霸取词
  5. // @version 2.0
  6. // @namespace https://github.com/bigleek/icibaDict
  7. // @author bigleek(bigleekcode@gmail.com)
  8. // @description Translate any text selected into a tooltip
  9. // @description:zh-cn 一个可以在浏览器中自由使用的屏幕取词脚本
  10. // @include *
  11. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM.xmlHttpRequest
  14. // @license MIT
  15. // ==/UserScript==
  16.  
  17. window.document.body.addEventListener("mouseup", translate, false);
  18. window.document.body.addEventListener("keyup", toggleYoudao, false);
  19. var toggle = true;
  20.  
  21. function toggleYoudao(e) {
  22. if (e.which == 81 && e.altKey && e.ctrlKey) {
  23. if (toggle) {
  24. window.document.body.removeEventListener("mouseup", translate, false);
  25. toggle = false;
  26. } else {
  27. window.document.body.addEventListener("mouseup", translate, false);
  28. toggle = true;
  29. }
  30. }
  31. }
  32.  
  33. function translate(e) {
  34. // remove previous .youdaoPopup if exists
  35. var previous = document.querySelector(".youdaoPopup");
  36. if (previous) {
  37. document.body.removeChild(previous);
  38. }
  39. //console.log("translate start");
  40. var selectObj = document.getSelection();
  41.  
  42. // if #text node
  43. if (selectObj.anchorNode && selectObj.anchorNode.nodeType == 3) {
  44. //GM_log(selectObj.anchorNode.nodeType.toString());
  45. var word = selectObj.toString();
  46. if (word == "") {
  47. return;
  48. }
  49. // linebreak wordwrap, optimize for pdf.js
  50. word = word.replace('-\n','');
  51. // multiline selection, optimize for pdf.js
  52. word = word.replace('\n', ' ');
  53. //console.log("word:", word);
  54. var ts = new Date().getTime();
  55. //console.log("time: ", ts);
  56. var mx = e.clientX;
  57. var my = e.clientY;
  58. translate(word, ts);
  59.  
  60. }
  61.  
  62. function popup(mx, my, result) {
  63. //console.log(mx)
  64. //console.log(my)
  65. //console.log("popup window!")
  66. var youdaoWindow = document.createElement('div');
  67. youdaoWindow.classList.toggle("youdaoPopup");
  68. // parse
  69. var dictJSON = JSON.parse(result);
  70. console.log(dictJSON);
  71. var paraphrase = dictJSON.message[0].paraphrase;
  72. if (!(paraphrase ==='')) {
  73. word();
  74. } else {
  75. sentence();
  76. }
  77. // main window
  78. // first insert into dom then there is offsetHeight!IMPORTANT!
  79. document.body.appendChild(youdaoWindow);
  80. youdaoWindow.style.color = "black";
  81. youdaoWindow.style.textAlign = "left";
  82. youdaoWindow.style.display = "block";
  83. youdaoWindow.style.position = "fixed";
  84. youdaoWindow.style.background = "lightblue";
  85. youdaoWindow.style.borderRadius = "5px";
  86. youdaoWindow.style.boxShadow = "0 0 5px 0";
  87. youdaoWindow.style.opacity = "0.9";
  88. youdaoWindow.style.width = "200px";
  89. youdaoWindow.style.wordWrap = "break-word";
  90. youdaoWindow.style.left = mx + 10 + "px";
  91. if (mx + 200 + 30 >= window.innerWidth) {
  92. youdaoWindow.style.left = parseInt(youdaoWindow.style.left) - 200 + "px";
  93. }
  94. if (my + youdaoWindow.offsetHeight + 30 >= window.innerHeight) {
  95. youdaoWindow.style.bottom = "20px";
  96. } else {
  97. youdaoWindow.style.top = my + 10 + "px";
  98. }
  99. youdaoWindow.style.padding = "5px";
  100. youdaoWindow.style.zIndex = '999999';
  101.  
  102. function word() {
  103.  
  104. function play(word) {
  105. //console.log("[DEBUG] PLAYOUND")
  106.  
  107. function playSound(buffer) {
  108. var source = context.createBufferSource();
  109. source.buffer = buffer;
  110. source.connect(context.destination);
  111. source.start(0);
  112. }
  113.  
  114. var context = new AudioContext();
  115. var soundUrl = `https://dict.youdao.com/dictvoice?type=2&audio=${word}`;
  116. var p = new Promise(function(resolve, reject) {
  117. var ret = GM.xmlHttpRequest({
  118. method: "GET",
  119. url: soundUrl,
  120. responseType: 'arraybuffer',
  121. onload: function(res) {
  122. try {
  123. context.decodeAudioData(res.response, function(buffer) {
  124. resolve(buffer);
  125. });
  126. } catch(e) {
  127. reject(e);
  128. }
  129. }
  130. });
  131. });
  132. p.then(playSound, function(e) {
  133. console.log(e);
  134. });
  135. }
  136.  
  137. var basic = dictJSON.message[0];
  138. var header = document.createElement('p');
  139. // header
  140. var span = document.createElement('span');
  141. span.innerHTML = "" + dictJSON.message[0].key + "";
  142. header.appendChild(span);
  143. // phonetic if there is
  144. // var phonetic = basic['phonetic'];
  145. if (false) {
  146. var phoneticNode = document.createElement('span');
  147. phoneticNode.innerHTML = '[' + phonetic + ']';
  148. phoneticNode.style.cursor = "pointer";
  149. header.appendChild(phoneticNode);
  150. var playLogo = document.createElement('span');
  151. header.appendChild(phoneticNode);
  152. phoneticNode.addEventListener('mouseup', function(e){
  153. if (e.target === phoneticNode) {
  154. e.stopPropagation();
  155. play(query);
  156. }
  157. }, false);
  158. }
  159. header.style.color = "darkBlue";
  160. header.style.margin = "0";
  161. header.style.padding = "0";
  162. span.style.fontweight = "900";
  163. span.style.color = "black";
  164.  
  165. youdaoWindow.appendChild(header);
  166. var hr = document.createElement('hr');
  167. hr.style.margin = "0";
  168. hr.style.padding = "0";
  169. hr.style.height = "1px";
  170. hr.style.borderTop = "dashed 1px black";
  171. youdaoWindow.appendChild(hr);
  172. var ul = document.createElement('ul');
  173. // ul style
  174. ul.style.margin = "0";
  175. ul.style.padding = "0";
  176. basic['means'].map(function(trans) {
  177. var li = document.createElement('li');
  178. li.style.listStyle = "none";
  179. li.style.margin = "0";
  180. li.style.padding = "0";
  181. li.style.background = "none";
  182. li.style.color = "inherit";
  183. li.appendChild(document.createTextNode(trans.part+trans.means));
  184. ul.appendChild(li);
  185. });
  186. youdaoWindow.appendChild(ul);
  187.  
  188. }
  189.  
  190. function sentence() {
  191. var ul = document.createElement('ul');
  192. // ul style
  193. ul.style.margin = "0";
  194. ul.style.padding = "0";
  195. var sentence = dictJSON.message[0];
  196.  
  197. sentence['means'].map(function(trans) {
  198. var li = document.createElement('li');
  199. li.style.listStyle = "none";
  200. li.style.margin = "0";
  201. li.style.padding = "0";
  202. li.style.background = "none";
  203. li.style.color = "inherit";
  204. li.appendChild(document.createTextNode(trans.part+trans.means));
  205. ul.appendChild(li);
  206. });
  207. youdaoWindow.appendChild(ul);
  208. }
  209. }
  210.  
  211.  
  212. function translate(word, ts) {
  213.  
  214.  
  215. var reqUrl = `https://dict-mobile.iciba.com/interface/index.php?c=word&m=getsuggest&nums=10&is_need_mean=1&word=${word}&ts=${ts}`;
  216. //console.log("request url: ", reqUrl);
  217. var ret = GM.xmlHttpRequest({
  218. method: "GET",
  219. url: reqUrl,
  220. headers: {"Accept": "application/json"}, // can be omitted...
  221. onreadystatechange: function(res) {
  222. //console.log("Request state changed to: " + res.readyState);
  223. },
  224. onload: function(res) {
  225. var retContent = res.response;
  226. //console.log(retContent)
  227. popup(mx, my, retContent);
  228. },
  229. onerror: function(res) {
  230. console.log("error");
  231. }
  232. });
  233. }
  234. }
  235.