Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    
  2.  
  3.     // ==UserScript==
  4.     // @name Chatvtroem
  5.     // @namespace Chatvdvoem
  6.     // @include http://chatvdvoem.ru/
  7.     // @version 2.5.10
  8.     // @grant none
  9.     // @downloadURL https://bitbucket.org/2ch_dev/cvd_double/raw/master/chatvtroem.user.js
  10.     // @description
  11.     // @encoding utf-8
  12.     // ==/UserScript==
  13.      
  14.     String.prototype._repeat = function (count) {
  15.         var result = '';
  16.         while (count--) {
  17.             result += this;
  18.         }
  19.         return result;
  20.     }
  21.      
  22.     var settings = {
  23.         'auto_reply': true,
  24.         'auto_disconnect': true,
  25.         'auto_restart': true,
  26.         'symbol': '\t',
  27.         'anonymous': false,
  28.         'write_chat_log' : false
  29.     };
  30.      
  31.     var filterWords = ['vk','id'];
  32.      
  33.     var anon_count = [];
  34.     var update_anon_count;
  35.      
  36.     var chat_log = '';
  37.      
  38.     function connect(from, to, side) {
  39.         from.initBz1 = function () {};
  40.         var process_event = from.process_event;
  41.         var last_message_by_anon = false;
  42.      
  43.         from.process_event = function (data) {
  44.             var result = process_event(data);
  45.             if (data.action == 'start_chat') {
  46.                 anon_count[side] = 0;
  47.                 update_anon_count();
  48.                 last_message_by_anon = false;
  49.             }
  50.             if (data.action == 'start_chat' && from.chat_cid == to.chat_cid) {
  51.                 from.onbeforeunload = to.onbeforeunload = undefined;
  52.                 start();
  53.             } else if (data.action == 'new_message') {
  54.                 var message = from.document.querySelector('#messages').firstChild.lastChild;
  55.                 var message_content = message.querySelector('.message').textContent;
  56.      
  57.                 var message_menu = document.createElement('span');
  58.                 message_menu.className = 'message_menu';
  59.                 message.appendChild(message_menu, message.firstChild);
  60.      
  61.                 var resend_button = add_button(message_menu, "отпр", function () {
  62.                     if (chatFilter(message_content) == true) {
  63.                         if (message_content.lastIndexOf("!no_send") == -1) {
  64.                             if (settings['anonymous'] == true) {
  65.                                 to.send('send_message', {
  66.                                     message: message_content
  67.                                 });
  68.                             } else {
  69.                                 to.send('send_message', {
  70.                                     message: message_content + settings['symbol']
  71.                                 });
  72.                             }
  73.                             to.chat_ping_send = Date().getTime();
  74.                         } else {
  75.                             console.log("Полученно сообщение от анона");
  76.                         }
  77.                     }
  78.                 });
  79.                 add_button(message_menu, "ред", function () {
  80.                     to.$('#text').val(message_content).focus();
  81.                 });
  82.      
  83.                 if (data.user == 'im') {
  84.                     var log_message_author;
  85.                     var log_color;
  86.                     if (last_message_by_anon) {
  87.                         message.querySelector('.name').innerHTML = '<i>Анон</i>';
  88.                         message.className = 'messageAnon';
  89.                         last_message_by_anon = false;
  90.                         log_message_author = 'Анон собеседнику ' + (side + 1);
  91.                         log_color = (side==0?'#0f0':'#0b0');
  92.                     } else {
  93.                         message.querySelector('.name').innerHTML = '<i>Сюда</i>';
  94.                         log_message_author = 'Собеседник ' + (2 - side);
  95.                         log_color = (side==0?'#CA3B1B':'#348ABC');
  96.                     }
  97.                     console.log('loger');
  98.                     if (settings['write_chat_log']) {
  99.                         chat_log += '<span style="color: white; background-color:' + log_color + ';">' + log_message_author
  100.                          + '</span> ' + message_content + '<br/>';
  101.                          console.log("loged");
  102.                     }
  103.                 } else {
  104.                     message.querySelector('.name').innerHTML = '<i>Отсюда</i>';
  105.      
  106.                     // говнокод для подсчёта количества tabов в конце
  107.                     // надо чем нибудь нормальным заменить
  108.                     var symbols_it = message_content.length - 1;
  109.                     while (symbols_it >= 0 && message_content.charAt(symbols_it) == settings['symbol']) {
  110.                         symbols_it -= 1;
  111.                     }
  112.                     var symbols_count = message_content.length - symbols_it - 1;
  113.      
  114.                     if (symbols_count != anon_count[side]) {
  115.                         anon_count[side] = symbols_count;
  116.                         update_anon_count();
  117.                     }
  118.      
  119.                     if (settings['auto_reply'] == true) {
  120.                         resend_button.onclick();
  121.                     }
  122.      
  123.                 }
  124.             }
  125.                
  126.     //        } else if (data.action == 'start_typing' && !to.chat_typing_status) {
  127.     //            to.send('start_typing');
  128.     //            to.chat_ping_send = Date().getTime();
  129.     //        } else if (data.action == 'stop_typing' && to.chat_typing_status) {
  130.     //            to.send('stop_typing');
  131.     //            to.chat_ping_send = Date().getTime();
  132.             else if (data.action == 'stop_chat' && data.user != 'im') {
  133.                 on_chat_stop({});
  134.             }
  135.             return result;
  136.         };
  137.      
  138.         var chat_stop = from.chat_stop;
  139.         from.chat_stop = function (opponent_init, script_params) {
  140.             if (!script_params) script_params = {};
  141.             on_chat_stop(script_params);
  142.             return chat_stop(opponent_init);
  143.         }
  144.      
  145.         function on_chat_stop(script_params) {
  146.             if (settings['write_chat_log']) {
  147.                 chat_log += '<span style="color:#fff; background-color:#999;">Собеседник ' + (side + 1) + ' ушёл от нас</span><br/>';
  148.             }
  149.             if ((settings['auto_disconnect'] == true && !script_params.auto_disc_init) ||
  150.                     script_params.force_auto_disconnect) {
  151.                 to.chat_stop(false, {
  152.                     force_restart: script_params.force_restart,
  153.                     auto_disc_init: true
  154.                 });
  155.             }
  156.             if (settings['auto_restart'] == true || script_params.force_restart) {
  157.                 safe_chat_start();
  158.             }
  159.      
  160.         }
  161.      
  162.         function migalka(newTxt) {
  163.             var oldTxt = document.title;
  164.            
  165.             if (document.title == oldTxt) {
  166.                 document.title = '** ' + newTxt + ' **';
  167.             } else {
  168.                 document.title = oldTxt;
  169.             }
  170.         }
  171.      
  172.         function chatFilter(msg) {
  173.             var q = false;
  174.            
  175.             for (var i = 0; i < filterWords.length; i++) {
  176.                 if (msg.lastIndexOf(filterWords[i]) != -1) {
  177.                     q = true;
  178.                     break;
  179.                             }
  180.             }
  181.            
  182.             if (!q) {
  183.                 clearInterval(timer);
  184.                 return true;
  185.             } else {
  186.     //            settings['auto_reply'] = false;
  187.                 var timer = setInterval(migalka('Аутист дал свои контакты'), 800);
  188.                 return false;
  189.             }
  190.         }
  191.      
  192.         function safe_chat_start() {
  193.             setTimeout(function() {
  194.                 if (from.chat_status == 'disconnect') from.chat_start();
  195.             }, 1600);
  196.         }
  197.      
  198.         from.$("#message_form").unbind().submit(function () {
  199.             if (!from.chat_cid) {
  200.                 return false;
  201.             }
  202.             last_message_by_anon = true;
  203.             from.send("send_message", {
  204.                 message: from.$("#text").val().replace(/\t/g, '') + '\t'._repeat(anon_count[side == 0 ? 1 : 0] + 1)
  205.             });
  206.             var chat_time = new Date();
  207.             from.chat_ping_send = chat_time.getTime();
  208.             from.$("#text").val('').focus();
  209.             return false;
  210.         });
  211.      
  212.         from.chat_start();
  213.     };
  214.      
  215.     function create_menu(one, two) {
  216.         var menu = document.createElement('div');
  217.         menu.id = 'menu';
  218.      
  219.         // Switches
  220.         add_switch(menu, 'auto_reply', '[✔] Пересылать', '[✘] Пересылать');
  221.         add_switch(menu, 'auto_disconnect', '[✔] Отключать автоматически', '[✘] Отключать автоматически');
  222.         add_switch(menu, 'auto_restart', '[✔] Авторестартить', '[✘] Авторестартить');
  223.         add_switch(menu, 'anonymous', '[✔] Анонимно', '[✘] Анонимно');
  224.         add_switch(menu, 'write_chat_log', '[✔] Лог чата', '[✘] Лог чата');
  225.         // Buttons
  226.         add_button(menu, 'Смотреть лог', function () {
  227.             window.open('data:text/html;charset=utf-8,' + encodeURIComponent(chat_log));
  228.         });
  229.         add_button(menu, 'Очистить лог', function () {
  230.             chat_log = '';
  231.         });
  232.         add_button(menu, 'Рестарт', function () {
  233.             one.contentWindow.chat_stop(false, {
  234.                 force_restart: true,
  235.                 force_auto_disconnect: true
  236.             });
  237.         });
  238.      
  239.         create_anon_indicator(menu);
  240.      
  241.      
  242.         document.body.appendChild(menu);
  243.     };
  244.      
  245.     function add_button(parent, name, f) {
  246.         var button = document.createElement('a');
  247.         button.setAttribute('href', '#');
  248.         button.textContent = name;
  249.         button.classList.add('menu_item');
  250.      
  251.         button.onclick = function () {
  252.             f(button);
  253.             return false;
  254.         };
  255.      
  256.         parent.appendChild(button);
  257.         return button;
  258.     };
  259.      
  260.     function add_switch(parent, settings_key, on_true, on_false, change_callback) {
  261.         var text;
  262.      
  263.         if (settings[settings_key] == true) {
  264.             text = on_true;
  265.         } else {
  266.             text = on_false;
  267.         }
  268.      
  269.         add_button(parent, text, function (e) {
  270.             settings[settings_key] = !settings[settings_key];
  271.      
  272.             if (settings[settings_key] == true) {
  273.                 e.textContent = on_true;
  274.             } else {
  275.                 e.textContent = on_false;
  276.             }
  277.             if (change_callback) change_callback();
  278.         });
  279.     };
  280.      
  281.     function create_anon_indicator(parent) {
  282.         var indicator = document.createElement('span');
  283.         indicator.classList.add('menu_item');
  284.      
  285.         anon_count = [0, 0];
  286.      
  287.         update_anon_count = function () {
  288.             indicator.innerHTML = "Счётчик анонов: " + anon_count[0] + "-" + anon_count[1];
  289.         }
  290.         parent.appendChild(indicator);
  291.         update_anon_count();
  292.     }
  293.      
  294.     function start() {
  295.      
  296.         function load_style(doc) {
  297.             var style = doc.createElement('link');
  298.             style.setAttribute('href', 'https://bitbucket.org/2ch_dev/cvd_double/downloads/style.css');
  299.             style.setAttribute('rel', 'stylesheet');
  300.             style.setAttribute('type', 'text/css');
  301.             style.setAttribute('media', 'screen');
  302.             doc.head.appendChild(style);
  303.         }
  304.      
  305.         var frameset = document.createElement('frameset');
  306.         var one = document.createElement('frame');
  307.         var two = document.createElement('frame');
  308.         frameset.setAttribute('cols', '50%, 50%');
  309.         one.addEventListener('load', function (e) {
  310.             two.setAttribute('src', '/?2');
  311.             load_style(one.contentWindow.document);
  312.         });
  313.         two.addEventListener('load', function (e) {
  314.             connect(one.contentWindow, two.contentWindow, 0);
  315.             connect(two.contentWindow, one.contentWindow, 1);
  316.             load_style(two.contentWindow.document);
  317.         });
  318.         one.setAttribute('src', '/?1');
  319.         frameset.appendChild(one);
  320.         frameset.appendChild(two);
  321.      
  322.         document.body.innerHTML = document.head.innerHTML = '';
  323.      
  324.         load_style(document);
  325.      
  326.         create_menu(one, two);
  327.      
  328.         document.body.appendChild(frameset);
  329.     };
  330.      
  331.     (function () {
  332.         var activate = document.createElement('a');
  333.         activate.setAttribute('href', '/');
  334.         activate.textContent = 'Траллеть';
  335.         activate.addEventListener('click', function (e) {
  336.             start();
  337.             e.preventDefault();
  338.         });
  339.         document.querySelector('.startChat').appendChild(activate);
  340.     })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement