From d74aa9b4bf8afb4bd4a9bf5f4665747f6d495859 Mon Sep 17 00:00:00 2001 From: CxJuice <110189934+CxJuice@users.noreply.github.com> Date: Fri, 29 Jul 2022 12:17:04 +0800 Subject: [PATCH] Create userscript.js --- userscript.js | 182 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 userscript.js diff --git a/userscript.js b/userscript.js new file mode 100644 index 0000000..43f771a --- /dev/null +++ b/userscript.js @@ -0,0 +1,182 @@ + // ==UserScript== +// @name UEX汉化脚本 +// @namespace Violentmonkey Scripts +// @match https://uexcorp.space/* +// @grant none +// @version 0.2 +// @author CxJuice +// @description 2022/7/28 12:48:01 + +// ==/UserScript== +(function() { + 'use strict'; + + const SUPPORT_LANG = ["zh-CN", "ja"]; + const lang = (navigator.language || navigator.userLanguage); + const locales = getLocales(lang) + + translateByCssSelector(); + translateDesc(); + traverseElement(document.body); + watchUpdate(); + + function getLocales(lang) { + if(lang.startsWith("zh")) { // zh zh-TW --> zh-CN + lang = "zh-CN"; + } + if(SUPPORT_LANG.includes(lang)) { + return JSON.parse(GM_getResourceText(lang)); + } + return { + css: [], + dict: {} + }; + } + + function translateRelativeTimeEl(el) { + const datetime = $(el).attr('datetime'); + $(el).text(timeago.format(datetime, lang.replace('-', '_'))); + } + + function translateElement(el) { + // Get the text field name + let k; + if(el.tagName === "INPUT") { + if (el.type === 'button' || el.type === 'submit') { + k = 'value'; + } else { + k = 'placeholder'; + } + } else { + k = 'data'; + } + + const txtSrc = el[k].trim(); + const key = txtSrc.toLowerCase() + .replace(/\xa0/g, ' ') // replace ' ' + .replace(/\s{2,}/g, ' '); + + if(locales.dict[key]) { + el[k] = el[k].replace(txtSrc, locales.dict[key]) + } + } + + function shoudTranslateEl(el) { + const blockIds = ["readme", "wiki-content"]; + const blockClass = [ + "CodeMirror", + "css-truncate" // 过滤文件目录 + ]; + const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg", "TABLE", "ARTICLE", "PRE"]; + + if(blockTags.includes(el.tagName)) { + return false; + } + + if(el.id && blockIds.includes(el.id)) { + return false; + } + + if(el.classList) { + for(let clazz of blockClass) { + if(el.classList.contains(clazz)) { + return false; + } + } + } + + return true; + } + + function traverseElement(el) { + if(!shoudTranslateEl(el)) { + return + } + + for(const child of el.childNodes) { + if(["RELATIVE-TIME", "TIME-AGO"].includes(el.tagName)) { + translateRelativeTimeEl(el); + return; + } + + if(child.nodeType === Node.TEXT_NODE) { + translateElement(child); + } + else if(child.nodeType === Node.ELEMENT_NODE) { + if(child.tagName === "INPUT") { + translateElement(child); + } else { + traverseElement(child); + } + } else { + // pass + } + } + } + + function watchUpdate() { + const m = window.MutationObserver || window.WebKitMutationObserver; + const observer = new m(function (mutations, observer) { + for(let mutationRecord of mutations) { + for(let node of mutationRecord.addedNodes) { + traverseElement(node); + } + } + }); + + observer.observe(document.body, { + subtree: true, + characterData: true, + childList: true, + }); + } + + // translate "about" + function translateDesc() { + $(".repository-content .f4").append("
"); + $(".repository-content .f4").append("翻译"); + $("#translate-me").click(function() { + // get description text + const desc = $(".repository-content .f4") + .clone() + .children() + .remove() + .end() + .text() + .trim(); + + if(!desc) { + return; + } + + GM_xmlhttpRequest({ + onload: function(res) { + if (res.status === 200) { + $("#translate-me").hide(); + // render result + const text = res.responseText; + $(".repository-content .f4").append("TK翻译"); + $(".repository-content .f4").append("
"); + $(".repository-content .f4").append(text); + } else { + alert("翻译失败"); + } + } + }); + }); + } + + function translateByCssSelector() { + if(locales.css) { + for(var css of locales.css) { + if($(css.selector).length > 0) { + if(css.key === '!html') { + $(css.selector).html(css.replacement); + } else { + $(css.selector).attr(css.key, css.replacement); + } + } + } + } + } +})();