Me / redIgnore By Me

// ==UserScript==
// @name         redIgnore By Me
// @namespace    Me
// @version      1.821
// @updateURL    https://openuserjs.org/meta/Me/redIgnore_By_Me.meta.js
// @downloadURL  https://openuserjs.org/install/Me/redIgnore_By_Me.user.js
// @description  This script will ignore CHICKENS who tries to make your military advisor background color in red color
// @author       Me
// @include      *://*.ikariam.gameforge.com/*
// @exclude      *://s*.ikariam.gameforge.com/*&ajax=1
// @exclude      *://s*.ikariam.gameforge.com/pillory.php*
// @grant        none
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @run-at		 document-start
// @license      MIT
// ==/UserScript==


//prevent conflicts with Ikariam jQuery version
this.jQuery = window.jQuery;
window.jQuery.noConflict(true);
this.$ = this.jQuery;

Object.defineProperty(this, 'warning', {get: function(){ 
    return localStorage.getItem('warning') == 'true';
}, set: function(val){ 
    localStorage.setItem('warning', val);
}});
changeColor(warning);
$(document).ready(function(){ 
    changeColor(warning);
    refreshAttacks();
    var source = window.ajaxHandlerCall;
    ajaxHandlerCall = function(){ 
        refreshAttacks();
        source.apply(this, arguments);
    };
    window.console.log = function(a){ 
        if(a == "==========================> ikariam.Controller: callback ajax success end"){ 
           changeColor(warning);
           addMeUI();
        }
    };
});

function addMeUI(){ 
    addMeIgnoreButtons();
}
function addMeIgnoreButtons(){ 
    getMeButtons().remove();
    var hostileRows = getHostileRows();
    hostileRows.each(function(){ 
        var id = this.id;
        var type = checkIfIgnore(id)?'UnIgnore':'Ignore';
        $(this).find('.actions').append($('<a/>', {id:(id + '_' + type), class:'button MeButton'}).html(type).click(function(){ 
            clicked(id);
            addMeUI();
            changeColor($('a[id$=_Ignore]').length!=0);
        }))
      });
}
function getHostileRows(){ 
    return getRows().filter('.hostile');
}
function getRows(){ 
    return $('tr[id^=js_MilitaryMovementsEventRow]', $('.military_event_table'));
}
function getMeButtons(){ 
    return $('.MeButton');
}
function clicked(id){ 
    if(checkIfIgnore(id))
        unIgnoreMovement(id);
    else
        ignoreMovement(id);
}
function refreshAttacks(){ 
    $.ajax({url:'index.php?view=militaryAdvisor&ajax=1', success: function(data){ 
        warning = checkThreat(JSON.parse(data)[2][1]);
        changeColor(warning);
    }});
}
function checkThreat(armyViewJson){ 
    for(var i in armyViewJson)
        if(armyViewJson[i]['class'] != undefined)
            if((armyViewJson[i]['class'].indexOf('hostile') != -1 && i.indexOf('Combat') == -1) && checkIfIgnore(i) == false)
                return true;
    return false;
}
function changeColor(isWarning){ 
    var militaryAdvisor = $('#js_GlobalMenu_military')[0];
    var advType = accountType();
    try{ 
        if(isWarning){ 
            if(militaryAdvisor.className.indexOf('alert') == -1)
                militaryAdvisor.className = militaryAdvisor.className.replace(advType, advType + 'alert');
        }
        else
            militaryAdvisor.className = militaryAdvisor.className.replace('alert', '');
           } catch(e){setTimeout(function(){changeColor(isWarning)})}
}
function ignoreMovement(movementId){ 
    localStorage.setItem(movementId, "ignore");
}
function unIgnoreMovement(movementId){ 
    localStorage.removeItem(movementId);
}
function checkIfIgnore(movementId){ 
    return localStorage.getItem(movementId) != null;
}
function accountType(){ 
    return (isPremium()?'premium':'normal');
}
function isPremium(){ 
    return !(($('#advisors').find('.premium').length==0)&&($('#advisors').find('.premiumactive').length==0));
}
function isRtl(){ 
    return $('link[href^="/skin/compiled-rtl"]').length!=0;
}
function toDOM (innerHTML){ 
    var fo = document.createElement('div');
    fo.innerHTML = innerHTML;
    return fo.firstChild;
}