forked from AIGODLIKE/AIGODLIKE-ComfyUI-Translation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuTranslate.js
executable file
·214 lines (194 loc) · 6.86 KB
/
MenuTranslate.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
class TExe {
static T = null;
MT(txt) {
return this.T?.Menu?.[txt] || this.T?.Menu?.[txt?.trim?.()];
}
constructor() {
this.excludeClass = ["lite-search-item-type"];
}
tSkip(node) {
// 是否需要跳过翻译?
// 判断node.classList 是否包含 excludeClass中的一个
return this.excludeClass.some((cls) => node.classList?.contains(cls));
}
translateKjPopDesc(node) {
let T = this.T;
if (!T) return false;
if (!node || !node.querySelectorAll) return false;
if (!node?.classList?.contains("kj-documentation-popup")) return false;
const allElements = node.querySelectorAll("*");
for (const ele of allElements) {
this.replaceText(ele);
}
return true;
}
translateAllText(node) {
let T = this.T;
if (!T) return;
if (!node || !node.querySelectorAll) return;
const allElements = node.querySelectorAll("*");
for (const ele of allElements) {
// let targetLangText = this.MT(ele.innerText) || this.MT(ele.value) || this.MT(ele.title);
// if (!targetLangText) {
// if (ele.nodeName === "INPUT" && ele.type === "button") {
// targetLangText = this.MT(ele.value);
// if (!targetLangText) continue;
// ele.value = targetLangText;
// }else if (ele.childNodes?.length > 1) {
// this.replaceText(ele);
// }
// continue;
// }
this.replaceText(ele);
}
}
replaceText(target) {
if (!target) return;
if (!this.T) return;
if (this.tSkip(target)) return;
for (const childNode of target.childNodes || [])
this.replaceText(childNode);
this.replaceText(target.firstChild);
// 翻译target
if (target.nodeType === Node.TEXT_NODE) {
if (target.nodeValue)
target.nodeValue = this.MT(target.nodeValue) || target.nodeValue;
}
else if (target.nodeType === Node.ELEMENT_NODE) {
if (target.title)
target.title = this.MT(target.title) || target.title;
if (target.nodeName === "INPUT" && target.type === "button") {
target.value = this.MT(target.value) || target.value;
}
if (target.innerText && this.MT(target.innerText))
{
target.innerText = this.MT(target.innerText);
}
if (target.textContent && this.MT(target.textContent))
{
target.textContent = this.MT(target.textContent);
}
}else if(target.nodeType === Node.COMMENT_NODE){
// pass
}
}
}
let texe = new TExe();
export function applyMenuTranslation(T) {
texe.T = T;
texe.translateAllText(document.querySelector(".litegraph"));
for (let node of document.querySelectorAll(".comfy-modal"))
observeFactory(node, (mutationsList, observer) => {
for (let mutation of mutationsList) {
texe.translateAllText(mutation.target);
}
});
// texe.translateAllText(window.comfyAPI?.app?.app?.menu?.element);
observeFactory(document.querySelector(".comfyui-menu"), handleComfyNewUIMenu, true);
for (let node of document.querySelectorAll(".comfyui-popup"))
observeFactory(node, handleComfyNewUIMenu, true);
const viewHistoryButton = document.getElementById("comfy-view-history-button");
const viewQueueButton = document.getElementById("comfy-view-queue-button");
[viewHistoryButton, viewQueueButton].map((btn) => {
observeFactory(btn, (mutationsList, observer) => {
observer.disconnect();
for (let mutation of mutationsList) {
if (mutation.type === "childList") {
const translatedValue = texe.MT(mutation.target.textContent);
if (!translatedValue) continue;
mutation.target.innerText = translatedValue;
}
}
observer.observe(btn, { childList: true, attributes: true });
});
});
observeFactory(document.querySelector(".comfy-menu"), handleViewQueueComfyListObserver);
observeFactory(document.querySelector(".comfy-menu").querySelectorAll(".comfy-list")[0], handleViewQueueComfyListObserver);
observeFactory(document.querySelector(".comfy-menu").querySelectorAll(".comfy-list")[1], handleViewQueueComfyListObserver);
observeFactory(document.querySelector("body.litegraph"), (mutationsList, observer) => {
for (let mutation of mutationsList) {
for (const node of mutation.addedNodes) {
// if (texe.translateKjPopDesc(node)) continue;
texe.translateAllText(node);
// 新版 settings面板的翻译
if (node.classList?.contains("p-dialog-mask"))
{
observeFactory(node.querySelector(".p-dialog"), (mutationsList, observer) => {
for (let mutation of mutationsList) {
texe.translateAllText(mutation.target);
}
});
continue;
}
if (node.classList?.contains("comfy-modal")) {
observeFactory(node, (mutationsList, observer) => {
for (let mutation of mutationsList) {
texe.translateAllText(mutation.target);
}
});
}
}
}
});
// search box
observeFactory(document.querySelector(".litegraph"), (mutationsList, observer) => {
if (observer.ob == undefined) {
observer.ob = [];
}
for (let mutation of mutationsList) {
if (mutation.removedNodes.length > 0 && observer.ob != undefined) {
for (let ob of observer.ob) ob.disconnect();
observer.ob = [];
break;
}
for (const sb of mutation.addedNodes) {
if (!sb || !sb.querySelector) continue
var helper = sb.querySelector(".helper");
if (!helper) continue;
var ob = observeFactory(helper, (mutationsList, observer) => {
for (let mutation of mutationsList) {
for (const item of mutation.addedNodes) {
if (item.innerText in T.Nodes) {
item.innerText = T.Nodes[item.innerText]["title"];
}
}
}
});
for (let item of helper.querySelectorAll(".lite-search-item")) {
if (item.innerText in T.Nodes) {
item.innerText = T.Nodes[item.innerText]["title"];
}
}
observer.ob.push(ob);
}
}
});
function handleViewQueueComfyListObserver(mutationsList) {
for (let mutation of mutationsList) {
texe.replaceText(mutation.target);
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
for (const node of mutation.addedNodes) {
texe.replaceText(node);
}
}
}
}
function handleComfyNewUIMenu(mutationsList)
{
for (let mutation of mutationsList) {
texe.translateAllText(mutation.target);
}
}
}
export function observeFactory(observeTarget, fn, subtree=false) {
if (!observeTarget) return;
const observer = new MutationObserver(function (mutationsList, observer) {
fn(mutationsList, observer);
});
observer.observe(observeTarget, {
childList: true,
attributes: true,
subtree: subtree,
});
return observer;
}