Skip to content

Commit d0fbffa

Browse files
WIP: Tagi10n: new plugin
Signed-off-by: Dee.H.Y <[email protected]>
1 parent f5ff6af commit d0fbffa

File tree

6 files changed

+38572
-0
lines changed

6 files changed

+38572
-0
lines changed

plugins/Tagi10n/Tagi10n.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Stash Tag Translation
2+
(async function () {
3+
"use strict";
4+
5+
const { waitForElement, PathElementListener, getConfiguration, baseURL } =
6+
window.csLib;
7+
8+
const defaultConfig = {
9+
defaultLanguage: "zh_CN",
10+
};
11+
12+
async function loadLanguageFile(lang) {
13+
try {
14+
const module = await import(
15+
`https://cdn.jsdelivr.net/gh/dongfengweixiao/CommunityScripts@tagi10n1/plugins/Tagi10n/i10n/tag_${lang}.js`
16+
);
17+
return module.default || {};
18+
} catch (error) {
19+
console.error(`Failed to load language file tag_${lang}.js`, error);
20+
return {};
21+
}
22+
}
23+
24+
function translateTags(languageMap, xpathArray) {
25+
if (!languageMap) {
26+
console.error("languageMap is undefined or null. Translation skipped.");
27+
return;
28+
}
29+
30+
xpathArray.forEach((xpath) => {
31+
const elements = document.evaluate(
32+
xpath,
33+
document,
34+
null,
35+
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
36+
null
37+
);
38+
for (let i = 0; i < elements.snapshotLength; i++) {
39+
const element = elements.snapshotItem(i);
40+
const originalText = element.textContent.trim();
41+
element.textContent = languageMap[originalText][0];
42+
}
43+
});
44+
}
45+
46+
function translateTagDescriptions(languageMap) {
47+
if (!languageMap) {
48+
console.error(
49+
"languageMap is undefined or null. Description translation skipped."
50+
);
51+
return;
52+
}
53+
54+
const tagNameElements = document.evaluate(
55+
"//div[contains(@class, 'tag-card')]//div[@class='TruncatedText']",
56+
document,
57+
null,
58+
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
59+
null
60+
);
61+
62+
for (let i = 0; i < tagNameElements.snapshotLength; i++) {
63+
const tagNameElement = tagNameElements.snapshotItem(i);
64+
const tagName = tagNameElement.textContent.trim();
65+
66+
const descriptionElement = document.evaluate(
67+
"//div[contains(@class, 'tag-card')]//div[contains(@class, 'tag-description')]",
68+
tagNameElement.parentNode,
69+
null,
70+
XPathResult.FIRST_ORDERED_NODE_TYPE,
71+
null
72+
).singleNodeValue;
73+
74+
const translationEntry = languageMap[tagName];
75+
76+
if (
77+
descriptionElement &&
78+
Array.isArray(translationEntry) &&
79+
translationEntry.length > 1
80+
) {
81+
descriptionElement.textContent = translationEntry[1];
82+
}
83+
}
84+
}
85+
86+
async function main() {
87+
const config = await getConfiguration("Tagi10n", defaultConfig);
88+
const lang = config.defaultLanguage;
89+
const languageMap = await loadLanguageFile(lang);
90+
91+
const xpathArray = [
92+
"//div[contains(@span, 'tag-item')]//div",
93+
"//*[contains(@class, 'tag-item')]//div",
94+
"//div[contains(@class, 'tag-card')]//div[@class='TruncatedText']",
95+
"//*[contains(@class, 'tag-name')]",
96+
"//span[contains(@span, 'tag-item')]//div",
97+
];
98+
99+
PathElementListener(baseURL + "scenes", ".tag-item", () => {
100+
translateTags(languageMap, xpathArray);
101+
});
102+
PathElementListener(baseURL + "tags", ".card-section", () => {
103+
translateTagDescriptions(languageMap);
104+
translateTags(languageMap, xpathArray);
105+
});
106+
PathElementListener(baseURL + "tags/", ".tag-name", () => {
107+
translateTagDescriptions(languageMap);
108+
translateTags(languageMap, xpathArray);
109+
});
110+
PathElementListener(baseURL + "images", ".tag-item", () => {
111+
translateTags(languageMap, xpathArray);
112+
});
113+
PathElementListener(baseURL + "performers", ".tag-item", () => {
114+
translateTags(languageMap, xpathArray);
115+
});
116+
PathElementListener(baseURL + "groups", ".tag-item", () => {
117+
translateTags(languageMap, xpathArray);
118+
});
119+
PathElementListener(baseURL + "galleries", ".tag-item", () => {
120+
translateTags(languageMap, xpathArray);
121+
});
122+
}
123+
124+
main();
125+
})();

plugins/Tagi10n/Tagi10n.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Tagi10n
2+
description: Display tag names in the given language.
3+
version: 0.0.1
4+
ui:
5+
requires:
6+
- CommunityScriptsUILibrary
7+
javascript:
8+
- https://cdn.jsdelivr.net/gh/dongfengweixiao/CommunityScripts@tagi10n1/plugins/Tagi10n/i10n/tag_zh_CN.js
9+
- Tagi10n.js
10+
json:
11+
12+
settings:
13+
defaultLanguage:
14+
displayName: Default Language
15+
description: Default Language
16+
type: STRING

0 commit comments

Comments
 (0)