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
10 // @run-at document-start
15 document.addEventListener("PostsInserted", function(e){
16 colorCharacters(document.body);
18 document.addEventListener("4chanXInitFinished", function(e){
21 colorCharacters(document.body);
27 var style = document.createElement("STYLE");
28 style.innerHTML = ".the_m_word{color:#9370DB} \n.the_k_word{color:#555555}";
29 document.head.appendChild(style);
32 //hotkeys for kita and yen
33 function hotkeyListeners(){
34 var listener_obj = {};
36 window.addEventListener("keydown", function(e){
37 listener_obj[e.keyCode] = true;
39 var node = document.activeElement;
40 if (listener_obj[17] && listener_obj[75]){
42 insertAtPos(node, 'キタ━━━(゚∀゚)━━━!!');
44 if (listener_obj[17] && listener_obj[220]){
46 insertAtPos(node, '\xa5');
48 }, {passive:false, capture:false, once:false});
50 window.addEventListener("keyup", function(e){
51 listener_obj[e.keyCode] = false;
52 }, {passive:false, capture:false, once:false});
56 function colorCharacters(root){
57 if(root.nodeType !== Node.ELEMENT_NODE){
61 var nodes = Array.from(root.getElementsByClassName('postMessage'));
62 if(root.classList.contains('postmessage')){
63 //insert above nodes, the root.
67 nodes.forEach(function(node){
68 if(node.textContent.indexOf('\xa5') <= -1 && node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!") <= -1){
71 var txtItterator = document.createNodeIterator(node, NodeFilter.SHOW_TEXT);
73 while((text_node = txtItterator.nextNode())){
74 //disregard text inside of A tag links and already colored text
75 if(text_node.parentNode.tagName == "A" || /the_[a-z]_word/g.test(text_node.parentNode.className)) continue;
76 setColor(text_node, txtItterator);
81 //give color to text inside of nodes.
82 // first scan for yen symbols and then check the front of the text for not nested kita.
83 function setColor(text_node, txtItterator){
84 var start_text_node = text_node;
86 var yen_node = searchYen(text_node);
87 if(yen_node != false){
88 //jump to internal node
89 text_node = txtItterator.nextNode();
90 //scan for nested kita
92 result = searchKita(text_node);
94 //jump foreward to point after kita inserted
95 text_node = txtItterator.nextNode();
96 text_node = txtItterator.nextNode();
98 } while(result != false);
100 //scan for outside kita from start
102 result = searchKita(start_text_node);
103 start_text_node = result.nextSibling;
104 }while(result != false && start_text_node !== undefined);
108 //find the location of a yen, split the text from above that position, create a span element and place split into this span.
109 //Then take the initial text node and insert into it from after the text node.
110 function searchYen (text_node){
111 var yenIndex = text_node.textContent.indexOf('\xa5');
113 var splitNode = text_node.splitText(yenIndex);
115 var span = document.createElement('span');
116 span.className = "the_m_word";
118 span.appendChild(splitNode);
119 text_node.parentNode.insertBefore(span, text_node.nextSibling);
126 //find the location of a kita, isolate it by splitting from the point where the kita ends and the point where it begins.
127 //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,
128 //then refer back to the parent and place it after the leftmost string.
129 function searchKita (text_node){
130 var kIndex = text_node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!");
132 var far_split_note = text_node.splitText(kIndex + "キタ━━━(゚∀゚)━━━!!".length);
133 var splitNode = text_node.splitText(kIndex);
135 var span = document.createElement('span');
136 span.className = "the_k_word";
138 span.appendChild(splitNode);
139 text_node.parentNode.insertBefore(span, text_node.nextSibling);
146 function insertAtPos(node, buzzwords){
147 var sel_start = node.selectionStart;
148 var sel_end = node.selectionEnd;
151 node.value = n_tc.substr(0, sel_start) + buzzwords + n_tc.substr(sel_end);
153 node.selectionStart = sel_start + buzzwords.length;
154 node.selectionEnd = sel_end + buzzwords.length;