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
11 function colorCharacters(root){
12 if(root.nodeType !== Node.ELEMENT_NODE){
16 var nodes = Array.from(root.getElementsByClassName('postMessage'));
17 if(root.classList.contains('postmessage')){
21 nodes.forEach(function(node){
22 if(node.textContent.indexOf('\xa5') <= -1 && node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!") <= -1){
25 var txtItterator = document.createNodeIterator(node, NodeFilter.SHOW_TEXT);
27 while((txtNode = txtItterator.nextNode(txtNode))){
28 var inside_node = searchYen(txtNode);
29 if(inside_node !== "-") {
30 searchKita(inside_node.firstChild);
31 txtItterator.nextNode();
34 var inside_node = searchKita(txtNode);
35 if(inside_node !== "-") {
36 searchYen(inside_node.firstChild);
37 txtItterator.nextNode();
43 var searchYen = function(text_node){
44 var hashIndex = text_node.textContent.indexOf('\xa5');
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);
59 var searchKita = function(text_node){
60 var kIndex = text_node.textContent.indexOf("キタ━━━(゚∀゚)━━━!!");
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);
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);
83 colorCharacters(document.body);
86 new MutationObserver(function(mutations){
87 mutations.forEach(function(mutation){
88 mutation.addedNodes.forEach(colorCharacters);
90 }).observe(document.body, {childList: true, subtree: true});
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]){
101 insertAtPos(node, 'キタ━━━(゚∀゚)━━━!!');
103 if (listener_obj[17] && listener_obj[220]){
105 insertAtPos(node, '\xa5');
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;
118 node.value = n_tc.substr(0, sel_start) + char + n_tc.substr(sel_end);