forked from AIGODLIKE/AIGODLIKE-ComfyUI-Translation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuTranslate.js
196 lines (178 loc) · 5.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
export function applyMenuTranslation(T) {
translateAllText(document.querySelector(".litegraph"));
for (let node of document.querySelectorAll(".comfy-modal"))
observeFactory(node, (mutationsList, observer) => {
for (let mutation of mutationsList) {
translateAllText(mutation.target);
}
});
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 = T.Menu[mutation.target.textContent];
if (!translatedValue) continue;
mutation.target.innerText = translatedValue;
}
}
observer.observe(btn, { childList: true, attributes: true });
});
});
const comfySettingDialog = document.querySelector("#comfy-settings-dialog");
observeFactory(
comfySettingDialog.querySelector("tbody"),
handleComfySettingDialogObserver
);
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) {
translateAllText(node);
if(node.classList.contains("comfy-modal")) {
observeFactory(node, (mutationsList, observer) => {
for (let mutation of mutationsList) {
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) {
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) {
replaceText(mutation.target);
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
for (const node of mutation.addedNodes) {
replaceText(node);
}
}
}
}
const translateSettingDialog = () => {
const comfySettingDialogAllElements = comfySettingDialog.querySelectorAll("*");
for (const ele of comfySettingDialogAllElements) {
let targetLangText = T.Menu[ele.innerText];
if (!targetLangText) {
if (ele.nodeName === "INPUT" && ele.type === "button") {
targetLangText = T.Menu[ele.value];
if (!targetLangText) continue;
ele.value = targetLangText;
}
continue;
}
replaceText(ele);
}
};
function handleComfySettingDialogObserver(mutationsList) {
for (let mutation of mutationsList) {
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
translateSettingDialog();
}
}
}
function translateAllText(node) {
const allElements = node.querySelectorAll("*");
for (const ele of allElements) {
let targetLangText = T.Menu[ele.innerText];
if (!targetLangText) {
if (ele.nodeName === "INPUT" && ele.type === "button") {
targetLangText = T.Menu[ele.value];
if (!targetLangText) continue;
ele.value = targetLangText;
}
continue;
}
replaceText(ele);
}
}
function replaceText(target) {
if (target?.childNodes.length > 1) {
for (const childNode of target.childNodes) {
replaceText(childNode);
const targetLangText =
childNode?.nodeType === Node.ELEMENT_NODE
? T.Menu[childNode.innerText]
: T.Menu[childNode.nodeValue];
if (!targetLangText) continue;
if (childNode?.nodeType === Node.TEXT_NODE) {
childNode.nodeValue = targetLangText;
}
if (childNode?.nodeType === Node.ELEMENT_NODE) {
childNode.innerText = targetLangText;
}
}
} else {
if (target?.firstChild) {
replaceText(target.firstChild);
}
if (target?.firstChild?.nodeType === Node.TEXT_NODE) {
const targetLangText = T.Menu[target.firstChild.nodeValue];
if (!targetLangText) return;
target.innerText = targetLangText;
}
}
}
}
export function observeFactory(observeTarget, fn) {
const observer = new MutationObserver(function (mutationsList, observer) {
fn(mutationsList, observer);
});
observer.observe(observeTarget, {
childList: true,
attributes: true,
});
return observer;
}