Last active
February 2, 2024 06:05
-
-
Save kelvie/29e7941a4fdbe6d9e3edcc81df5fac28 to your computer and use it in GitHub Desktop.
Palworld Palname Translate パルーワルドのパル名和訳
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Palworld Palname Translate | |
// @name:ja パルーワルドのパル名和訳 | |
// @description Translate Palworld names to Japanese (auto runs if palworld is in the URL, otherwuse use the menu item) | |
// @description:ja 英語のパルーワルドサイトでパル名を和訳します(URLにpalworldが含まれてる場合は自動的に実行,そうでない場合はメニューから) | |
// @version 1.0.0 | |
// @license MIT | |
// @grant GM_registerMenuCommand | |
// @grant GM_getResourceText | |
// | |
// @resource palnames https://gist.githubusercontent.com/kelvie/714c8eae062a95aeea9eb3c1cf442f1a/raw/13fa2c6add8d06502bbc74c557ced3c4e322a252/palnames_v1.json | |
// | |
// @match *://*/* | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const palstable = JSON.parse(GM_getResourceText('palnames')); | |
// translation table | |
let tt = {}; | |
palstable.forEach(pal => { | |
tt[pal.en] = pal.ja; | |
}); | |
// split into names with spaces and names without spaces | |
let namesWithSpaces = []; | |
let namesWithoutSpaces = []; | |
Object.keys(tt).forEach(k => { | |
if (k.includes(' ')) { | |
namesWithSpaces.push(k); | |
} else { | |
namesWithoutSpaces.push(k); | |
} | |
}); | |
// We need to find the names with spaces first, so we don't replace the two | |
// word ones | |
let sanitize = (s) => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); | |
// case insensitive, whole word match | |
let mkRegex = (ks) => new RegExp('\\b(' + | |
ks.map(sanitize).join('|') | |
+ ')\\b', 'gi'); | |
let reWithSpaces = mkRegex(namesWithSpaces); | |
let reWithoutSpaces = mkRegex(namesWithoutSpaces); | |
let matchFunc = (matched) => tt[matched.toLowerCase()]; | |
let observer = null; | |
function translateNode(node) { | |
let walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false); | |
let n; | |
while (n = walker.nextNode()) { | |
n.nodeValue = n.nodeValue.replace(reWithSpaces, matchFunc); | |
n.nodeValue = n.nodeValue.replace(reWithoutSpaces, matchFunc); | |
} | |
} | |
function translate() { | |
translateNode(document.body); | |
// Add a mutation observer to catch any new text nodes | |
if (observer) { | |
observer.disconnect(); | |
observer = null; | |
} | |
observer = new MutationObserver((mutations) => { | |
mutations.map((mutation) => mutation.addedNodes) | |
.reduce((a, b) => a.concat(b), []) | |
.forEach((nodeList) => { | |
nodeList.forEach(translateNode); | |
}); | |
}); | |
observer.observe(document.body, { childList: true, subtree: true }); | |
} | |
// Run if /palworld|palpedia/i is in the URL (or any other good pal site) | |
if (window.location.href.match(/palworld|palpedia/i)) { | |
translate(); | |
} else { | |
GM_registerMenuCommand('パル名を和訳', translate); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment