更新 userscript.js

This commit is contained in:
LeonRay297 2023-02-12 17:12:03 +08:00 committed by GitHub
parent b1f3f27384
commit bebe3586e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,18 +1,17 @@
// ==UserScript== // ==UserScript==
// @name UEX——CN // @name UEX,CCUGame汉化脚本
// @name:zh-CN UEX汉化脚本 // @name:zh-CN UEX,CCUGame汉化脚本
// @namespace https://github.com/CxJuice/Uex_Chinese_Translate // @namespace https://github.com/CxJuice/Uex_Chinese_Translate
// @version 0.9.4 // @version 0.9.6
// @description UEX——CN // @description UEX_Chinese_Translate
// @description:zh UEX汉化插件 // @description:zh UEX_Chinese
// @description:zh-CN UEX汉化插件 // @description:zh-CN UEX汉化
// @author CxJuice // @author CxJuice
// @match https://uexcorp.space/*
// @match https://www.erkul.games/*
// @match https://ccugame.app/* // @match https://ccugame.app/*
// @match https://uexcorp.space/*
// @grant GM_xmlhttpRequest // @grant GM_xmlhttpRequest
// @grant GM_getResourceText // @grant GM_getResourceText
// @resource zh-CN https://cdn.jsdelivr.net/gh/CxJuice/Uex_Chinese_Translate@main/zh-CN-uex1.11.json // @resource zh-CN https://cdn.jsdelivr.net/gh/CxJuice/Uex_Chinese_Translate@main/zh-CN-uex1.13.json
// @require https://cdn.bootcdn.net/ajax/libs/timeago.js/4.0.2/timeago.full.min.js // @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 // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js
// ==/UserScript== // ==/UserScript==
@ -20,26 +19,26 @@ window.addEventListener('load', function(event) {
// 当页面加载完成时,执行我们的脚本 // 当页面加载完成时,执行我们的脚本
myScript(); myScript();
}); });
// 监听页面中的鼠标点击事件 // 监听页面中的鼠标点击事件
document.addEventListener('click', function(event) { document.addEventListener('click', function(event) {
// 当发生鼠标点击事件时,执行我们的脚本 // 当发生鼠标点击事件时,执行我们的脚本
myScript(); myScript();
}); });
function myScript() { function myScript() {
(function() { (function() {
'use strict'; 'use strict';
const SUPPORT_LANG = ["zh-CN", "ja"]; const SUPPORT_LANG = ["zh-CN", "ja"];
const lang = (navigator.language || navigator.userLanguage); const lang = (navigator.language || navigator.userLanguage);
const locales = getLocales(lang) const locales = getLocales(lang)
translateByCssSelector(); translateByCssSelector();
translateDesc(); translateDesc();
traverseElement(document.body); traverseElement(document.body);
watchUpdate(); watchUpdate();
function getLocales(lang) { function getLocales(lang) {
if(lang.startsWith("zh")) { // zh zh-TW --> zh-CN if(lang.startsWith("zh")) { // zh zh-TW --> zh-CN
lang = "zh-CN"; lang = "zh-CN";
@ -52,12 +51,12 @@ function myScript() {
dict: {} dict: {}
}; };
} }
function translateRelativeTimeEl(el) { function translateRelativeTimeEl(el) {
const datetime = $(el).attr('datetime'); const datetime = $(el).attr('datetime');
$(el).text(timeago.format(datetime, lang.replace('-', '_'))); $(el).text(timeago.format(datetime, lang.replace('-', '_')));
} }
function translateElement(el) { function translateElement(el) {
// Get the text field name // Get the text field name
let k; let k;
@ -70,32 +69,32 @@ function myScript() {
} else { } else {
k = 'data'; k = 'data';
} }
const txtSrc = el[k].trim(); const txtSrc = el[k].trim();
const key = txtSrc.toLowerCase() const key = txtSrc.toLowerCase()
.replace(/\xa0/g, ' ') // replace ' ' .replace(/\xa0/g, ' ') // replace ' '
.replace(/\s{2,}/g, ' '); .replace(/\s{2,}/g, ' ');
if(locales.dict[key]) { if(locales.dict[key]) {
el[k] = el[k].replace(txtSrc, locales.dict[key]) el[k] = el[k].replace(txtSrc, locales.dict[key])
} }
} }
function shoudTranslateEl(el) { function shoudTranslateEl(el) {
const blockIds = []; const blockIds = [];
const blockClass = [ const blockClass = [
"css-truncate" // 过滤文件目录 "css-truncate" // 过滤文件目录
]; ];
const blockTags = [ "IMG", "svg"]; const blockTags = [ "IMG", "svg"];
if(blockTags.includes(el.tagName)) { if(blockTags.includes(el.tagName)) {
return false; return false;
} }
if(el.id && blockIds.includes(el.id)) { if(el.id && blockIds.includes(el.id)) {
return false; return false;
} }
if(el.classList) { if(el.classList) {
for(let clazz of blockClass) { for(let clazz of blockClass) {
if(el.classList.contains(clazz)) { if(el.classList.contains(clazz)) {
@ -103,21 +102,21 @@ function myScript() {
} }
} }
} }
return true; return true;
} }
function traverseElement(el) { function traverseElement(el) {
if(!shoudTranslateEl(el)) { if(!shoudTranslateEl(el)) {
return return
} }
for(const child of el.childNodes) { for(const child of el.childNodes) {
if(["RELATIVE-TIME", "TIME-AGO"].includes(el.tagName)) { if(["RELATIVE-TIME", "TIME-AGO"].includes(el.tagName)) {
translateRelativeTimeEl(el); translateRelativeTimeEl(el);
return; return;
} }
if(child.nodeType === Node.TEXT_NODE) { if(child.nodeType === Node.TEXT_NODE) {
translateElement(child); translateElement(child);
} }
@ -132,7 +131,7 @@ function myScript() {
} }
} }
} }
function watchUpdate() { function watchUpdate() {
const m = window.MutationObserver || window.WebKitMutationObserver; const m = window.MutationObserver || window.WebKitMutationObserver;
const observer = new m(function (mutations, observer) { const observer = new m(function (mutations, observer) {
@ -142,14 +141,14 @@ function myScript() {
} }
} }
}); });
observer.observe(document.body, { observer.observe(document.body, {
subtree: true, subtree: true,
characterData: true, characterData: true,
childList: true, childList: true,
}); });
} }
// translate "about" // translate "about"
function translateDesc() { function translateDesc() {
$(".repository-content .f4").append("<br/>"); $(".repository-content .f4").append("<br/>");
@ -163,11 +162,11 @@ function myScript() {
.end() .end()
.text() .text()
.trim(); .trim();
if(!desc) { if(!desc) {
return; return;
} }
GM_xmlhttpRequest({ GM_xmlhttpRequest({
onload: function(res) { onload: function(res) {
if (res.status === 200) { if (res.status === 200) {
@ -184,7 +183,7 @@ function myScript() {
}); });
}); });
} }
function translateByCssSelector() { function translateByCssSelector() {
if(locales.css) { if(locales.css) {
for(var css of locales.css) { for(var css of locales.css) {
@ -199,4 +198,4 @@ function myScript() {
} }
} }
})(); })();
} }