Advertisement
Guest User

Kanji Bigify

a guest
May 26th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Kanji Bigify
  3. // @namespace   jklmnop.net
  4. // @description Make Kanji/Kana Bigger!
  5. // @include     *://*.4channel.org/*
  6. // @include     *://*.4chan.org/*
  7. // @exclude     *://twitter.com/*
  8. // @exclude     *://www.dlsite.com/*
  9. // @exclude     *://kirarafantasia.com/*
  10. // @exclude     *://*.nyaa.si/*
  11. // @version     1
  12. // @grant       none
  13. // ==/UserScript==
  14.  
  15.  
  16. /*
  17.  * Only works on 4chan/nel for now. Change the include line to just a * to make it work for everything.
  18.  * Write sites with exclude to ignore them
  19.  */
  20.  
  21. var kanjiClassName = 'kanjibigifier';
  22.  
  23. var kanjiCSS = [
  24.   "." + kanjiClassName + " {",
  25.   "font-size: 20px;",
  26.   "}"
  27. ].join("");
  28.  
  29. var textPath = '//text()[not(ancestor::script) and not(ancestor::style)]';
  30.  
  31. var kRegex = /([\u2E80-\u2EFF\u2F00-\u2FDF\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF]+)/g;
  32.  
  33. function addGlobalStyle(css) {
  34.   try {
  35.     var elmHead, elmStyle;
  36.     elmHead = document.getElementsByTagName('head')[0];
  37.     elmStyle = document.createElement('style');
  38.     elmStyle.type = 'text/css';
  39.     elmHead.appendChild(elmStyle);
  40.     elmStyle.innerHTML = css;
  41.   } catch (e) {
  42.     if (!document.styleSheets.length) {
  43.       document.createStyleSheet();
  44.     }
  45.     document.styleSheets[0].cssText += css;
  46.   }
  47. }
  48.  
  49. addGlobalStyle(kanjiCSS);
  50.  
  51. var snapTextElements = document.evaluate(
  52.   textPath,
  53.   document,
  54.   null,
  55.   XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
  56.   null
  57. );
  58.  
  59. for (var i = snapTextElements.snapshotLength - 1; i >= 0; i--) {
  60.   var elmText = snapTextElements.snapshotItem(i);
  61.   if (kRegex.test(elmText.nodeValue)) {
  62.     var elmSpan = document.createElement("span");
  63.     var sText = elmText.nodeValue;
  64.     elmText.parentNode.replaceChild(elmSpan, elmText);
  65.     kRegex.lastIndex = 0;
  66.     for (var match = null, lastLastIndex = 0;
  67.        (match = kRegex.exec(sText)); ) {
  68.       elmSpan.appendChild(document.createTextNode(
  69.       sText.substring(lastLastIndex, match.index)));
  70.       var elmKanjiSpan = document.createElement("span");
  71.       //elmLink.setAttribute("href", match[0]);
  72.       elmKanjiSpan.setAttribute("class", kanjiClassName);
  73.       elmKanjiSpan.appendChild(document.createTextNode(match[0]));
  74.       elmSpan.appendChild(elmKanjiSpan);
  75.       lastLastIndex = kRegex.lastIndex;
  76.     }
  77.     elmSpan.appendChild(document.createTextNode(
  78.       sText.substring(lastLastIndex)));
  79.     elmSpan.normalize();
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement