Galac / Okord : Delete messages

// ==UserScript==
// @name         Okord : Delete messages
// @namespace    http://your.homepage/
// @version      0.1
// @description  This script helps to delete messages in Okord
// @author       Galac
// @match        *fr01.okord.com/diplomacy.html*
// @grant        none
// ==/UserScript==

var spyId = "deleteSpy";
var battleId = "deleteBattle";
var transportId = "deleteTransport";
var feudalityId = "deleteFeudality";
var privateId = "deletePrivate";
var sentId = "deleteSent";
var btnDeleteMessageId = "deleteMessages";

createSpyInactivesButton();

function createSpyInactivesButton() {
    
    $(document).ready(function() {
        
        var divNode = document.createElement("DIV");
        var btnDeleteMessage = document.createElement("button");
        btnDeleteMessage.id = btnDeleteMessageId;
        var txtBtnDeleteMessage = document.createTextNode("Supprimer les messages");
        btnDeleteMessage.appendChild(txtBtnDeleteMessage);
        divNode.appendChild(btnDeleteMessage);

        createCheckBox(divNode, spyId, "Espionnages", true);
        createCheckBox(divNode, battleId, "Batailles", true);
        createCheckBox(divNode, transportId, "Transports", false);
        createCheckBox(divNode, feudalityId, "Féodalités", false);
        createCheckBox(divNode, privateId, "Privés", false);
        createCheckBox(divNode, sentId, "Privés (envoyés)", false);

        document.getElementById("general").appendChild(divNode);

        $('#' + btnDeleteMessageId).click(function(){ 
            deleteMessages();
        });
    });
};

function createCheckBox(divNode, id, txt, checkedByDefault){

    var divNodeChild = document.createElement("DIV");
    var checkBox = document.createElement("input");
    checkBox.id = id;
    checkBox.checked = checkedByDefault;
    checkBox.type = "checkbox";
    divNodeChild.appendChild(checkBox);

    var label = document.createElement("label");
    label.htmlFor = id;
    var labelTxt = document.createTextNode(txt);
    label.appendChild(labelTxt);
    divNodeChild.appendChild(label);
    
    divNode.appendChild(divNodeChild); 
};

function deleteMessages(){

    var requestDeleteMessages = "action=DeleteList";
    var messagesIds = getMessagesIds();
    for(var messageIdIndex = 0; messageIdIndex < messagesIds.length; messageIdIndex++){
        var messageId = messagesIds[messageIdIndex];
        requestDeleteMessages += "&value%5B%5D=" + messageId;
    }

    new Promise(function(resolve, reject) {
        resolve($.ajax({
            url: "http://fr01.okord.com/resource/class/view/ajax-message.php",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            data: requestDeleteMessages,
            type: "POST",
            error: function(error) { console.log(error);}
        }))})
    .then(function(){
        location.reload();
    });
};

function getMessagesIds(){
    
    var mixedHeaderSpans = document.getElementById("mixed_header").getElementsByTagName("span");

    var messageIds = [];
    for(var spanIndex = 0; spanIndex < mixedHeaderSpans.length; spanIndex++) {
        var hasToDelete = false;
        var span = mixedHeaderSpans[spanIndex];

        if(document.getElementById(spyId).checked){
            if(span.getAttribute("class") === "iconType spy"){
                hasToDelete = true;
            }
        }
        
        if(document.getElementById(battleId).checked){
            if(span.getAttribute("class") === "iconType battle"){
                hasToDelete = true;
            }
        }
        
        if(document.getElementById(transportId).checked){
            if(span.getAttribute("class") === "iconType transport"){
                hasToDelete = true;
            }
        }
        
        if(document.getElementById(feudalityId).checked){
            if(span.getAttribute("class") === "iconType feudality"){
                hasToDelete = true;
            }
        }
        
        if(document.getElementById(privateId).checked){
            if(span.getAttribute("class") === "iconType private"){
                hasToDelete = true;
            }
        }
        
        if(document.getElementById(sentId).checked){
            if(span.getAttribute("class") === "iconType sent"){
                hasToDelete = true;
            }
        }
        
        // Embuscade
        // Massacrer
        // Vassaux
        // ...

        if(hasToDelete){
            var messageId = mixedHeaderSpans[spanIndex - 1].getElementsByTagName("input")[0].getAttribute("value");
            messageIds.push(messageId);
        }
    }

    return messageIds;
};