From 50494a7124978c708cb0052bbc0c89bde7849c76 Mon Sep 17 00:00:00 2001 From: CxJuice <110189934+CxJuice@users.noreply.github.com> Date: Sat, 28 Jan 2023 01:22:18 +0800 Subject: [PATCH] Update userscript.js --- userscript.js | 71 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/userscript.js b/userscript.js index 43f771a..0038c18 100644 --- a/userscript.js +++ b/userscript.js @@ -1,25 +1,43 @@ - // ==UserScript== +// ==UserScript== // @name UEX汉化脚本 // @namespace Violentmonkey Scripts // @match https://uexcorp.space/* // @grant none -// @version 0.2 +// @version 0.8.0 +// @license GNU GPLv3 // @author CxJuice // @description 2022/7/28 12:48:01 - +// @match https://uexcorp.space/* +// @match https://www.erkul.games/* +// @grant GM_xmlhttpRequest +// @grant GM_getResourceText +// @resource zh-CN https://cdn.jsdelivr.net/gh/CxJuice/Uex_Chinese_Translate@main/zh-CN-uex1.7.json +// @require https://cdn.bootcdn.net/ajax/libs/timeago.js/4.0.2/timeago.full.min.js +// @require https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js // ==/UserScript== +window.addEventListener('load', function(event) { + // 当页面加载完成时,执行我们的脚本 + myScript(); +}); + +// 监听页面中的鼠标点击事件 +document.addEventListener('click', function(event) { + // 当发生鼠标点击事件时,执行我们的脚本 + myScript(); +}); +function myScript() { (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"; @@ -32,12 +50,12 @@ 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; @@ -50,33 +68,34 @@ } 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 = [ + "mat-icon", "CodeMirror", "css-truncate" // 过滤文件目录 ]; - const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg", "TABLE", "ARTICLE", "PRE"]; - + const blockTags = []; + 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)) { @@ -84,21 +103,21 @@ } } } - + 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); } @@ -113,7 +132,7 @@ } } } - + function watchUpdate() { const m = window.MutationObserver || window.WebKitMutationObserver; const observer = new m(function (mutations, observer) { @@ -123,14 +142,14 @@ } } }); - + observer.observe(document.body, { subtree: true, characterData: true, childList: true, }); } - + // translate "about" function translateDesc() { $(".repository-content .f4").append("
"); @@ -144,11 +163,11 @@ .end() .text() .trim(); - + if(!desc) { return; } - + GM_xmlhttpRequest({ onload: function(res) { if (res.status === 200) { @@ -165,7 +184,7 @@ }); }); } - + function translateByCssSelector() { if(locales.css) { for(var css of locales.css) { @@ -179,4 +198,6 @@ } } } + })(); +}