Greasy Fork 还支持 简体中文。

nastoolNameTest

NasTools名称测试划词版

目前為 2023-04-13 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name nastoolNameTest
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description NasTools名称测试划词版
  6. // @author yubanmeiqin9048
  7. // @include http*
  8. // @grant GM_log
  9. // @grant GM_xmlhttpRequest
  10. // @connect *
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14.  
  15. (function() {
  16. 'use strict';
  17. //nastool地址
  18. const nastoolUrl = 'http://localhost:3000';
  19. // 结果面板
  20. class TranslateTip {
  21. constructor() {
  22. const div = document.createElement('div');
  23. div.hidden = true;
  24. div.setAttribute('style',
  25. `position:absolute!important;
  26. font-size:13px!important;
  27. overflow:auto!important;
  28. background:#fff!important;
  29. font-family:sans-serif,Arial!important;
  30. font-weight:normal!important;
  31. text-align:left!important;
  32. color:#000!important;
  33. padding:0.5em 1em!important;
  34. line-height:1.5em!important;
  35. border-radius:5px!important;
  36. border:1px solid #ccc!important;
  37. box-shadow:4px 4px 8px #888!important;
  38. max-width:350px!important;
  39. max-height:216px!important;
  40. z-index:2147483647!important;`
  41. );
  42. document.documentElement.appendChild(div);
  43. //点击了内容面板,不再创建图标
  44. div.addEventListener('mouseup', e => e.stopPropagation());
  45. this._tip = div;
  46. }
  47. showText(text) { //显示翻译文本
  48. this._tip.innerHTML = text;
  49. this._tip.hidden = !1;
  50. }
  51. hide() {
  52. this._tip.innerHTML = '';
  53. this._tip.hidden = true;
  54. }
  55. pop(ev) {
  56. this._tip.style.top = ev.pageY + 'px';
  57. //面板最大宽度为350px
  58. this._tip.style.left = (ev.pageX + 350 <= document.body.clientWidth ?
  59. ev.pageX : document.body.clientWidth - 350) + 'px';
  60. }
  61. }
  62. const tip = new TranslateTip();
  63.  
  64. class Icon {
  65. constructor() {
  66. const icon = document.createElement('span');
  67. icon.hidden = true;
  68. icon.innerHTML = `<svg style="margin:4px !important;" width="16" height="16" viewBox="0 0 24 24">
  69. <path d="M12 2L22 12L12 22L2 12Z" style="fill:none;stroke:#3e84f4;stroke-width:2;"></path></svg>`;
  70. icon.setAttribute('style',
  71. `width:24px!important;
  72. height:24px!important;
  73. background:#fff!important;
  74. border-radius:50%!important;
  75. box-shadow:4px 4px 8px #888!important;
  76. position:absolute!important;
  77. z-index:2147483647!important;`
  78. );
  79. document.documentElement.appendChild(icon);
  80. //拦截二个鼠标事件,以防止选中的文本消失
  81. icon.addEventListener('mousedown', e => e.preventDefault(), true);
  82. icon.addEventListener('mouseup', ev => ev.preventDefault(), true);
  83. icon.addEventListener('click', ev => {
  84. if (ev.ctrlKey) navigator.clipboard.readText()
  85. .then(text => {
  86. this.queryText(text.trim(),ev);
  87. })
  88. .catch(err => {
  89. console.error('Failed to read contents: ', err);
  90. });
  91. else {
  92. const text = window.getSelection().toString().trim().replace(/\s{2,}/g, ' ');
  93. this.queryText(text,ev);
  94. }
  95. });
  96. this._icon = icon;
  97. }
  98. pop(ev) {
  99. const icon = this._icon;
  100. icon.style.top = ev.pageY + 9 + 'px';
  101. icon.style.left = ev.pageX + -18 +'px';
  102. icon.hidden = !1;
  103. setTimeout(this.hide.bind(this), 2e3);
  104. }
  105. hide() {
  106. this._icon.hidden = true;
  107. }
  108. queryText(text,ev) {
  109. if (text) {
  110. this._icon.hidden = true;
  111. tip.pop(ev);
  112. nastool(text);
  113. }
  114. }
  115. }
  116. const icon = new Icon();
  117.  
  118. document.addEventListener('mouseup', function(e) {
  119. var text = window.getSelection().toString().trim();
  120. GM_log(text);
  121. if (!text) {
  122. icon.hide();
  123. tip.hide();
  124. }
  125. else icon.pop(e);
  126. });
  127.  
  128. function nastool(text) {
  129. tip.showText(`识别中`);
  130. let name = text.replace(/&/g,"");
  131. GM_log(text);
  132. GM_xmlhttpRequest({
  133. url: nastoolUrl + "/do?random=" + Math.random(),
  134. method: "POST",
  135. responseType: 'json',
  136. data : `cmd=name_test&data=%7B%22name%22%3A%22${name}%22%7D`,
  137. headers: {
  138. "user-agent": navigator.userAgent,
  139. "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
  140. },
  141. onload: (res) => {
  142. if (res.status === 200) {
  143. let html = '';
  144. html += res.response.data.type ? `类型:${res.response.data.type}<br>` : '';
  145. html += res.response.data.category ? `分类:${res.response.data.category}<br>` : '';
  146. html += res.response.data.name ? `名称:${res.response.data.name}<br>` : '';
  147. html += res.response.data.title ? `标题:${res.response.data.title}<br>` : '';
  148. html += res.response.data.season_episode ? `季集:${res.response.data.season_episode}<br>` : '';
  149. html += res.response.data.year ? `年份:${res.response.data.year}<br>` : '';
  150. html += res.response.data.tmdbid ? 'tmdb:<a href="'+res.response.data.tmdblink + '" target="_blank">' + res.response.data.tmdbid + '</a>': '';
  151. tip.showText(html);
  152. } else {
  153. tip.showText("连接失败");
  154. }
  155. }
  156. });
  157. }
  158. })();