Spaces:
Configuration error
Configuration error
File size: 6,879 Bytes
af12fb4 |
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;
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 = 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;
}
this.replaceText(ele);
}
}
replaceText(target) {
let T = this.T;
if (!T) return;
if (this.tSkip(target)) return;
if (target?.childNodes.length > 1) {
for (const childNode of target.childNodes) {
this.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) {
this.replaceText(target.firstChild);
}
if (target?.firstChild?.nodeType === Node.TEXT_NODE) {
const targetLangText = T.Menu[target.firstChild.nodeValue];
if (!targetLangText) return;
target.innerText = targetLangText;
}
}
}
}
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);
}
});
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) {
// if (texe.translateKjPopDesc(node)) continue;
texe.translateAllText(node);
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);
}
}
}
}
const translateSettingDialog = () => {
const comfySettingDialogAllElements = comfySettingDialog.querySelectorAll("*");
for (const ele of comfySettingDialogAllElements) {
let targetLangText = T.Menu[ele.innerText];
let titleText = T.Menu[ele.title];
if(titleText) ele.title = titleText;
if (!targetLangText) {
if (ele.nodeName === "INPUT" && ele.type === "button") {
targetLangText = T.Menu[ele.value];
if (!targetLangText) continue;
ele.value = targetLangText;
}
continue;
}
texe.replaceText(ele);
}
};
function handleComfySettingDialogObserver(mutationsList) {
for (let mutation of mutationsList) {
if (mutation.type === "childList" && mutation.addedNodes.length > 0) {
translateSettingDialog();
}
}
}
}
export function observeFactory(observeTarget, fn) {
const observer = new MutationObserver(function (mutationsList, observer) {
fn(mutationsList, observer);
});
observer.observe(observeTarget, {
childList: true,
attributes: true,
});
return observer;
}
|