pwd fix
[4Free-FSE.git] / Kita-Yen_4chan.user.js
blob4dfbcaedb4b66e75d61a778b8cbe3386852b33d5
1 // ==UserScript==
2 // @name Kita-Yen 4Chan
3 // @description Add kita to your post with ctr+"k" and Yen with ctr+"\"
4 // @version 2.1
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
11 // ==/UserScript==
13 //insertion logic
14 function colorCharacters(root){
15     if(root.nodeType !== Node.ELEMENT_NODE){
16         return;
17     }
19     var nodes = Array.from(root.getElementsByClassName('postMessage'));
20     if(root.classList.contains('postmessage')){
21         nodes.unshift(root);
22     }
24     nodes.forEach(function(node){
25         if(node.textContent.indexOf('\xa5') <= -1 && node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!") <= -1){
26             return;
27         }
28         var txtItterator = document.createNodeIterator(node, NodeFilter.SHOW_TEXT);
29         var txtNode;
30         while((txtNode = txtItterator.nextNode(txtNode))){
31             var inside_node = searchYen(txtNode);
32             if(inside_node !== "-") {
33                 searchKita(inside_node.firstChild);
34                 txtItterator.nextNode();
35             }
37             var inside_node = searchKita(txtNode);
38             if(inside_node !== "-") {
39                 searchYen(inside_node.firstChild);
40                 txtItterator.nextNode();
41             }
42         }
43     });
46 var searchYen = function(text_node){
47     var hashIndex = text_node.textContent.indexOf('\xa5');
48     if(hashIndex > -1){
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);
57         return span;
58     }
59     return "-";
62 var searchKita = function(text_node){
63     var kIndex = text_node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!");
64     if(kIndex > -1){
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);
73         return span;
74     }
75     return "-";
78 //color styling
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);
85 //injection
86 colorCharacters(document.body);
87 addStyle();
89 new MutationObserver(function(mutations){
90     mutations.forEach(function(mutation){
91         mutation.addedNodes.forEach(colorCharacters);
92     });
93 }).observe(document.body, {childList: true, subtree: true});
96 //hotkeys
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]){
103         e.preventDefault();
104         insertAtPos(node, 'キタ━━━(゚∀゚)━━━!!');
105     }
106     if (listener_obj[17] && listener_obj[220]){
107         e.preventDefault();
108         insertAtPos(node, '\xa5');
109     }
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;
120     n_tc = node.value;
121     node.value = n_tc.substr(0, sel_start) + char + n_tc.substr(sel_end);