malygos / COMMON

// ==UserScript==
// @name         COMMON
// @include      *
// @exclude      *dummy.html*
// @exclude      */tab/*
// @exclude      https://onedrive.live.com*
// @exclude      *inbox.google.com*
// @require      http://code.jquery.com/jquery-latest.js

// @run-at       document-start
// @grant        GM_addStyle
// ==/UserScript==


GM_addStyle('.btnTopTamper { margin-right:1px; border:none; background: rgba(250, 250, 250, 0.85);  font-family: Arial; font-weight: bold; color: #4f4f4f;  font-size: 10px;  padding: 2px 6px 2px 6px;  text-decoration: none;}.btnTopTamper:hover {  background: #f5f5f5;  text-decoration: none;}');


//I AM ON (url)
unsafeWindow.iamon = function(_urlmatch){
    var iamon_url = window.location.href.toLowerCase().indexOf(_urlmatch.toLowerCase()) != -1;
    if(iamon_url){
        console.log('You are on '+_urlmatch);
    }
    return (iamon_url);
}

//function for adding button to topbar (creates it if doesn't exist)
unsafeWindow.addToTopBar = function(_ID, _name){
    if($('#topBarTamper').length == 0){
        htmlTopBar='<div style="display:inline; position: fixed; left:31%; top:0px ; z-index:99999; text-align:center;" id="topBarTamper"></div>';
        $("body").append(htmlTopBar);
    }
    newButton ='<button class="btnTopTamper" id="'+_ID+'">'+_name+'</button>';
    
    $('#topBarTamper').append(newButton);
}

unsafeWindow.injectColor= function(_color){
    if(document.getElementById("colorInjected")==null){
        var colorInject = document.createElement("div"); 
        colorInject.className = "colorInjectCSS";
        colorInject.id = "colorInjected";
        (document.documentElement).appendChild(colorInject);
        GM_addStyle(".colorInjectCSS { position: absolute; top: 0px; left: 0px; z-index: 999999; width: 2px; height: 4px;}");
    }
    GM_addStyle(".colorInjectCSS { background-color: "+_color+"; }");
}

unsafeWindow.doWhen = function(_myFunction, _myTrigger){
    if(typeof _myTrigger != 'function' || typeof _myFunction != 'function' ){
        console.log('Bad use of doWhen ('+_myTrigger.toString()+') Please use functions' );
        return
    }
    console.log('Searching Trigger...' );
    myTimer = setInterval(function(){
        if(_myTrigger()!=null && _myTrigger()!="" && _myTrigger()!=undefined && _myTrigger().length!=0){
            clearInterval(myTimer);
            console.log("Found Trigger :"+_myTrigger);
            console.log("Starting... "+_myFunction);
            _myFunction()
            return true
        }
    }, 200);
}
unsafeWindow.doEachTime = function(_myFunction, _myTrigger){
    //not implemented
}
unsafeWindow.stayLoggedIn = function(){
    setInterval(function(){
        $.get(window.location);
        injectColor('green');
        console.log('Stay Logged Call');
    }, 60000);
}


unsafeWindow._remove = function(_element, _occ) {
    _act_on_found(_element,'remove', _occ)
}
unsafeWindow._click = function(_element, _occ) {
    _act_on_found(_element,'click', _occ)
}
unsafeWindow._act_on_found = function(_element,_action, _occ) {
    if (typeof(_occ)==='undefined') _occ = 1;
    myTimer = setInterval(function(){
        if($(_element).length){
            console.log(_action+' : '+_element)
            
            if(_action =='remove') $(_element).remove();
            if(_action =='click') $(_element).click();
            
            
            _occ--;
            if (_occ < 1) clearInterval(myTimer);
        }
    }, 200);
}