Misskey Translation Script

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

当前为 2023-05-01 提交的版本,查看 最新版本

  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.  
  20. var cat = localStorage.getItem('lang');
  21. var vdog = localStorage.getItem('v');
  22. if (cat == null) {
  23. var lang_dog = navigator.language || navigator.userLanguage;
  24. } else {
  25. lang_dog = cat;
  26. }
  27. //获取浏览器的语言
  28. lang_dog = lang_dog.substr(0, 2);
  29.  
  30. function dog_add_fy(eldog) {
  31. //添加翻译按钮、区域以及绑定点击事件
  32. if (eldog.className == 'article') {
  33. dog_add_fy_old(eldog);
  34. return;
  35. }
  36. if (eldog.fanyi == 1) {
  37. // console.log('已经添加过了,重复添加。')
  38. } else {
  39. const dogbt = document.createElement("button");
  40. const btclass = Array.from(eldog.getElementsByTagName("footer")).slice(-1)[0].childNodes[0].getAttribute("class")
  41. dogbt.setAttribute("class", btclass);
  42. const nicon = document.createElement("i");
  43. nicon.setAttribute("class", "ti ti-language");
  44. dogbt.appendChild(nicon);
  45. Array.from(eldog.getElementsByTagName("footer")).slice(-1)[0].appendChild(dogbt);
  46. dogbt.addEventListener('click', dog_fy);//绑定翻译函数
  47. const ctp = eldog.querySelector('div[style="container-type: inline-size;"]')
  48. const ctix = ctp.getElementsByTagName('div')[0];
  49. // console.log(ctix);
  50. ctix.setAttribute("neko", "fanyi-cont");
  51. const fyc = document.createElement("div");
  52. fyc.setAttribute("class", "fanyi");
  53. fyc.ct = 0;
  54. ctp.appendChild(fyc);
  55. eldog.fanyi = 1;
  56. }
  57. }
  58.  
  59. function dog_add_fy_old(eldog) {
  60. if (eldog.fanyi == 1) {
  61. // console.log('已经添加过了,重复添加。')
  62. return;
  63. }
  64. const dogbt = document.createElement("button");
  65. const btclass = Array.from(eldog.getElementsByTagName("footer")).slice(-1)[0].getElementsByClassName("button")[0].getAttribute("class")
  66. dogbt.setAttribute("class", btclass);
  67. const nicon = document.createElement("i");
  68. nicon.setAttribute("class", "ti ti-language");
  69. dogbt.appendChild(nicon);
  70. Array.from(eldog.getElementsByTagName("footer")).slice(-1)[0].appendChild(dogbt);
  71. dogbt.addEventListener('click', dog_fy);//绑定翻译函数
  72. const ctdog = eldog.getElementsByClassName('content')[0];
  73. const ydog = ctdog.getElementsByClassName('text')[0];
  74. ydog.setAttribute("neko", "text fanyi-cont");
  75. const fyc = document.createElement("div");
  76. fyc.setAttribute("class", "fanyi");
  77. fyc.ct = 0;
  78. ctdog.appendChild(fyc);
  79. eldog.fanyi = 1;
  80. }
  81.  
  82.  
  83.  
  84. async function dog_fy() {
  85. //从后端获得翻译文本并写入到html中
  86. // console.log('翻译中');
  87. const pdog = this.parentElement.parentElement;
  88.  
  89. const ldog = pdog.getElementsByClassName('fanyi');
  90.  
  91. if (ldog) {
  92. const dog_fy_el = ldog[0];
  93. if (dog_fy_el.ct == 0) {
  94. console.log('还没有翻译');
  95. const hdog = pdog.querySelector('div[neko="fanyi-cont"]').innerText;
  96. post_dog = { 'c': hdog, 't': lang_dog };
  97. dog_fy_el.innerText = '正在翻译中……';
  98. uiy = await fetch(ApiUrl, {
  99. method: 'POST',
  100. body: JSON.stringify(post_dog),
  101. headers: new Headers({
  102. 'Content-Type': 'application/json'
  103. })
  104. });
  105. if (uiy.status == 200) {
  106. rt = await uiy.json();
  107. res_dog = rt.r;
  108. } else {
  109. res_dog = '接口不对劲';
  110. }
  111. dog_fy_el.innerText = `${res_dog}`;
  112. dog_fy_el.style.cssText = " margin-top: 1em; border: dashed 2px;border-radius: 10px;padding: 16px;";
  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. })();