|
| 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 | +})(); |
0 commit comments