From 582b044b161edbccd69f8a5028ffa8156f5b4450 Mon Sep 17 00:00:00 2001 From: woohoo Date: Tue, 7 Dec 2021 18:15:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B1=95=E6=9F=9C=E3=80=81?= =?UTF-8?q?=E6=9B=B4=E6=96=B0crime=E5=BF=AB=E9=80=9F=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- torn-trans-zhcn.user.js | 140 ++++++++++++++++++++++++++++++++-------- tpl.js | 25 ++----- 2 files changed, 121 insertions(+), 44 deletions(-) diff --git a/torn-trans-zhcn.user.js b/torn-trans-zhcn.user.js index 303cf25..225d0c7 100644 --- a/torn-trans-zhcn.user.js +++ b/torn-trans-zhcn.user.js @@ -46,6 +46,7 @@ 'Spouse\'s Properties': '配偶的房产', 'Items': '物品仓库', 'Travel Agency': '旅行社', + 'Display Cabinet': '展示柜', }; const titleLinksDict = { 'Personal stats': '个人统计信息', @@ -1181,6 +1182,12 @@ 'Real name': '真实姓名', 'Country': '国家', 'City': '城市', + 'Grocer': '杂货', + 'Education': '教育', + 'Casino': '赌场', + 'Medical': '医疗', + 'Army': '军队', + 'Law': '法律', 'There is no active competition': '现在没有活动', "doesn't wish to share": '不希望公开', }; @@ -1386,6 +1393,7 @@ 'Caliber:': '口径:', 'Ammo:': '弹药:', 'Bonus:': '额外:', + 'Masked:': '面具:', }; const itemNameDict = { "Ammunition Pack": '弹药包', @@ -3313,13 +3321,13 @@ * crime */ if (window.location.href.contains(/crimes\.php/)) { - const $$ = $('.content-wrapper'); + const $$ = document.querySelector('.content-wrapper'); const OB = new MutationObserver(() => { OB.disconnect(); titleTrans(); contentTitleLinksTrans(); trans(); - OB.observe($$.get(0), { + OB.observe($$, { characterData: true, attributes: true, subtree: true, @@ -3327,28 +3335,33 @@ }); }); const trans = () => { - if ($('.wh-translate').length === 0 && $('div#tab-menu.captcha').length === 0) { - $('div.content-title').before( - `
快捷操作:
+ const dom = `
快捷操作:
- +
- +
- -

`); + +

` + const is_wh_translate = $$.querySelector('.wh-translate') !== null; + const is_captcha = $$.querySelector('div#tab-menu.captcha') !== null; + const $title = $('div.content-title'); + const $info = $('.info-msg-cont'); + if (!is_wh_translate && !is_captcha) { + if ($title.length > 0) $title.before(dom); + else if ($info.length > 0) $info.before(dom); } }; trans(); - OB.observe($$.get(0), { + OB.observe($$, { characterData: true, attributes: true, subtree: true, @@ -3369,12 +3382,12 @@ }; const translated = {cat: '', count: -1}; const translatedOnce = {item_opt: -1, opt_icon_count: -1}; - initOB($(`div#category-wrap`).get(0), options, () => { + initOB(document.getElementById('category-wrap'), options, () => { // 手机操作选项 - const $item_opt = $(`ul.itemsList span.opt-name`); + const $item_opt = document.querySelectorAll(`ul.itemsList span.opt-name`); if (translatedOnce.item_opt !== $item_opt.length - 1) { let count = -1; - $item_opt.each((i, e) => { + $item_opt.forEach((e, i) => { if (itemPageDict[e.firstChild.nodeValue.trim()]) { e.firstChild.nodeValue = itemPageDict[e.firstChild.nodeValue.trim()]; } @@ -3384,8 +3397,8 @@ } // 物品名 const expanded_dom = `ul.itemsList[aria-expanded="true"]`; - const $active_item_list = $(`${expanded_dom} span.name`); const $active_tab = $(`${expanded_dom}`); + const $active_item_list = $active_tab.find(`span.name`); const itemCat = $active_tab.attr('id'); if ($active_item_list.length - 1 !== translated.count || itemCat !== translated.cat) { let count = -1; @@ -3406,10 +3419,10 @@ } } // 物品详情 - const $item_info = $(`${expanded_dom} li.show-item-info span.info-msg`); + const $item_info = $active_tab.find(`li.show-item-info span.info-msg`); $item_info.each((i, e) => { // torntools 插件 - const isTTModified = $(e).hasClass('tt-modified'); + const isTTModified = e.classList.contains('tt-modified'); if (isTTModified) { } const itemName = e.children[0].children[0]; //大麻1 @@ -3752,12 +3765,13 @@ * profile 玩家资料页面 */ if (window.location.href.contains(/profiles\.php\?XID=[0-9]+/)) { + const $wrapper = document.querySelector('.content-wrapper'); const profileOB = new MutationObserver(() => { profileOB.disconnect(); titleTrans(); contentTitleLinksTrans(); profileTrans(); - profileOB.observe($('.content-wrapper').get(0), { + profileOB.observe($wrapper, { characterData: true, attributes: true, subtree: true, @@ -3769,7 +3783,7 @@ ? document.title.trim().split(/('s |s' )/)[0] : null; if (!playerName) { - console.error('错误:获取用户名失败。'); + console.error('翻译助手错误:获取用户名失败。'); try { profileOB.disconnect() } catch { @@ -3961,7 +3975,7 @@ sendCashTrans('.content-wrapper'); }; profileTrans(); - profileOB.observe($('.content-wrapper').get(0), { + profileOB.observe($wrapper, { characterData: true, attributes: true, subtree: true, @@ -4544,6 +4558,74 @@ return; } + /* + 展柜 + */ + if (window.location.href.contains(/displaycase\.php/)) { + const $page_wrapper = document.querySelector('#display-page-wrap'); + initOB($page_wrapper, { + subtree: true, + attributes: true, + childList: true + }, + () => { + // 标题和右边的链接 + titleTrans(); + // 右上角返回按钮 + const $back_to_profile = $page_wrapper.querySelector('#back'); + if ($back_to_profile) { + const spl = $back_to_profile.innerText.split(/('s |s' )/); + if (spl.length === 3 && spl[2] === 'Profile') { + $back_to_profile.innerText = `${spl[0]}的个人资料`; + } + } + const $display_cabinet = $page_wrapper.querySelector('.display-cabinet'); + if ($display_cabinet) { + // 物品名 + const $item_name = $display_cabinet.querySelectorAll('div.b-item-name span:nth-of-type(2)'); + $item_name.forEach((e) => { + if (itemNameDict[e.innerText]) { + e.innerText = itemNameDict[e.innerText]; + } + }); + // 展开详细框 + const $show_item_info = $display_cabinet.querySelector('.show-item-info'); + if ($show_item_info) { + // tt插件 + const is_tt_modified = !!$show_item_info.querySelector('.tt-modified'); + if (is_tt_modified) { + console.warn(is_tt_modified) + } + // 物品名 + const $item_name = $show_item_info.querySelector('span.bold'); + // 去除物品名的the + const the_removed = $item_name.innerText.trim().slice(4); + // 物品的类别 + const $item_type = $item_name.nextSibling; + // 绿字 物品效果 + const $item_effect = $show_item_info.querySelector('div.item-effect'); + // 下方的表格 + const $info_table_title = $show_item_info.querySelectorAll('div.title'); + if (itemNameDict[the_removed]) { + $item_name.innerText = `${itemNameDict[the_removed]}(${the_removed})`; + } + if (itemTypeDict[$item_type.nodeValue.trim()]) { + $item_type.nodeValue = itemTypeDict[$item_type.nodeValue.trim()]; + } + if ($item_effect && itemEffectDict[$item_effect.innerText.trim()]) { + $item_effect.innerText = itemEffectDict[$item_effect.innerText.trim()]; + } + $info_table_title.forEach((e) => { + if (itemPageDict[e.innerText.trim()]) { + e.innerText = itemPageDict[e.innerText.trim()]; + } + }); + } + } + }); + return; + } + /** * 升级页面 */ @@ -5581,9 +5663,10 @@ * 页标题右侧按钮 */ function contentTitleLinksTrans() { - const $links = $('div.content-title span:nth-child(2)').length === 0 + const $links_default = $('div.content-title span:nth-child(2)'); + const $links = $links_default.length === 0 ? $('div[class^="topSection"] span[class*="Title"]') - : $('div.content-title span:nth-child(2)'); + : $links_default; $links.each((i, e) => { if (titleLinksDict[$(e).text()]) { $(e).text(titleLinksDict[$(e).text()]); @@ -5699,24 +5782,29 @@ ${htmlCont}
ob */ function initOB(dom = document, opt = {}, func = () => { - }, record = false) { - if (record) { + }, dev = false, once = false) { + //let count = -1; + if (dev) { const mo = new MutationObserver((mutation) => { + //count++; console.log(mutation) mo.disconnect(); func(); - mo.observe(dom, opt) + if (!once) { + mo.observe(dom, opt) + } }); func(); mo.observe(dom, opt); } else { + //count++; const mo = new MutationObserver(() => { mo.disconnect(); func(); - mo.observe(dom, opt) + if (!once) mo.observe(dom, opt) }); func(); - mo.observe(dom, opt); + mo.observe(dom, opt) } } }()); diff --git a/tpl.js b/tpl.js index f72c937..5c613a8 100644 --- a/tpl.js +++ b/tpl.js @@ -1,23 +1,12 @@ function template() { if (window.location.href.contains(/[]/)) { - const $$ = $('.content-wrapper'); - const opt = { - characterData: true, - attributes: true, - subtree: true, - childList: true - }; - const OB = new MutationObserver(() => { - OB.disconnect(); - titleTrans(); - contentTitleLinksTrans(); - trans(); - OB.observe($$.get(0), opt); - }); - const trans = () => { - }; - trans(); - OB.observe($$.get(0), opt); + // 标题和右边的链接 + const $cont_title = document.querySelector('.content-title'); + initOB($cont_title, {childList: true, subtree: true}, + () => { + titleTrans(); + contentTitleLinksTrans(); + }); return; } }