Moved scripts from Greasyfork to Git
[4Free-FSE.git] / Kita-Yen_4chan.js
bloba79ca10eeedad702f7792b68ecebf92bb6314baa
1 // ==UserScript==
2 // @name Kita-Yen_4Chan
3 // @description Add kita to your post with ctr+"k" and Yen with ctr+"\"
4 // @version 2.0
5 // @match *://boards.4chan.org/*
6 // @grant none
7 // @namespace https://greasyfork.org/users/125336
8 // ==/UserScript==
10 //insertion logic
11 function colorCharacters(root){
12     if(root.nodeType !== Node.ELEMENT_NODE){
13         return;
14     }
16     var nodes = Array.from(root.getElementsByClassName('postMessage'));
17     if(root.classList.contains('postmessage')){
18         nodes.unshift(root);
19     }
21     nodes.forEach(function(node){
22         if(node.textContent.indexOf('\xa5') <= -1 && node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!") <= -1){
23             return;
24         }
25         var txtItterator = document.createNodeIterator(node, NodeFilter.SHOW_TEXT);
26         var txtNode;
27         while((txtNode = txtItterator.nextNode(txtNode))){
28             var inside_node = searchYen(txtNode);
29             if(inside_node !== "-") {
30                 searchKita(inside_node.firstChild);
31                 txtItterator.nextNode();
32             }
34             var inside_node = searchKita(txtNode);
35             if(inside_node !== "-") {
36                 searchYen(inside_node.firstChild);
37                 txtItterator.nextNode();
38             }
39         }
40     });
43 var searchYen = function(text_node){
44     var hashIndex = text_node.textContent.indexOf('\xa5');
45     if(hashIndex > -1){
46         var splitNode = text_node.splitText(hashIndex);
48         var span = document.createElement('span');
49         span.className = "the_m_word";
51         span.appendChild(splitNode);
52         text_node.parentNode.insertBefore(span, text_node.nextSibling);
54         return span;
55     }
56     return "-";
59 var searchKita = function(text_node){
60     var kIndex = text_node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!");
61     if(kIndex > -1){
62         var far_split_note =  text_node.splitText(kIndex + "キタ━━━(゚∀゚)━━━!!".length);
63         var splitNode = text_node.splitText(kIndex);
65         var span = document.createElement('span');
66         span.className = "the_k_word";
68         span.appendChild(splitNode);
69         text_node.parentNode.insertBefore(span, text_node.nextSibling);
70         return span;
71     }
72     return "-";
75 //color styling
76 var addStyle = function(){
77     var style = document.createElement("STYLE");
78     style.innerHTML = ".the_m_word{color:#9370DB} \n.the_k_word{color:#555555}";
79     document.head.appendChild(style);
82 //injection
83 colorCharacters(document.body);
84 addStyle();
86 new MutationObserver(function(mutations){
87     mutations.forEach(function(mutation){
88         mutation.addedNodes.forEach(colorCharacters);
89     });
90 }).observe(document.body, {childList: true, subtree: true});
93 //hotkeys
94 var listener_obj = {};
95 window.addEventListener("keydown", function(e){
96     listener_obj[e.keyCode] = true;
98     var node = document.activeElement;
99     if (listener_obj[17] && listener_obj[75]){
100         e.preventDefault();
101         insertAtPos(node, 'キタ━━━(゚∀゚)━━━!!');
102     }
103     if (listener_obj[17] && listener_obj[220]){
104         e.preventDefault();
105         insertAtPos(node, '\xa5');
106     }
107 }, {passive:false, capture:false, once:false});
109 window.addEventListener("keyup", function(e){
110     listener_obj[e.keyCode] = false;
111 }, {passive:false, capture:false, once:false});
113 var insertAtPos = function(node, char){
114     var sel_start = node.selectionStart;
115     var sel_end = node.selectionEnd;
117     n_tc = node.value;
118     node.value = n_tc.substr(0, sel_start) + char + n_tc.substr(sel_end);