库X崔羊羊 2021-12-15 18:32 采纳率: 100%
浏览 29
已结题

求改JS脚本,将网页中的英文替换为中文,支持包含加粗、斜体等格式

大家好,我在使用油猴脚本制作一个网页游戏“卡片战斗先导者OD”的汉化
https://dulst.com/overdulst/cards/31853506/Vairina-Valiente
如上↑,我要汉化这个网站的UI、卡名、效果等信息,现在在大段的效果文本上遇到了一个问题,举个例子↓

img

请看,这是一整句效果,但是却被加粗的给隔开了,如果分多段汉化,根本没法阅读
【下面是我抄来的代码,求帮忙改改,使其能将被加粗、斜体等格式分开的当做一整句话,谢谢!Q3Q
(中间的翻译太长了,所以节选了一段)
(代码是我抄来的,我只学过用DW画网页,完全看不懂JS,推到重写也行啦,哭唧唧~)
(如果需要把加粗和斜体的<strong <i 啥的放到【const i18n = new Map([】也可以哒!)

// ==UserScript==
// @name         dulst.com的CardFighit Vanguard OverDulst文本替换汉化
// @namespace    http://tampermonkey.net/
// @version      0.6
// @description  通过将英文替换为中文的方法,汉化卡片战斗先导者OD的网页游戏
// @author       库X崔羊羊
// @match        https://dulst.com/overdulst/*
// @icon         https://www.google.com/s2/favicons?domain=dulst.com
// ==/UserScript==
 
(function() {
    'use strict';
 
    const i18n = new Map([
        ['when this unit would be retired for your card\'s cost', '由于你的卡片的费用要将这个单位退场之际'],
        ['you may retire it as two rear-guards', '这个单位可以当做2张的份量'],
        ['Put this unit into your damage zone', '将这张卡放置到伤害区'],
        ['choose a face up grade 3 card from your damage zone', '选择你的伤害区中的1张正面表示的等级3的卡'],
        ['increase of the trigger effects revealed by your drive check gets', '因你的攻击判定判出的触发效果增加时再'],
        ['Blaster Dark', '暗黑狂风'],
        ['Blaster', '狂风'],
        ['At the end of that turn', '这个回合结束时'],
        ['retire that called unit', '将被CALL出场的那个单位退场'],
        ['Put it on the top of the deck if you did not call', '没有CALL出场的话放置回牌堆顶'],
        ['When this unit boosts a vanguard', '这个单位支援先导者时'],
        ['Triple Drive', '三判'],
        ['Drive check three times', '能够进行3次攻击判定'],
        ['choose three of your front row units', '选择前列的你的3个单位'],
        ['The \\[Power\\] gained from this unit\'s boost will be lost', '通过这个单位支援给予的力量增加会不再有效'],
        ['When this unit boosts a rear-guard', '这个单位支援后防者时'],
        ['if you persona rode this turn', '这个回合中你进行了人格RIDE的话'],
        ['and your opponent cannot call grade 1 or greater cards from their hand to \\(GC\\) until end of that battle', '这次战斗中,对手不能将等级1以上的卡从手牌CALL到G上'],
        ['if you have five or more cards in your soul', '你的灵魂里的卡有5张以上的话'],
        ['This card cannot be ridden', '这张卡不能RIDE'],
        ['called or moved to the front row', 'CALL或移动将这张卡放置到前列'],
        ['When you would move or swap rear-guards during your main phase', '你在主要阶段中将后防者移动或交换位置之际'],
        ['you can also move it within the same row', '也可以在横列间移动或交换位置'],
        ['Your units in front of this unit cannot be chosen by your opponent\'s effects', '这个单位前方的你的单位不会被对手的效果选择'],
        ['Note:', '注意:'],
        ['Currently lets units move anywhere on the board instead of allowing movement by row.', '目前允许单位在圆阵上任意移动,而不是纵横移动'],
        ['the next time you would', '你下一次通过'],
        ['an order and play it this turn', '将指令卡使用之际'],
        ['reduce that cost by', '那个费用减少'],
        ['Reduce from the total cost', '在合计的费用中减少'],
        ['Does not Counter Charge even if it becomes negative', '但是即使减到负数也不能计数回充'],
        ['bind this unit', '将这个单位封锁'],
        ['choose an order card from your bind zone', '选择你的封锁区中的1张指令卡'],
        ['you may put all cards from your bind zone into the drop', '你可以将你的封锁区中所有的卡放置到弃牌区'],
        ['until end of turn for each card put', '←放置的卡每有1张,直到回合结束'],
        ['you may discard the top five cards of your deck', '你可以将你的牌堆顶的5张卡舍弃'],
        ['until end of that battle for each of your rear-guards', '←你的后防者每有1张,在这次战斗中'],
        ['Beta Promo', '内测奖励'],
        ['Granted to the top 2 players of the first overDulst tournament!!', '授予第1届overDulst锦标赛的前两名选手!!'],
        ['if you have another unit with', '如果你有其他的单位含有'],
        ['Toushirou', '藤四郎'],
        ['When this unit boosts a', '当这个单位支援'],
        ['you may have this unit get', '你可以使这个单位获得'],
        ['at the end of that battle', '这次战斗结束时'],
        ['retire all of your rear-guards in the same column as this unit', '将与这个单位同纵列的你所有的后防者退场'],
        ['Discard a set order card from your hand', '将手牌中的1张设置指令卡舍弃'],
        ['retire a Shadow Army token', '将1张夜影兵衍生物退场'],
        ['If one or less of your opponent\'s cards are imprisoned in your Prison', '被收容在你的监狱的对手的卡在1张以下的话'],
        ['choose a unit card from your opponent\'s drop', '选择对手的弃牌区中的1张单位卡'],
        ['During the battle this unit boosted a vanguard', '这个单位支援先导者的战斗中'],
        ['During the battle this unit boosted a grade 0', '这个单位支援等级0的单位的战斗中'],
        ['During the battle this unit boosted', '这个单位支援的战斗中'],
 
    ])
 
    replaceText(document.body)
 
    const bodyObserver = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(addedNode => replaceText(addedNode))
        })
    })
    bodyObserver.observe(document.body, { childList: true, subtree: true })
 
    function replaceText(node) {
        nodeForEach(node).forEach(htmlnode => {
            i18n.forEach((value, index) => {
                const textReg = new RegExp(index, 'g')
                if (htmlnode instanceof Text ) {
                    htmlnode.nodeValue = htmlnode.nodeValue.replace(textReg, value)
                }
                else if (htmlnode instanceof HTMLInputElement ) {
                    htmlnode.value = htmlnode.value.replace(textReg, value)
                }
            })
        })
    }
 
    function nodeForEach(node) {
        const list = []
        if (node.childNodes.length === 0) list.push(node)
        else {
            node.childNodes.forEach(child => {
                if (child.childNodes.length === 0) list.push(child)
                else list.push(...nodeForEach(child))
            })
        }
        return list
    }
})();

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月23日
    • 修改了问题 12月15日
    • 修改了问题 12月15日
    • 创建了问题 12月15日

    悬赏问题

    • ¥30 求解达问题(有红包)
    • ¥15 请解包一个pak文件
    • ¥15 不同系统编译兼容问题
    • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
    • ¥30 数字电源对DSP芯片的具体要求
    • ¥20 antv g6 折线边如何变为钝角
    • ¥30 如何在Matlab或Python中 设置饼图的高度
    • ¥15 nginx中的CORS策略应该如何配置
    • ¥30 信号与系统实验:采样定理分析
    • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao