MD5 filter bugfix
[4Free-FSE.git] / Kita-Yen_4chan.user.js
blobbe76316719814b5cef1ecc197c19ba6025603009
1 // ==UserScript==
2 // @name Kita-Yen 4Chan
3 // @description Add kita to your post with ctr+"k" and Yen with ctr+"\"
4 // @version 2.5
5 // @match *://boards.4chan.org/*
6 // @grant none
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
11 // ==/UserScript==
15 document.addEventListener("PostsInserted", function(e){
16         colorCharacters(document.body);
17 });
18 document.addEventListener("4chanXInitFinished", function(e){
19         //injection
20         addStyle();
21         colorCharacters(document.body);
22         hotkeyListeners();
23 });
25 //color styling
26 function addStyle(){
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]){
41                         e.preventDefault();
42                         insertAtPos(node, 'キタ━━━(゚∀゚)━━━!!');
43                 }
44                 if (listener_obj[17] && listener_obj[220]){
45                         e.preventDefault();
46                         insertAtPos(node, '\xa5');
47                 }
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});
55 //insertion logic
56 function colorCharacters(root){
57     if(root.nodeType !== Node.ELEMENT_NODE){
58         return;
59     }
61     var nodes = Array.from(root.getElementsByClassName('postMessage'));
62     if(root.classList.contains('postmessage')){
63                 //insert above nodes, the root.
64         nodes.unshift(root);
65     }
67     nodes.forEach(function(node){
68         if(node.textContent.indexOf('\xa5') <= -1 && node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!") <= -1){
69             return;
70         }
71         var txtItterator = document.createNodeIterator(node, NodeFilter.SHOW_TEXT);
72         var text_node;
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);
77         }
78     });
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;
85     var result;
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
91                 do{
92                         result = searchKita(text_node);
93                         if(result != false){
94                                 //jump foreward to point after kita inserted
95                                 text_node = txtItterator.nextNode();
96                                 text_node = txtItterator.nextNode();
97                         }
98                 } while(result != false);
99         }
100         //scan for outside kita from start
101         do{
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');
112     if(yenIndex > -1){
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);
121         return span;
122     }
123     return false;
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("キタ━━━(゚∀゚)━━━!!");
131     if(kIndex > -1){
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);
140         return span;
141     }
142     return false;
146 function insertAtPos(node, buzzwords){
147     var sel_start = node.selectionStart;
148     var sel_end = node.selectionEnd;
150     n_tc = node.value;
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;