User:Erutuon/scripts/katakanaOnyomi.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

Locates the on readings in a kanji entry and converts the displayed text from hiragana to katakana, though the link still points to an entry in hiragana.

To install, put this in your common.js:

importScript("User:Erutuon/scripts/katakanaOnyomi.js");

$(function () {
	// Based on the change_codepoint function in [[Module:ja]].
	function changeCodePoints(difference, minimum, maximum) {
		return function (str) {
			if (typeof str !== 'string')
				throw new TypeError("Expected String, got " + typeof(str));
			return str
				.split("")
				.map(function (char) {
					const codePoint = char.codePointAt(0);
					return (minimum <= codePoint && codePoint <= maximum)
						? String.fromCodePoint(codePoint + difference)
						: char;
				})
				.join("");
		};
	}
	
	// Based on the kata_to_hira and hira_to_kata functions in [[Module:ja]].
	// Codepoints between and including ぁ (U+3041) and ゖ (U+3096) can be
	// converted to katakana by adding 96.
	// const kataToHira = changeCodePoints(-96);
	const hiraToKata = changeCodePoints(96, "ぁ".codePointAt(0), "ゖ".codePointAt(0));
	
	document
		.querySelectorAll('.on-yomi .Hira a')
		.forEach(function (elem) { elem.innerText = hiraToKata(elem.innerText) });
});