Yokayo / 2ch buttons fix

// ==UserScript==
// @name            2ch buttons fix
// @version         0.1.1
// @license MIT
// @description     Заменяет стрелочку на обычный сет кнопок (2ch)
// @inject-into     content
// @run-at          document-start
// @grant           GM_xmlhttpRequest
// @grant           GM.getValue
// @grant           GM.setValue
// @grant           GM.deleteValue
// @grant           GM.xmlHttpRequest
// @grant           unsafeWindow
// @include         https://2ch.*/*
// ==/UserScript==
/* eslint-disable */

function calculateGlobalXOffset(el){
    var a = 0;
    while(el){
        a += el.offsetLeft;
        el = el.offsetParent;
    }
    return a;
}

function calculateGlobalYOffset(el){
    var a = 0;
    while(el){
        a += el.offsetTop;
        el = el.offsetParent;
    }
    return a;
}

function hide_post(post, store){
    if(post.isThread()){
        post._renderHideThread(null);
    }else{
        var hidePost = document.getElementById('post-body-' + post.num);
        if(!hidePost)
            return;
        var newPost = hidePost.cloneNode(true);
        newPost.classList.add('post_type_hidden');
        hidePost.parentNode.insertBefore(newPost, hidePost);
        hidePost.parentNode.removeChild(hidePost);
    }
    if(store)
        post._storeHide();
}

document.addEventListener('DOMContentLoaded', main);

function main(){
    if(window.location.href.indexOf('2ch.') == -1 || window.location.href.indexOf('arch') != -1)
        return;
    var e, a;
    window.board = window.location.href.split("2ch")[1].split("/")[1];
    if(!window.board || window.board == '')
        return;
    //alert(window.board);
    if(document.getElementsByClassName("post__number").length == 0){
        e = document.getElementsByClassName("postbtn-reply-href post__reflink");
        for(a = 0; a < e.length; a++){
            if(!e[a].appendedButtons){
                b = e[a].getAttribute("data-num");
                //e[a].parentNode.className = 'post_btn';
                e[a].insertAdjacentHTML('afterend', "   " + (e[a].parentNode.parentNode.parentNode.className == "post post_type_oppost" ? "<svg aria-hidden='true' focusable='false' data-num='" + b + "' data-prefix='far' data-icon='star' class='postbtn-fav svg-inline--fa fa-star fa-w-18' title='Добавить в избранное' style='position: relative; top: 2px; cursor: pointer; height: 14px; width: 14px;' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 576 512'><path fill='"
                                                             + (Favorites.isFavorited(b) ? "rgb(255, 102, 0)" : "currentColor") + "' d='"
                                                             + (Favorites.isFavorited(b) ? "M259.3 17.8L194 150.2 47.9 171.5c-26.2 3.8-36.7 36.1-17.7 54.6l105.7 103-25 145.5c-4.5 26.3 23.2 46 46.4 33.7L288 439.6l130.7 68.7c23.2 12.2 50.9-7.4 46.4-33.7l-25-145.5 105.7-103c19-18.5 8.5-50.8-17.7-54.6L382 150.2 316.7 17.8c-11.7-23.6-45.6-23.9-57.4 0z" : "M528.1 171.5L382 150.2 316.7 17.8c-11.7-23.6-45.6-23.9-57.4 0L194 150.2 47.9 171.5c-26.2 3.8-36.7 36.1-17.7 54.6l105.7 103-25 145.5c-4.5 26.3 23.2 46 46.4 33.7L288 439.6l130.7 68.7c23.2 12.2 50.9-7.4 46.4-33.7l-25-145.5 105.7-103c19-18.5 8.5-50.8-17.7-54.6zM388.6 312.3l23.7 138.4L288 385.4l-124.3 65.3 23.7-138.4-100.6-98 139-20.2 62.2-126 62.2 126 139 20.2-100.6 98z") + "'></path></svg> <a class='postbtn-expand' data-num='" + b + "' style='background: url(data:image/gif;base64,R0lGODlhDgAOAJEAAPDw8IyMjP///wAAACH5BAEAAAIALAAAAAAOAA4AQAIcVI55pgDanIS0Imfvy3LnH3QfNnpUR1YpwmRCAQA7) no-repeat; display: inline-block; width: 14px; height: 14px; cursor: pointer; position: relative; top: 2px;'></a> " : "   ") + "<a class=\"postbtn-hide\" data-num=\"" + b + "\" style=\"cursor: pointer; background: url(data:image/gif;base64,R0lGODlhDgAOAJEAAPDw8IyMjP///wAAACH5BAEAAAIALAAAAAAOAA4AQAIUVI55pu0Pozyg2jqz3u9evjGRUAAAOw==); display: inline-block; width: 14px; height: 14px; position: relative; top: 2px\"></a> <a class=\"postbtn-report\" style=\"background: url(data:image/gif;base64,R0lGODlhDgAOAKECAIyMjPDw8P///////yH5BAEKAAIALAAAAAAOAA4AAAIZFI55pu0QwktxomqhzNj2+WXc5pGiwlhCAQA7); display: inline-block; width: 14px; height: 14px; cursor: pointer; position: relative; top: 2px\"></a> <a class=\"postbtn-options\" data-num=\"" + b + "\" style=\"background: url(data:image/gif;base64,R0lGODlhDgAOAJEAAPDw8IyMjP///wAAACH5BAEAAAIALAAAAAAOAA4AQAIaVI55huCvGoy0WvoyBrcr52XgJpKcRzGXUAAAOw==); display: inline-block; width: 14px; height: 14px; cursor: pointer; position: relative; top: 2px\"></a>");
                //e[a].parentNode.parentNode.removeChild(e[a].parentNode.nextSibling.nextSibling);
                e[a].parentNode.nextSibling.nextSibling.style.display = "none";
                e[a].appendedButtons = true;
            }
        }
    }else{
        e = document.querySelectorAll("[class=post__number]");
        for(a = 0; a < e.length; a++){
            if(!e[a].appendedButtons){
                b = e[a].previousSibling.getAttribute("data-num");
                e[a].insertAdjacentHTML('afterend', "   " + (e[a].parentNode.parentNode.parentNode.className == "post post_type_oppost" ? "<svg aria-hidden='true' focusable='false' data-num='" + b + "' data-prefix='far' data-icon='star' class='postbtn-fav svg-inline--fa fa-star fa-w-18' title='Добавить в избранное' style='position: relative; top: 2px; cursor: pointer; height: 14px; width: 14px;' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 576 512'><path fill='"
                                                             + (Favorites.isFavorited(b) ? "rgb(255, 102, 0)" : "currentColor") + "' d='"
                                                             + (Favorites.isFavorited(b) ? "M259.3 17.8L194 150.2 47.9 171.5c-26.2 3.8-36.7 36.1-17.7 54.6l105.7 103-25 145.5c-4.5 26.3 23.2 46 46.4 33.7L288 439.6l130.7 68.7c23.2 12.2 50.9-7.4 46.4-33.7l-25-145.5 105.7-103c19-18.5 8.5-50.8-17.7-54.6L382 150.2 316.7 17.8c-11.7-23.6-45.6-23.9-57.4 0z" : "M528.1 171.5L382 150.2 316.7 17.8c-11.7-23.6-45.6-23.9-57.4 0L194 150.2 47.9 171.5c-26.2 3.8-36.7 36.1-17.7 54.6l105.7 103-25 145.5c-4.5 26.3 23.2 46 46.4 33.7L288 439.6l130.7 68.7c23.2 12.2 50.9-7.4 46.4-33.7l-25-145.5 105.7-103c19-18.5 8.5-50.8-17.7-54.6zM388.6 312.3l23.7 138.4L288 385.4l-124.3 65.3 23.7-138.4-100.6-98 139-20.2 62.2-126 62.2 126 139 20.2-100.6 98z") + "'></path></svg> <a class='postbtn-expand' data-num='" + b + "' style='background: url(data:image/gif;base64,R0lGODlhDgAOAJEAAPDw8IyMjP///wAAACH5BAEAAAIALAAAAAAOAA4AQAIcVI55pgDanIS0Imfvy3LnH3QfNnpUR1YpwmRCAQA7) no-repeat; display: inline-block; width: 14px; height: 14px; cursor: pointer; position: relative; top: 2px;'></a> " : "   ") + "<a class=\"postbtn-hide\" data-num=\"" + b + "\" style=\"cursor: pointer; background: url(data:image/gif;base64,R0lGODlhDgAOAJEAAPDw8IyMjP///wAAACH5BAEAAAIALAAAAAAOAA4AQAIUVI55pu0Pozyg2jqz3u9evjGRUAAAOw==); display: inline-block; width: 14px; height: 14px; position: relative; top: 2px\"></a> <a class=\"postbtn-report\" style=\"background: url(data:image/gif;base64,R0lGODlhDgAOAKECAIyMjPDw8P///////yH5BAEKAAIALAAAAAAOAA4AAAIZFI55pu0QwktxomqhzNj2+WXc5pGiwlhCAQA7); display: inline-block; width: 14px; height: 14px; cursor: pointer; position: relative; top: 2px\"></a> <a class=\"postbtn-options\" data-num=\"" + b + "\" style=\"background: url(data:image/gif;base64,R0lGODlhDgAOAJEAAPDw8IyMjP///wAAACH5BAEAAAIALAAAAAAOAA4AQAIaVI55huCvGoy0WvoyBrcr52XgJpKcRzGXUAAAOw==); display: inline-block; width: 14px; height: 14px; cursor: pointer; position: relative; top: 2px\"></a>");
                //e[a].parentNode.parentNode.removeChild(e[a].parentNode.nextSibling.nextSibling);
                e[a].parentNode.nextSibling.nextSibling.style.display = "none";
                e[a].appendedButtons = true;
            }
        }
    }
    e = document.getElementsByClassName("postbtn-fav");
    for(a = 0; a < e.length; a++){
        e[a].addEventListener('mouseup', function(){Favorites.add(this.getAttribute('data-num')); this.children[0].setAttribute('fill', (Favorites.isFavorited(this.getAttribute('data-num')) ? 'rgb(255, 102, 0)' : 'currentColor')); this.children[0].setAttribute('d', Favorites.isFavorited(this.getAttribute('data-num')) ? 'M259.3 17.8L194 150.2 47.9 171.5c-26.2 3.8-36.7 36.1-17.7 54.6l105.7 103-25 145.5c-4.5 26.3 23.2 46 46.4 33.7L288 439.6l130.7 68.7c23.2 12.2 50.9-7.4 46.4-33.7l-25-145.5 105.7-103c19-18.5 8.5-50.8-17.7-54.6L382 150.2 316.7 17.8c-11.7-23.6-45.6-23.9-57.4 0z' : 'M528.1 171.5L382 150.2 316.7 17.8c-11.7-23.6-45.6-23.9-57.4 0L194 150.2 47.9 171.5c-26.2 3.8-36.7 36.1-17.7 54.6l105.7 103-25 145.5c-4.5 26.3 23.2 46 46.4 33.7L288 439.6l130.7 68.7c23.2 12.2 50.9-7.4 46.4-33.7l-25-145.5 105.7-103c19-18.5 8.5-50.8-17.7-54.6zM388.6 312.3l23.7 138.4L288 385.4l-124.3 65.3 23.7-138.4-100.6-98 139-20.2 62.2-126 62.2 126 139 20.2-100.6 98z');});
    }
    e = document.getElementsByClassName("postbtn-hide");
    for(a = 0; a < e.length; a++){
        if(e[a].processed)
            continue;
        e[a].dataNum = e[a].getAttribute("data-num");
        e[a].addEventListener('mouseup', function(){var post = Post(this.dataNum); hide_post(post, true);});
        e[a].processed = true;
    }
    e = document.getElementsByClassName("postbtn-expand");
    for(a = 0; a < e.length; a++){
        if(e[a].processed)
            continue;
        e[a].dataNum = e[a].getAttribute('data-num');
        e[a].addEventListener('mouseup', function(){PostF.expandThread(this.dataNum);});
    }
    var abu_select_menu = document.createElement("span");
    abu_select_menu.setAttribute("id", "ABU-select");
    abu_select_menu.setAttribute("class", "modal");
    document.body.appendChild(abu_select_menu);
    var b = document.createElement("a");
    //b.href = "#";
    b.innerHTML = "Ответить";
    b.addEventListener('mouseup', function(){document.getElementById(this.parentNode.dataNum).nextSibling.click(); window.AbuSelectOpen = false; this.parentNode.style.display = "none";});
    abu_select_menu.appendChild(b);
    b = document.createElement("a");
    //b.href = "#";
    b.innerHTML = "Скрыть";
    b.addEventListener('mouseup', function(){hide_post(Post(this.parentNode.dataNum), true); window.AbuSelectOpen = false; this.parentNode.style.display = "none";});
    abu_select_menu.appendChild(b);
    b = document.createElement("a");
    //b.href = "#";
    b.innerHTML = "Пожаловаться";
    b.addEventListener('mouseup', function(){
        var post = Post(this.parentNode.dataNum);
        this.parentNode.style.display = "none";
        var comment = prompt("Введите причину");
        if(comment == null){
            window.AbuSelectOpen = false;
            this.parentNode.parentNode.removeChild(this.parentNode);
            return;
        }
        var formData = new FormData();
        var data = {'board': window.board, 'thread': post.getThread(), 'posts': this.parentNode.dataNum, 'comment': comment, 'task': 'report',};
        for(var key in data){
            formData.append(key, data[key]);
        }
        $alert( "Работаем..." );
        $.ajax({
            method: "POST",
            url:'/makaba/makaba.fcgi?json=1',
            data: formData,
            success: function() {
                $alert( "Накляузничано." );
            },
            contentType: false,
            processData: false
        })
        window.AbuSelectOpen = false;
        this.parentNode.style.display = "none";});
    abu_select_menu.appendChild(b);
    abu_select_menu.style.position = "absolute";
    abu_select_menu.style.border = "1px solid #808080";
    abu_select_menu.style.display = "none";
    abu_select_menu.style.top = "0px";
    abu_select_menu.style.left = "0px";
    e = document.getElementsByClassName("postbtn-options");
    for(a = 0; a < e.length; a++){
        if(e[a].processed) // adding AbuSelect callback
            continue;
        e[a].addEventListener('mouseup', function(){
            if(!window.AbuSelectOpen){
                abu_select_menu.style.left = (calculateGlobalXOffset(this) + this.clientWidth).toString() + "px";
                abu_select_menu.style.top = calculateGlobalYOffset(this).toString() + "px";
                abu_select_menu.style.display = "inline-block";
                abu_select_menu.dataNum = this.getAttribute("data-num");
                window.AbuSelectOpen = true;
            }else{
                abu_select_menu.style.display = "none";
                window.AbuSelectOpen = false;
            }
        });
        e[a].processed = true;
    }
}

function hidePost(num, store){ // custom storage, currently unneeded
    e = document.querySelectorAll('[id="post-' + num + '"]');
    var isOP = false;
    if(!e)
        return;
    if(e[0].className.indexOf("oppost") != -1){
        e = e[0].parentNode;
        isOP = true;
    }else{
        e = e[0];
    }
    e.style.display = "none";
    if(isOP){
        //alert(e.children[0].children[0].children[0]);
        var title = e.children[0].children[0].children[0].children[1].innerHTML;
        title = title.replace(/	/g, '');
        title = title.replace(/\n/g, '');
        if(title[title.length-1] == ' ')
         title = title.substring(0, title.length-1);
        e.insertAdjacentHTML('afterend', '<div id="hidden-thread-' + num + '-wrapper"><div id="hidden-thread-' + num + '" style="display: inline-block; margin-left: 10px; height: 20px; background: #DDDDDD; box-shadow: 0px 0px 0px 2px #7F7F7F; cursor: pointer; margin-top: 5px; margin-bottom: 5px;" data-num="' + num + '" class="thread thread_hidden">Скрытый тред <span style="font-weight: bold">№' + num + '</span> <span style="font-style: italic">(' + title + ')</span></div></div>');
    }else{
        alert(e.getAttribute('id'));
        var title = e.children[0].children[0].children[1].innerHTML + ' ' + e.children[0].children[0].children[2].innerHTML + ' ' + e.children[0].children[0].children[3].innerHTML;
        e.insertAdjacentHTML('afterend', '<div id="hidden-post-' + num + '-wrapper"><div id="hidden-post-' + num + '" style="display: inline-block; margin-left: 10px; height: 20px; background: #DDDDDD; box-shadow: 0px 0px 0px 2px #7F7F7F; cursor: pointer; margin-top: 5px; margin-bottom: 5px;" data-num="' + num + '" class="post post_hidden">' + title + '</div></div>');
    }
    if(store){
        var hidden = Store.get('board.' + window.board + '.hidden', []);
        if(hidden.constructor != Array){
            hidden = new Array();
            Store.set('board.' + window.board + '.hidden', hidden);
        }
        var inHiddenList = false;
        for(var a = 0; a < hidden.length; a++){
            if(hidden[a].num == num){
                inHiddenList = true;
                break;
            }
        }
        if(!inHiddenList){
            var list_entry = {"num": num, "time_added": getTimeInDays()};
            hidden.push(list_entry);
            Store.set('board.' + window.board + '.hidden', hidden);
        }
    }
    var c = document.querySelectorAll('[id="hidden-' + (isOP ? 'thread' : 'post') + '-' + num + '-wrapper"]');
    if(c.length < 1)
        return;
    c[0].addEventListener('mouseup', function(){unhidePost(this.children[0].getAttribute('data-num')); this.parentNode.removeChild(this);});
    return;
}

function unhidePost(num){
    e = document.querySelectorAll('[id="hidden-post-' + num + '-wrapper"]');
    var isOP, isStoreHidden = false;
    if(e.length == 0){
        e = document.querySelectorAll('[id="hidden-thread-' + num + '-wrapper"]');
        if(e.length > 0)
            isOP = true;
        else
            return;
    }
    e[0].previousSibling.style.display = 'block';
    var hidden = Store.get('board.' + window.board + '.hidden', []);
        for(var a = 0; a < hidden.length; a++){
            if(hidden[a].num == num){
                //isStoreHidden = true;
                hidden.splice(a, 1);
                Store.set('board.' + window.board + '.hidden', hidden);
                break;
            }
        }
}


function getTimeInDays() {
    return Math.ceil((+new Date)/1000/60/60/24);
}