Misskey Translation Script

A Translation Script for Misskey Note | 一个用于misskey贴文的翻译脚本

当前为 2023-04-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Misskey Translation Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.5
  5. // @description A Translation Script for Misskey Note | 一个用于misskey贴文的翻译脚本
  6. // @author dogcraft
  7. // @match https://m.dogcraft.cn/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. var ApiUrl = 'https://test1-api.dogcraft.top/ts/';
  12.  
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17.  
  18. console.log('Misskey Translate Script v2.0');
  19. ApiUrl = 'https://test1-api.dogcraft.top/ts/';
  20.  
  21. var cat = localStorage.getItem('lang');
  22. var vdog = localStorage.getItem('v');
  23. if (cat == null) {
  24. var lang_dog = navigator.language || navigator.userLanguage;
  25. } else {
  26. lang_dog = cat;
  27. }
  28. //获取浏览器的语言
  29. lang_dog = lang_dog.substr(0, 2);
  30.  
  31. function dog_add_fy(eldog) {
  32. //添加翻译按钮、区域以及绑定点击事件
  33. if (eldog.className == 'article') {
  34. dog_add_fy_old(eldog);
  35. return;
  36. }
  37. if (eldog.fanyi == 1) {
  38. // console.log('已经添加过了,重复添加。')
  39. } else {
  40. const dogbt = document.createElement("button");
  41. const btclass = Array.from(eldog.getElementsByTagName("footer")).slice(-1)[0].childNodes[0].getAttribute("class")
  42. dogbt.setAttribute("class", btclass);
  43. const nicon = document.createElement("i");
  44. nicon.setAttribute("class", "ti ti-language");
  45. dogbt.appendChild(nicon);
  46. Array.from(eldog.getElementsByTagName("footer")).slice(-1)[0].appendChild(dogbt);
  47. dogbt.addEventListener('click', dog_fy);//绑定翻译函数
  48. const ctp = eldog.querySelector('div[style="container-type: inline-size;"]')
  49. const ctix = ctp.getElementsByTagName('div')[0];
  50. // console.log(ctix);
  51. ctix.setAttribute("class", "fanyi-cont");
  52. const fyc = document.createElement("div");
  53. fyc.setAttribute("class", "fanyi");
  54. fyc.ct = 0;
  55. ctp.appendChild(fyc);
  56. eldog.fanyi = 1;
  57. }
  58. }
  59.  
  60. function dog_add_fy_old(eldog) {
  61. if (eldog.fanyi == 1) {
  62. // console.log('已经添加过了,重复添加。')
  63. return;
  64. }
  65. const dogbt = document.createElement("button");
  66. const btclass = Array.from(eldog.getElementsByTagName("footer")).slice(-1)[0].getElementsByClassName("button")[0].getAttribute("class")
  67. dogbt.setAttribute("class", btclass);
  68. const nicon = document.createElement("i");
  69. nicon.setAttribute("class", "ti ti-language");
  70. dogbt.appendChild(nicon);
  71. Array.from(eldog.getElementsByTagName("footer")).slice(-1)[0].appendChild(dogbt);
  72. dogbt.addEventListener('click', dog_fy);//绑定翻译函数
  73. const ctdog = eldog.getElementsByClassName('content')[0];
  74. const ydog = ctdog.getElementsByClassName('text')[0];
  75. ydog.setAttribute("class", "text fanyi-cont");
  76. const fyc = document.createElement("div");
  77. fyc.setAttribute("class", "fanyi");
  78. fyc.ct = 0;
  79. ctdog.appendChild(fyc);
  80. eldog.fanyi = 1;
  81. }
  82.  
  83.  
  84.  
  85. async function dog_fy() {
  86. //从后端获得翻译文本并写入到html中
  87. // console.log('翻译中');
  88. const pdog = this.parentElement.parentElement;
  89.  
  90. const ldog = pdog.getElementsByClassName('fanyi');
  91.  
  92. if (ldog) {
  93. const dog_fy_el = ldog[0];
  94. if (dog_fy_el.ct == 0) {
  95. console.log('还没有翻译');
  96. const hdog = pdog.getElementsByClassName('fanyi-cont')[0].innerText;
  97. post_dog = { 'c': hdog, 't': lang_dog };
  98. dog_fy_el.innerText = '正在翻译中……';
  99. uiy = await fetch(ApiUrl, {
  100. method: 'POST',
  101. body: JSON.stringify(post_dog),
  102. headers: new Headers({
  103. 'Content-Type': 'application/json'
  104. })
  105. });
  106. if (uiy.status == 200) {
  107. rt = await uiy.json();
  108. res_dog = rt.r;
  109. } else {
  110. res_dog = '接口不对劲';
  111. }
  112. dog_fy_el.innerText = `\n${res_dog}`;
  113. dog_fy_el.ct = 1
  114. this.childNodes[0].setAttribute("class", "ti ti-language-off");
  115. this.style = "color: red;";
  116.  
  117. } else {
  118. // console.log('已经翻译过了');
  119. if (dog_fy_el.ct == 2) {
  120. dog_fy_el.style.display = "";
  121. dog_fy_el.ct = 1;
  122. this.childNodes[0].setAttribute("class", "ti ti-language-off");
  123. this.style = "color: red;";
  124. } else if (dog_fy_el.ct == 1) {
  125. dog_fy_el.style.display = "none";
  126. dog_fy_el.ct = 2;
  127. this.childNodes[0].setAttribute("class", "ti ti-language");
  128. this.style = "color: blue;";
  129. }
  130. }
  131. } else {
  132. console.log('有地方不对劲');
  133. }
  134. }
  135.  
  136.  
  137.  
  138. var config = { attributes: false, childList: true, subtree: true };
  139. sj = []
  140. // 当观察到突变时执行的回调函数
  141. var callback = function (mutationsList) {
  142. mutationsList.forEach(function (item, index) {
  143. if (item.type == 'childList') {
  144. for (let iy_dog = 0; iy_dog < item.addedNodes.length; iy_dog++) {
  145. const iadog = item.addedNodes[iy_dog];
  146. if (iadog.getElementsByTagName == undefined) {
  147. // console.log('不是元素');
  148. } else {
  149. // console.log('是元素');
  150. sld = iadog.getElementsByTagName('article');
  151. if (sld.length > 0) {
  152. for (let ct_dog = 0; ct_dog < sld.length; ct_dog++) {
  153. const sdldog = sld[ct_dog];
  154. if (sdldog.parentElement.tagName != "A") {
  155. dog_add_fy(sdldog);
  156. } else {
  157. // console.log('不是要找的元素');
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. });
  165. };
  166.  
  167.  
  168. function getar() {
  169. ar = document.getElementById('misskey_app');
  170. if (ar == null) {
  171. console.log("没找到,等一秒");
  172. setTimeout(getar, 1000);
  173. return null;
  174. }
  175. else {
  176. for (let si = 0; si < sl.length; si++) {
  177. const sl_dog = sl[si];
  178. if (sl_dog.parentElement.tagName != "A") {
  179. dog_add_fy(sl_dog);
  180. }
  181. }
  182. console.log("找到了");
  183. var observer = new MutationObserver(callback);
  184. observer.observe(ar, config);
  185. return "dog"
  186. }
  187. }
  188.  
  189.  
  190. window.onload = function () {
  191. console.log('页面加载完毕');
  192. sl = document.getElementsByTagName('article');
  193. getar();
  194. }
  195.  
  196. })();