Greasy Fork

Greasy Fork is available in English.

test

FF14 Garland 场景汉化脚本 更新至国服5.01

当前为 2019-11-26 提交的版本,查看 最新版本

// ==UserScript==
// @name test
// @namespace Violentmonkey Scripts
// @match *://*.garlandtools.org/*
// @grant none
// @description FF14 Garland 场景汉化脚本 更新至国服5.01
// @author CDS MG
// @version 0.0.13
// ==/UserScript==
'use strict'
const i18n = new Map([
["fire","高兴高兴高兴"],
["ice","哀伤哀伤哀伤"]
])

const alertbak = window.alert.bind(window)
window.alert = (message) => {
  if (i18n.has(message)) message = i18n.get(message)
  return alertbak(message)
}
const confirmbak = window.confirm.bind(window)
window.confirm = (message) => {
  if (i18n.has(message)) message = i18n.get(message)
  return confirmbak(message)
}
const promptbak = window.prompt.bind(window)
window.prompt = (message, _default) => {
  if (i18n.has(message)) message = i18n.get(message)
  return promptbak(message, _default)
}

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(textNode => {
	  i18n.forEach(function (value, key, map){
		var regexPattern=new RegExp(key,"ig")
		//if (textNode instanceof Text && regexPattern.test(textNode.nodeValue)){
    if (textNode instanceof Text && regexPattern.test(textNode.nodeValue)){
			textNode.nodeValue = textNode.nodeValue.replace(regexPattern,value)
    }
		else if (textNode instanceof HTMLInputElement) {
			if (textNode.type === 'button' && regexPattern.test(textNode.value))
				textNode.value = textNode.value.replace(regexPattern,value)
			else if (textNode.type === 'text' && regexPattern.test(textNode.placeholder))
				textNode.placeholder = textNode.placeholder.replace(regexPattern,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
}