dailyhwm / CharFind*

// ==UserScript==
// @version 3.1.1
// @author Demin, alex_kocharin
// @description	ФОРМЫ ПОИСКА: перс - ник, ид; протокол, форум, клан, бой - чат, лог; карты, лот рынка, смс по нику.
// @icon https://www.heroeswm.ru/hwmicon.ico
// @name CharFind*
// @namespace CharFind
// @homepageURL https://greasyfork.org/ru/scripts/1250-hwmxcharfind

// @include https://*.heroeswm.ru/*
// @include https://*.lordswm.com/*

// @exclude /ch_box.php*
// @exclude */chatonline.php*
// @exclude */chat_line.php*
// @exclude */chatpost.php*
// @exclude */chat.php*
// @exclude */ticker.html*

// @license MIT
// ==/UserScript==
(function() {
    var url_cur = location.href;
    var url = 'http://' + location.hostname + '/';
    try {
        if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported") > -1)) {
            this.GM_getValue = function(key, def) {
                return localStorage[key] || def;
            };
            this.GM_setValue = function(key, value) {
                return localStorage[key] = value;
            };
            this.GM_deleteValue = function(key) {
                return delete localStorage[key];
            };
        }
        if ((tag('body'))[0]) {

            let is_lordswm = url.match('lordswm');
            var text_find = is_lordswm ? 'Search' : '<b style="text-decoration: none; color: #1199FF;">формы быстрого ПОИСКА </b>';
            var text_go1 = is_lordswm ? 'Find name: ' : 'Перс. по Нику ';
            var text_go2 = is_lordswm ? 'Character (ID): ' : 'Перс. по ID ';
            var text_go3 = is_lordswm ? 'Protocol (ID): ' : 'Передачи. по ID ';
            var text_go4 = is_lordswm ? 'Clan ID: ' : 'Клан. по ID ';
            var text_go5 = is_lordswm ? 'Mail (nick): ' : 'Почта. смс по Нику ';
            var text_go6 = is_lordswm ? 'War chat №: ' : 'Бой. Чат по № ';
            var text_go61 = is_lordswm ? 'War turn №: ' : 'Бой. ИТОГИ по № ';
            var text_go7 = is_lordswm ? 'Tavern №: ' : 'Карточная игра. по № ';
            var text_go8 = is_lordswm ? 'Forum tems (ID): ' : 'Форум. тема по ID ';
            var text_go9 = is_lordswm ? 'Auk. Lot for (ID): ' : 'Рынок. лот по ID ';

            //var alla = document.getElementsByClassName('sh_logo')[0];
            //var b = (tag('body'))[0];
            //var alla = document.querySelector("body > table > tbody > tr > td > table > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr:nth-child(2) > td > a > img");
            //var alla = document.querySelector("#hwm_topline_with_hint1");
            var alla = document.querySelector("body > table > tbody > tr > td > table > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr:nth-child(2) > td > a > img");
            var got = false;
            if (alla){
                got = true;
            } else {
                alla = document.querySelector("#hwm_topline_with_hint1");
                got = true;
            }
         for (var i = 0; i < alla.length; i++) {
                //	if (i>7) {break;}
                if (alla[i].src.indexOf('logo_') >= 0) {
                    if (alla[i].parentNode.tagName == 'A') {
                        got = alla[i];
                        break;
                    }
                }
            }
//console.log(alla)
            if (got) {
                var newdiv = document.createElement('div');
                with(newdiv.style) {
                    visibility = 'hidden';
                    position = 'absolute';
                    borderStyle = 'solid';
                    borderColor = '#000000';
                    borderWidth = '5px';
                    padding = '0px';
                    zIndex = '99999';
                    //left = '50%'; // this centr formi
                    left = '0';
                    top = '0';
                    //width = '338px'; // this centr formi
                    //marginLeft = '-169px'; // this centr formi
                }
                var ebl_logo = document.getElementsByClassName('sh_logo_out');
//console.log('logo_url', ebl_logo);
                newdiv.innerHTML = '<table cellspacing=4 cellpadding=0 bgcolor="#f5f3ea">' +
                    '<tr><td><center><b>' + text_find + '</b></center></td></tr>' +
                    '<tr><td>' + text_go1 + '<input type=text id="hwm_s_name" value="" maxlength="20" style="width: 150px;" ><input type="submit" id="hwm_s_name_ok" value="&darr;тут"><input type="submit" id="hwm_s_name_ok2" value="там&uarr;"></td></tr>' +
                    '<tr><td>' + text_go2 + '&nbsp;&nbsp;&nbsp;&nbsp;<input type=text id="hwm_s_id" value="" maxlength="11" style="width: 100px;" ><input type="submit" id="hwm_s_id_ok" value="&darr;"><input type="submit" id="hwm_s_id_ok2" value="&uarr;"></td></tr>' +
                    '<tr><td>' + text_go3 + '&nbsp;&nbsp;&nbsp;<input type=text id="hwm_p_id" value="" maxlength="11" style="width: 100px;"><input type="submit" id="hwm_p_id_ok" value="&darr;"><input type="submit" id="hwm_p_id_ok2" value="&uarr;"></td></tr>' +
                    '<tr><td>' + text_go8 + '<input type=text id="hwm_tf_id" value="" maxlength="11" style="width: 100px;"><input type="submit" id="hwm_tf_id_ok" value="&darr;"><input type="submit" id="hwm_tf_id_ok2" value="&uarr;"></td></tr>' +
                    '<tr><td>' + text_go4 + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text id="hwm_s_clanid" value="" maxlength="6" style="width: 100px;"><input type="submit" id="hwm_s_clanid_ok" value="&darr;"><input type="submit" id="hwm_s_clanid_ok2" value="&uarr;"></td></tr>' +
                    '<tr><td>' + text_go6 + '&nbsp;&nbsp;<input type=text id="hwm_s_warchat" value="" maxlength="11" style="width: 100px;"><input type="submit" id="hwm_s_warchat_ok" value="&darr;"><input type="submit" id="hwm_s_warchat_ok2" value="&uarr;"></td></tr>' +
                    '<tr><td>' + text_go61 + '&nbsp;&nbsp;<input type=text id="hwm_s_warturn" value="" maxlength="11" style="width: 100px;"><input type="submit" id="hwm_s_warturn_ok" value="&darr;"><input type="submit" id="hwm_s_warturn_ok2" value="&uarr;"></td></tr>' +
                    '<tr><td>' + text_go7 + '<input type=text id="hwm_s_tavern" value="" maxlength="11" style="width: 100px;"><input type="submit" id="hwm_s_tavern_ok" value="&darr;"></td></tr>' +
                    /* '<tr><td>'+text_go5+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=text id="hwm_s_sms" value="" maxlength="20" style="width: 150px;"><input type="submit" id="hwm_s_sms_ok" value="&darr;"><input type="submit" id="hwm_s_sms_ok2" value="&uarr;"></td></tr>'+ */
                    '<tr><td>' + text_go9 + '&nbsp;&nbsp;&nbsp;&nbsp;<input type=text id="hwm_s_auklot" value="" maxlength="11" style="width: 100px;"><input type="submit" id="hwm_s_auklot_ok" value="&darr;"><input type="submit" id="hwm_s_auklot_ok2" value="&uarr;"></td></tr>' +
                    '<tr><td>Почта. смс по Нику ' +
                    '<form style="display: inline; width: 100px;" method="post" action="sms.php">' +
                    '<input name="search_nik" style="width:150;" type="text" maxlength="20" value="" >' +
                    '<input value="»" type="submit">' +
                    '</form>' +
                    '</td></tr>' +
                    '</table>';

                document.body.appendChild(newdiv);
                addEvent($("hwm_s_name_ok"), "click", hwm_s_name_f);
                addEvent($("hwm_s_name_ok2"), "click", hwm_s_name_f2);
                addEvent($("hwm_s_id_ok"), "click", hwm_s_id_f);
                addEvent($("hwm_s_id_ok2"), "click", hwm_s_id_f2);
                addEvent($("hwm_s_clanid_ok"), "click", hwm_s_clanid_f);
                addEvent($("hwm_s_clanid_ok2"), "click", hwm_s_clanid_f2);
               /* addEvent($("hwm_s_sms_ok"), "click", hwm_s_sms_f);
                addEvent($("hwm_s_sms_ok2"), "click", hwm_s_sms_f2); */
                addEvent($("hwm_s_auklot_ok"), "click", hwm_s_auklot_f);
                addEvent($("hwm_s_auklot_ok2"), "click", hwm_s_auklot_f2);
                addEvent($("hwm_s_warchat_ok"), "click", hwm_s_warchat_f);
                addEvent($("hwm_s_warchat_ok2"), "click", hwm_s_warchat_f2);
                addEvent($("hwm_s_warturn_ok"), "click", hwm_s_warturn_f);
                addEvent($("hwm_s_warturn_ok2"), "click", hwm_s_warturn_f2);
                addEvent($("hwm_s_tavern_ok"), "click", hwm_s_tavern_f);
                addEvent($("hwm_p_id_ok"), "click", hwm_p_id_f);
                addEvent($("hwm_p_id_ok2"), "click", hwm_p_id_f2);
                addEvent($("hwm_tf_id_ok"), "click", hwm_tf_id_f);
                addEvent($("hwm_tf_id_ok2"), "click", hwm_tf_id_f2);
                addEvent($("hwm_s_name"), "keypress", handleKeys1);
                addEvent($("hwm_s_id"), "keypress", handleKeys2);
                addEvent($("hwm_s_clanid"), "keypress", handleKeys3);
               /* addEvent($("hwm_s_sms"), "keypress", handleKeys3); */
                addEvent($("hwm_s_auklot"), "keypress", handleKeys3);
                addEvent($("hwm_s_warchat"), "keypress", handleKeys3);
                addEvent($("hwm_s_warturn"), "keypress", handleKeys3);
                addEvent($("hwm_s_tavern"), "keypress", handleKeys3);
                addEvent($("hwm_p_id"), "keypress", handleKeys2);
                addEvent($("hwm_p_id_ok2"), "keypress", handleKeys3);
                addEvent($("hwm_tf_id"), "keypress", handleKeys2);
                addEvent($("hwm_tf_id_ok2"), "keypress", handleKeys3);

                //got.parentNode.href = '/search.php';
                //var wrap_a = document.getElementsByClassName('sh_logo_out')[0];
                //var bad_link = wrap_a.getElementsByTagName('a')[0];
                alla.parentNode.addEventListener('click', function(e) {
                    e.preventDefault();
                    if (newdiv.style.visibility == 'hidden') {
                        //newdiv.style.left = e.clientX + 5; // this centr formi
                        newdiv.style.left = e.clientX + 5;
                        newdiv.style.top = e.clientY + 5;
                        newdiv.style.visibility = 'visible';
                    } else {
                        newdiv.style.visibility = 'hidden';
                    }
                }, false);
            }
        }
    } finally { /* update_n() */ }

    function hwm_s_name_f() {
        if (($("hwm_s_name").value.length) >= 3) {
            window.location = url + 'pl_info.php?nick=' + urlEncode($("hwm_s_name").value);
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_name_f2() {
        if (($("hwm_s_name").value.length) >= 3) {
            window.open(url + 'pl_info.php?nick=' + urlEncode($("hwm_s_name").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_id_f() {
        if (Number($("hwm_s_id").value) >= 1 && ($("hwm_s_id").value.length) >= 1) {
            window.location = url + 'pl_info.php?id=' + ($("hwm_s_id").value);
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_id_f2() {
        if (Number($("hwm_s_id").value) >= 1 && ($("hwm_s_id").value.length) >= 1) {
            window.open(url + 'pl_info.php?id=' + ($("hwm_s_id").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_clanid_f() {
        if (Number($("hwm_s_clanid").value) >= 0 && ($("hwm_s_clanid").value.length) >= 1) {
            window.location = url + 'clan_info.php?id=' + ($("hwm_s_clanid").value);
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_clanid_f2() {
        if (Number($("hwm_s_clanid").value) >= 0 && ($("hwm_s_clanid").value.length) >= 1) {
            window.open(url + 'clan_info.php?id=' + ($("hwm_s_clanid").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_warchat_f() {
        if (Number($("hwm_s_warchat").value) >= 0 && ($("hwm_s_warchat").value.length) >= 1) {
            window.location = url + 'battlechat.php?warid=' + ($("hwm_s_warchat").value);
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_warchat_f2() {
        if (Number($("hwm_s_warchat").value) >= 0 && ($("hwm_s_warchat").value.length) >= 1) {
            window.open(url + 'battlechat.php?warid=' + ($("hwm_s_warchat").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }
    function hwm_s_warturn_f() {
        if (Number($("hwm_s_warturn").value) >= 0 && ($("hwm_s_warturn").value.length) >= 1) {
            window.location = url + 'battle.php?lastturn=-3&warid=' + ($("hwm_s_warturn").value);
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_warturn_f2() {
        if (Number($("hwm_s_warturn").value) >= 0 && ($("hwm_s_warturn").value.length) >= 1) {
            window.open(url + 'battle.php?lastturn=-3&warid=' + ($("hwm_s_warturn").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_tavern_f() {
        if (Number($("hwm_s_tavern").value) >= 0 && ($("hwm_s_tavern").value.length) >= 1) {
            window.open(url + 'cgame.php?gameid=' + ($("hwm_s_tavern").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }
   /* function hwm_s_sms_f() { if (($("hwm_s_sms").value.length)>=3) {
    	window.location = url + 'sms.php?key=' + encodeURIComponent($("hwm_s_sms").value);
    	newdiv.style.visibility = 'hidden';} }
    function hwm_s_sms_f2() { if (($("hwm_s_sms").value.length)>=3) {
    	window.open(url + 'sms.php?key=' + encodeURIComponent($("hwm_s_sms").value), "_blank");
    	newdiv.style.visibility = 'hidden';} } */
    function hwm_s_auklot_f() {
        if (Number($("hwm_s_auklot").value) >= 0 && ($("hwm_s_auklot").value.length) >= 1) {
            window.location = url + 'auction_lot_protocol.php?id=' + ($("hwm_s_auklot").value);
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_s_auklot_f2() {
        if (Number($("hwm_s_auklot").value) >= 0 && ($("hwm_s_auklot").value.length) >= 1) {
            window.open(url + 'auction_lot_protocol.php?id=' + ($("hwm_s_auklot").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_p_id_f() {
        if (Number($("hwm_p_id").value) >= 1 && ($("hwm_p_id").value.length) >= 1) {
            window.location = url + 'pl_transfers.php?id=' + ($("hwm_p_id").value);
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_p_id_f2() {
        if (Number($("hwm_p_id").value) >= 1 && ($("hwm_p_id").value.length) >= 1) {
            window.open(url + 'pl_transfers.php?id=' + ($("hwm_p_id").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_tf_id_f() {
        if (Number($("hwm_tf_id").value) >= 1 && ($("hwm_tf_id").value.length) >= 1) {
            window.location = url + 'forum_messages.php?tid=' + ($("hwm_tf_id").value);
            newdiv.style.visibility = 'hidden';
        }
    }

    function hwm_tf_id_f2() {
        if (Number($("hwm_tf_id").value) >= 1 && ($("hwm_tf_id").value.length) >= 1) {
            window.open(url + 'forum_messages.php?tid=' + ($("hwm_tf_id").value), "_blank");
            newdiv.style.visibility = 'hidden';
        }
    }

    function handleKeys1(e) {
        var evt = (e) ? e : window.event; //IE reports window.event not arg
        var c = (evt.charCode) ? evt.charCode : evt.keyCode;
        if (c == 13) {
            hwm_s_name_f();
            return;
        }
    }

    function handleKeys2(e) {
        var evt = (e) ? e : window.event; //IE reports window.event not arg
        var c = (evt.charCode) ? evt.charCode : evt.keyCode;
        if (c == 13) {
            hwm_s_id_f()
            return;
        }
    }

    function handleKeys3(e) {
        var evt = (e) ? e : window.event; //IE reports window.event not arg
        var c = (evt.charCode) ? evt.charCode : evt.keyCode;
        if (c == 13) {
            hwm_s_clanid_f()
            return;
        }
    }

    function urlEncode(str) {
        // Простая проверка
        if (!str || typeof(str) == "undefined") return;
        // Создаем хеш для хранения символов, где ключ - сам символ, а значение - его шестнадцатеричНый эквивалент
        var utf8Array = {};
        // Сначала добавляем стандартные 255 символов
        var i = j = j2 = 0;
        for (i = 0; i <= 255; i++) {
            j = parseInt(i / 16);
            var j2 = parseInt(i % 16);
            utf8Array[String.fromCharCode(i)] = ('%' + j.toString(16) + j2.toString(16)).toUpperCase();
        }
        // И отдельно проработаем кириллицу
        var rusAdditional = {
            '_': '%5F',
            'А': '%C0',
            'Б': '%C1',
            'В': '%C2',
            'Г': '%C3',
            'Д': '%C4',
            'Е': '%C5',
            'Ж': '%C6',
            'З': '%C7',
            'И': '%C8',
            'Й': '%C9',
            'К': '%CA',
            'Л': '%CB',
            'М': '%CC',
            'Н': '%CD',
            'О': '%CE',
            'П': '%CF',
            'Р': '%D0',
            'С': '%D1',
            'Т': '%D2',
            'У': '%D3',
            'Ф': '%D4',
            'Х': '%D5',
            'Ц': '%D6',
            'Ч': '%D7',
            'Ш': '%D8',
            'Щ': '%D9',
            'Ъ': '%DA',
            'Ы': '%DB',
            'Ь': '%DC',
            'Э': '%DD',
            'Ю': '%DE',
            'Я': '%DF',
            'а': '%E0',
            'б': '%E1',
            'в': '%E2',
            'г': '%E3',
            'д': '%E4',
            'е': '%E5',
            'ж': '%E6',
            'з': '%E7',
            'и': '%E8',
            'й': '%E9',
            'к': '%EA',
            'л': '%EB',
            'м': '%EC',
            'н': '%ED',
            'о': '%EE',
            'п': '%EF',
            'р': '%F0',
            'с': '%F1',
            'т': '%F2',
            'у': '%F3',
            'ф': '%F4',
            'х': '%F5',
            'ц': '%F6',
            'ч': '%F7',
            'ш': '%F8',
            'щ': '%F9',
            'ъ': '%FA',
            'ы': '%FB',
            'ь': '%FC',
            'э': '%FD',
            'ю': '%FE',
            'я': '%FF',
            'ё': '%B8',
            'Ё': '%A8'
        }
        for (i in rusAdditional) utf8Array[i] = rusAdditional[i];
        // Посимвольно заменяем символы на их шестнадцатиричные эквиваленты
        var res = "";
        for (i = 0; i < str.length; i++) {
            var simbol = str.substr(i, 1);
            res += typeof utf8Array[simbol] != "undefined" ? utf8Array[simbol] : simbol;
        }
        // Пробелы заменяем на плюсы
        //	res = res.replace(/\s/g, "+");
        //	alert(res);
        return res;
    }

    function $(id) {
        return document.getElementById(id);
    }

    function tag(id) {
        return document.getElementsByTagName(id);
    }

    function addEvent(elem, evType, fn) {
        if (elem.addEventListener) {
            elem.addEventListener(evType, fn, false);
        } else if (elem.attachEvent) {
            elem.attachEvent("on" + evType, fn)
        } else {
            elem["on" + evType] = fn
        }
    }

    function createXMLHttpReq(rndm) {
        var objXMLHttpReq;
        if (window.XMLHttpRequest) { /* проверка на РЕАЛЬНОСТЬ браузера */
            objXMLHttpReq = new XMLHttpRequest();
        } else if (window.ActiveXObject) { /* проверка на IE */
            objXMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return objXMLHttpReq;
    }

    function URLAttrValueGet(attr_name, aurl) {
        attr_name = attr_name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + attr_name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(aurl);
        if (results == null)
            return "";
        else
            return results[1];
    }
    if (location.href.indexOf('/sms.php') > -1) {
        lkey = URLAttrValueGet('key', location.href);
        if (lkey != '') {
            el = document.getElementsByName('search_nik')[0];
            el.value = decodeURIComponent(lkey);
            el.parentNode.submit();
        }
    }
})();