2 // @name Kita-Yen 4Chan
3 // @description Add kita to your post with ctr+"k" and Yen with ctr+"\"
5 // @match *://boards.4chan.org/*
7 // @namespace https://greasyfork.org/users/125336
8 // @updateURL https://github.com/ECHibiki/4chan-UserScripts/raw/master/Kita-Yen_4chan.user.js
9 // @downloadURL https://github.com/ECHibiki/4chan-UserScripts/raw/master/Kita-Yen_4chan.user.js
14 function colorCharacters(root){
15 if(root.nodeType !== Node.ELEMENT_NODE){
19 var nodes = Array.from(root.getElementsByClassName('postMessage'));
20 if(root.classList.contains('postmessage')){
24 nodes.forEach(function(node){
25 if(node.textContent.indexOf('\xa5') <= -1 && node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!") <= -1){
28 var txtItterator = document.createNodeIterator(node, NodeFilter.SHOW_TEXT);
30 while((txtNode = txtItterator.nextNode(txtNode))){
31 var inside_node = searchYen(txtNode);
32 if(inside_node !== "-") {
33 searchKita(inside_node.firstChild);
34 txtItterator.nextNode();
37 var inside_node = searchKita(txtNode);
38 if(inside_node !== "-") {
39 searchYen(inside_node.firstChild);
40 txtItterator.nextNode();
46 var searchYen = function(text_node){
47 var hashIndex = text_node.textContent.indexOf('\xa5');
49 var splitNode = text_node.splitText(hashIndex);
51 var span = document.createElement('span');
52 span.className = "the_m_word";
54 span.appendChild(splitNode);
55 text_node.parentNode.insertBefore(span, text_node.nextSibling);
62 var searchKita = function(text_node){
63 var kIndex = text_node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!");
65 var far_split_note = text_node.splitText(kIndex + "キタ━━━(゚∀゚)━━━!!".length);
66 var splitNode = text_node.splitText(kIndex);
68 var span = document.createElement('span');
69 span.className = "the_k_word";
71 span.appendChild(splitNode);
72 text_node.parentNode.insertBefore(span, text_node.nextSibling);
79 var addStyle = function(){
80 var style = document.createElement("STYLE");
81 style.innerHTML = ".the_m_word{color:#9370DB} \n.the_k_word{color:#555555}";
82 document.head.appendChild(style);
86 colorCharacters(document.body);
89 new MutationObserver(function(mutations){
90 mutations.forEach(function(mutation){
91 mutation.addedNodes.forEach(colorCharacters);
93 }).observe(document.body, {childList: true, subtree: true});
97 var listener_obj = {};
98 window.addEventListener("keydown", function(e){
99 listener_obj[e.keyCode] = true;
101 var node = document.activeElement;
102 if (listener_obj[17] && listener_obj[75]){
104 insertAtPos(node, 'キタ━━━(゚∀゚)━━━!!');
106 if (listener_obj[17] && listener_obj[220]){
108 insertAtPos(node, '\xa5');
110 }, {passive:false, capture:false, once:false});
112 window.addEventListener("keyup", function(e){
113 listener_obj[e.keyCode] = false;
114 }, {passive:false, capture:false, once:false});
116 var insertAtPos = function(node, char){
117 var sel_start = node.selectionStart;
118 var sel_end = node.selectionEnd;
121 node.value = n_tc.substr(0, sel_start) + char + n_tc.substr(sel_end);