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
18 colorCharacters(document.body);
21 new MutationObserver(function(mutations){
22 //send a MutationRecord for each mutation
23 mutations.forEach(function(mutation){
24 //For this mutation record access the addedNode property and call a function on it.
25 /* forEach passes on the element, index and array insance */
26 mutation.addedNodes.forEach(colorCharacters);
28 }).observe(document.body, {childList: true, subtree: true});
34 function colorCharacters(root){
35 if(root.nodeType !== Node.ELEMENT_NODE){
39 var nodes = Array.from(root.getElementsByClassName('postMessage'));
40 if(root.classList.contains('postmessage')){
41 //insert above nodes, the root.
45 nodes.forEach(function(node){
46 if(node.textContent.indexOf('\xa5') <= -1 && node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!") <= -1){
49 var txtItterator = document.createNodeIterator(node, NodeFilter.SHOW_TEXT);
51 while((text_node = txtItterator.nextNode())){
52 //disregard text inside of A tag links and already colored text
53 if(text_node.parentNode.tagName == "A" || /the_[a-z]_word/g.test(text_node.parentNode.className)) continue;
54 setColor(text_node, txtItterator);
59 //give color to text inside of nodes.
60 // first scan for yen symbols and then check the front of the text for not nested kita.
61 function setColor(text_node, txtItterator){
62 var start_text_node = text_node;
64 var yen_node = searchYen(text_node);
65 if(yen_node != false){
66 //jump to internal node
67 text_node = txtItterator.nextNode();
68 //scan for nested kita
70 var result = searchKita(text_node);
72 //jump foreward to point after kita inserted
73 text_node = txtItterator.nextNode();
74 text_node = txtItterator.nextNode();
76 }while(result != false);
79 //scan for outside kita from start
81 var result = searchKita(start_text_node);
82 start_text_node = result.nextSibling;
83 }while(result != false && start_text_node !== undefined);
87 //find the location of a yen, split the text from above that position, create a span element and place split into this span.
88 //Then take the initial text node and insert into it from after the text node.
89 function searchYen (text_node){
90 var yenIndex = text_node.textContent.indexOf('\xa5');
92 var splitNode = text_node.splitText(yenIndex);
94 var span = document.createElement('span');
95 span.className = "the_m_word";
97 span.appendChild(splitNode);
98 text_node.parentNode.insertBefore(span, text_node.nextSibling);
105 //find the location of a kita, isolate it by splitting from the point where the kita ends and the point where it begins.
106 //Now that there are 3 text nodes, take the middle one from the start position index split, add the text which goes to the point of the rightmost split,
107 //then refer back to the parent and place it after the leftmost string.
108 function searchKita (text_node){
109 var kIndex = text_node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!");
111 var far_split_note = text_node.splitText(kIndex + "キタ━━━(゚∀゚)━━━!!".length);
112 var splitNode = text_node.splitText(kIndex);
114 var span = document.createElement('span');
115 span.className = "the_k_word";
117 span.appendChild(splitNode);
118 text_node.parentNode.insertBefore(span, text_node.nextSibling);
126 var style = document.createElement("STYLE");
127 style.innerHTML = ".the_m_word{color:#9370DB} \n.the_k_word{color:#555555}";
128 document.head.appendChild(style);
131 //hotkeys for kita and yen
132 var listener_obj = {};
133 window.addEventListener("keydown", function(e){
134 listener_obj[e.keyCode] = true;
136 var node = document.activeElement;
137 if (listener_obj[17] && listener_obj[75]){
139 insertAtPos(node, 'キタ━━━(゚∀゚)━━━!!');
141 if (listener_obj[17] && listener_obj[220]){
143 insertAtPos(node, '\xa5');
145 }, {passive:false, capture:false, once:false});
147 window.addEventListener("keyup", function(e){
148 listener_obj[e.keyCode] = false;
149 }, {passive:false, capture:false, once:false});
151 function insertAtPos(node, buzzwords){
152 var sel_start = node.selectionStart;
153 var sel_end = node.selectionEnd;
156 node.value = n_tc.substr(0, sel_start) + buzzwords + n_tc.substr(sel_end);
158 node.selectionStart = sel_start + buzzwords.length;
159 node.selectionEnd = sel_end + buzzwords.length;