Skip to content

Instantly share code, notes, and snippets.

@zengargoyle
Last active October 22, 2016 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zengargoyle/3fa3ffe6a2d19610fea3 to your computer and use it in GitHub Desktop.
Save zengargoyle/3fa3ffe6a2d19610fea3 to your computer and use it in GitHub Desktop.
Greasemonkey Kanji Bigifier

A basic Greasemonkey script to make kanji/kana bigger.

Bookmarklets.txt -- contains a crunched up version suitable for making a bookmarklet.

javascript:void%20function(){function%20e(e){try{var%20t,n;t=document.getElementsByTagName(%22head%22)[0],n=document.createElement(%22style%22),n.type=%22text/css%22,t.appendChild(n),n.innerHTML=e}catch(u){document.styleSheets.length||document.createStyleSheet(),document.styleSheets[0].cssText+=e}}var%20t=%22kanjibigifier%22,n=[%22.%22+t+%22%20{%22,%22font-size:%2024px;%22,%22}%22].join(%22%22),u=%22//text()[not(ancestor::script)%20and%20not(ancestor::style)]%22,a=/([\u2E80-\u2EFF\u2F00-\u2FDF\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF]+)/g;e(n);for(var%20d=document.evaluate(u,document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null),o=d.snapshotLength-1;o%3E=0;o--){var%20s=d.snapshotItem(o);if(a.test(s.nodeValue)){var%20c=document.createElement(%22span%22),l=s.nodeValue;s.parentNode.replaceChild(c,s),a.lastIndex=0;for(var%20r=null,i=0;r=a.exec(l);){c.appendChild(document.createTextNode(l.substring(i,r.index)));var%20m=document.createElement(%22span%22);m.setAttribute(%22class%22,t),m.appendChild(document.createTextNode(r[0])),c.appendChild(m),i=a.lastIndex}c.appendChild(document.createTextNode(l.substring(i))),c.normalize()}}}();
// ==UserScript==
// @name Kanji Bigify
// @namespace jklmnop.net
// @description Make Kanji/Kana Bigger!
// @include http://www.reddit.com/r/LearnJapanese/*
// @version 1
// @grant none
// ==/UserScript==
var kanjiClassName = 'kanjibigifier';
var kanjiCSS = [
"." + kanjiClassName + " {",
"font-size: 24px;",
"}"
].join("");
var textPath = '//text()[not(ancestor::script) and not(ancestor::style)]';
var kRegex = /([\u2E80-\u2EFF\u2F00-\u2FDF\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF]+)/g;
function addGlobalStyle(css) {
try {
var elmHead, elmStyle;
elmHead = document.getElementsByTagName('head')[0];
elmStyle = document.createElement('style');
elmStyle.type = 'text/css';
elmHead.appendChild(elmStyle);
elmStyle.innerHTML = css;
} catch (e) {
if (!document.styleSheets.length) {
document.createStyleSheet();
}
document.styleSheets[0].cssText += css;
}
}
addGlobalStyle(kanjiCSS);
var snapTextElements = document.evaluate(
textPath,
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null
);
for (var i = snapTextElements.snapshotLength - 1; i >= 0; i--) {
var elmText = snapTextElements.snapshotItem(i);
if (kRegex.test(elmText.nodeValue)) {
var elmSpan = document.createElement("span");
var sText = elmText.nodeValue;
elmText.parentNode.replaceChild(elmSpan, elmText);
kRegex.lastIndex = 0;
for (var match = null, lastLastIndex = 0;
(match = kRegex.exec(sText)); ) {
elmSpan.appendChild(document.createTextNode(
sText.substring(lastLastIndex, match.index)));
var elmKanjiSpan = document.createElement("span");
//elmLink.setAttribute("href", match[0]);
elmKanjiSpan.setAttribute("class", kanjiClassName);
elmKanjiSpan.appendChild(document.createTextNode(match[0]));
elmSpan.appendChild(elmKanjiSpan);
lastLastIndex = kRegex.lastIndex;
}
elmSpan.appendChild(document.createTextNode(
sText.substring(lastLastIndex)));
elmSpan.normalize();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment