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