Created
January 7, 2019 04:39
-
-
Save TSedlar/ee614e00f90ad1d5069cb0416cf59a65 to your computer and use it in GitHub Desktop.
Changes NHK furigana to romaji upon clicking them
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 Romaji Tooltip | |
// @version 1 | |
// @grant none | |
// @match https://www3.nhk.or.jp/* | |
// @require https://code.jquery.com/jquery-3.3.1.min.js | |
// @require https://gist.githubusercontent.com/TSedlar/7a8090760a05c5ff0b7f82edd88ab001/raw/639090330cf619bc7f2cd1497579608afe140a71/Japanese.js | |
// ==/UserScript== | |
const translated = {}; | |
const isJapanese = string => { | |
const hira = Object.keys(translationMap.hiragana); | |
const kata = Object.keys(translationMap.katakana); | |
return [...hira, ...kata].some(x => string.indexOf(x) >= 0); | |
}; | |
const collectElements = () => { | |
$(".article-main__title") | |
.contents() | |
.filter((i, e) => e.nodeType == 3) | |
.wrap('<span class="furi-romi-title-wrap"></span>'); | |
$(".article-main__body p") | |
.contents() | |
.filter((i, e) => e.nodeType == 3) | |
.wrap('<span class="furi-romi-article-wrap"></span>'); | |
const furigana = $("rt"); | |
const titleFurigana = $(".furi-romi-title-wrap"); | |
const articleFurigana = $(".furi-romi-article-wrap"); | |
const articleLinks = $(".article-main__body p a span[class='under']"); | |
return [...furigana, ...titleFurigana, ...articleFurigana, ...articleLinks]; | |
}; | |
$(document).ready(() => { | |
collectElements() | |
.filter(e => { | |
const text = $(e) | |
.text() | |
.trim(); | |
return text.length > 0 && isJapanese(text); | |
}) | |
.forEach(e => { | |
translated[e] = false; | |
const original = $(e) | |
.text() | |
.trim(); | |
const translation = translateToRomaji(original); | |
e.onclick = evt => { | |
if (translated[e]) { | |
$(e).text(original); | |
} else { | |
$(e).text(translation); | |
} | |
translated[e] = !translated[e]; | |
}; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment