EasyTrigger / CLUSTER-MATE

// ==UserScript==
// @id             iitc-plugin-cluster-mate
// @name           CLUSTER-MATE
// @category       Mods
// @author         @EasyTrigger
// @version        1.1
// @namespace      https://github.com/jonatkins/ingress-intel-total-conversion
// @updateURL      https://openuserjs.org/install/EasyTrigger/CLUSTER-MATE.user.js
// @downloadURL    https://openuserjs.org/install/EasyTrigger/CLUSTER-MATE.user.js
// @description    See plug-in info
// @include        https://www.ingress.com/intel*
// @include        http://www.ingress.com/intel*
// @match          https://www.ingress.com/intel*
// @match          http://www.ingress.com/intel*
// @include        https://www.ingress.com/mission/*
// @include        http://www.ingress.com/mission/*
// @match          https://www.ingress.com/mission/*
// @match          http://www.ingress.com/mission/*
// @grant          none
// ==/UserScript==


function wrapper(plugin_info) {
    if(typeof window.plugin !== 'function') window.plugin = function() {};

    // PLUG-IN ////////////////////////////////////////////////////////////////////////////////////////////////
    //  INFO  /////////////////////////////////////////////////////////////////////////////////////////////////
    //                                                                                                       //
    //  CLUSTER-MATE is a plugin born to monitor an anomaly site, it hides useless iitc tabs in a view-only  //
    //  mode, prevents iitc to go idle, refreshes the browser periodically at user-defined time intervals    //
    //  (not during measurements windows) to keep it fast and automatically cycles the map view between      //
    //  clusters and site view during measurements displaying the remaining times.                           //
    //                                                                                                       //
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////

    plugin.clustermateoff = function() {
        var parent = $(".leaflet-top.leaflet-left", window.map.getContainer());
        var button = document.createElement("a");
        button.className = "leaflet-bar-part";
        button.addEventListener("click", plugin.clustermateoff.onBtnClick, false);
        button.title = 'Toggle CLUSTER-MATE';
        var tooltip = document.createElement("div");
        tooltip.className = "leaflet-control-cmoff-tooltip";
        button.appendChild(tooltip);
        var container = document.createElement("div");
        container.className = "leaflet-control-cmoff leaflet-bar leaflet-control";
        container.appendChild(button);
        parent.append(container);
        plugin.clustermateoff.button = button;
        plugin.clustermateoff.tooltip = tooltip;
        plugin.clustermateoff.container = container;
        $('.leaflet-control-cmoff > a').attr('style', "background-image: url(https://cdn0.iconfinder.com/data/icons/android-icons/512/gps-01-64.png); background-size: cover;");};
    plugin.clustermateoff.onBtnClick = function() {
        var btn = plugin.clustermateoff.button;
        var tooltip = plugin.clustermateoff.tooltip;
        var layer = plugin.clustermateoff.layer;
        if(btn.classList.contains("active")) {override = 0;
                                              window.plugin.cleanCast();
                                              window.plugin.autoCenter();
                                              $('a.leaflet-bar-part.active').remove();
                                              plugin.clustermateoff();
                                              btn.classList.remove("active");} else {override = 1;
                                                                                     window.plugin.cleanCast.off();
                                                                                     $('a.leaflet-bar-part.active').remove();
                                                                                     btn.classList.add("active");}};
    plugin.localstorages = function() {
        if ( localStorage.siteurl === undefined ) { siteurl = zerozero; localStorage.siteurl = siteurl;} else { siteurl = localStorage.siteurl; }
        if ( localStorage.cluster1 === undefined ) { cluster1 = zerozero; localStorage.cluster1 = cluster1;} else { cluster1 = localStorage.cluster1; }
        if ( localStorage.cluster2 === undefined ) { cluster2 = zerozero; localStorage.cluster2 = cluster1;} else { cluster2 = localStorage.cluster2; }
        if ( localStorage.cluster3 === undefined ) { cluster3 = zerozero; localStorage.cluster3 = cluster1;} else { cluster3 = localStorage.cluster3; }
        if ( localStorage.cluster4 === undefined ) { cluster4 = zerozero; localStorage.cluster4 = cluster1;} else { cluster4 = localStorage.cluster4; }
        if ( localStorage.measurement1 === undefined ) { measurement1 = 14; localStorage.measurement1 = measurement1; } else { measurement1 = localStorage.measurement1; }
        if ( localStorage.measurement2 === undefined ) { measurement2 = 15; localStorage.measurement2 = measurement2; } else { measurement2 = localStorage.measurement2; }
        if ( localStorage.measurement3 === undefined ) { measurement3 = 16; localStorage.measurement3 = measurement3; } else { measurement3 = localStorage.measurement3; }
        if ( localStorage.measurement4 === undefined ) { measurement4 = 17; localStorage.measurement4 = measurement4; } else { measurement4 = localStorage.measurement4; }
        if ( localStorage.clusterview_stop === undefined ) { clusterview_stop = 10; localStorage.clusterview_stop = clusterview_stop;} else { clusterview_stop = localStorage.clusterview_stop; }
        if ( localStorage.reload === undefined ) { reload = 5; localStorage.reload = reload;} else { reload = localStorage.reload; }};
    plugin.clustermatetoolbox = function() {
        tbsettings = function() {window.plugin.options();};
        $('#toolbox').append(" <a id='clustermate' onclick='tbsettings();' accesskey='' title='ClusterMate Settings' style='display: inline-block;'>ClusterMate</a>");};
    button = function() {};
    button.siteurl = function() {
        siteurl = prompt('Insert ANOMALY SITE URL:'); localStorage.siteurl = siteurl;
        if (localStorage.siteurl !== 'null') { alert('ANOMALY SITE URL SAVED:\n' + localStorage.siteurl);} else { alert('ANOMALY SITE URL NOT SAVED!'); siteurl = zerozero;} localStorage.siteurl = siteurl; };
    button.cluster1 = function() {
        cluster1 = prompt('Insert Cluster1 URL:'); localStorage.cluster1 = cluster1;
        if (localStorage.cluster1 !== 'null') { alert('CLUSTER 1 URL SAVED:\n' + localStorage.cluster1);} else { alert('CLUSTER 1 URL NOT SAVED!'); cluster1 = zerozero;} localStorage.cluster1 = cluster1; };
    button.cluster2 = function() {
        cluster2 = prompt('Insert Cluster2 URL:'); localStorage.cluster2 = cluster2;
        if (localStorage.cluster2 !== 'null') { alert('CLUSTER 2 URL SAVED:\n' + localStorage.cluster2);} else { alert('CLUSTER 2 URL NOT SAVED!'); cluster2 = zerozero;} localStorage.cluster2 = cluster2; };
    button.cluster3 = function() {
        cluster3 = prompt('Insert Cluster3 URL:'); localStorage.cluster3 = cluster3;
        if (localStorage.cluster3 !== 'null') { alert('CLUSTER 3 URL SAVED:\n' + localStorage.cluster3);} else { alert('CLUSTER 3 URL NOT SAVED!'); cluster3 = zerozero;} localStorage.cluster3 = cluster3; };
    button.cluster4 = function() {
        cluster4 = prompt('Insert Cluster4 URL:'); localStorage.cluster4 = cluster4;
        if (localStorage.cluster4 !== 'null') { alert('CLUSTER 4 URL SAVED:\n' + localStorage.cluster4);} else { alert('CLUSTER 4 URL NOT SAVED!'); cluster4 = zerozero;} localStorage.cluster4 = cluster4; };
    button.measurement1 = function() {
        measurement1 = prompt('Insert First Measurement Starting Hour:\n(HOURS ONLY! Es: 14:00 -> 14)');
        localStorage.measurement1 = parseInt(measurement1);
        measurement2 = parseInt(measurement1) + 1;
        measurement3 = parseInt(measurement2) + 1;
        measurement4 = parseInt(measurement3) + 1;
        localStorage.measurement2 = parseInt(measurement2);
        localStorage.measurement3 = parseInt(measurement3);
        localStorage.measurement4 = parseInt(measurement4);
        if (localStorage.measurement1 !== 'null') { alert('MEASUREMENT TIMES SAVED:\nNEW MEASUREMENT TIMESPANS:\n' + localStorage.measurement1 + ':' + clusterview_stop + '\n' + localStorage.measurement2 + ':' + clusterview_stop + '\n' + localStorage.measurement3 + ':' + clusterview_stop + '\n' + localStorage.measurement4 + ':' + clusterview_stop );} else { alert('MEASUREMENT TIMES NOT SAVED!');} };
    button.autorefresh = function() {
        reload = prompt('SET AUTOREFRESH TIMEOUT (MINUTES):'); localStorage.reload = parseInt(reload);
        if (localStorage.reload !== 'null') { alert('AUTOREFRESH TIMEOUT SAVED:\n' + localStorage.reload + ' Minutes');} else { alert('AUTOREFRESH TIMEOUT NOT SAVED!');} };
    button.clusterview_stop = function() {
        clusterview_stop = prompt('SET MEASUREMENT ENDING MINUTE (MINUTE ONLY):'); localStorage.clusterview_stop = clusterview_stop;
        if (localStorage.clusterview_stop !== 'null') { alert('ENDING MINUTE SAVED: ' + '00:' + localStorage.clusterview_stop +'\n\nNEW MEASUREMENT TIMESPANS:\n' + localStorage.measurement1 + ':' + clusterview_stop + ' - ' + localStorage.measurement2 + ':' + clusterview_stop + ' - ' + localStorage.measurement3 + ':' + clusterview_stop + ' - ' + localStorage.measurement4 + ':' + clusterview_stop );} else { alert('ENDING MINUTE NOT SAVED!');} };
    window.plugin.options = function() {
        $('head').append('<style>' + '.clustermatebox > a { display:block; color:#ffce00; border:1px solid #ffce00; padding:3px 0; margin:10px auto; width:80%; text-align:center; background:rgba(8,48,78,.9); }' + '</style>');
        var html = '<div class="clustermatebox">' +
            '<a onclick="window.plugin.options.clusters();return false;" tabindex="0">- - - CLUSTER SETTINGS - - -</a>' +
            '<a onclick="window.plugin.options.timings();return false;" tabindex="0">- - - TIMING SETTINGS - - -</a>' +
            '</div>';
        dialog({html: html,
                id: 'plugin-clustermate-options',
                dialogClass: 'ui-dialog-clustermate',
                title: 'ClusterMate Options'});};
    window.plugin.options.timings = function() {
        var html = '<div class="clustermatebox">' +
            '<a onclick="button.measurement1();return false;" tabindex="0">MEASURES TIMINGS</a>' +
            '<a onclick="button.clusterview_stop();return false;" tabindex="0">MEASUREMENT TIMESPAN</a>' +
            '<a onclick="button.autorefresh();return false;" tabindex="0">AUTOREFRESH SETTINGS</a>' +
            '</div>';
        dialog({html: html,
                id: 'plugin-clustermate-timing',
                dialogClass: 'ui-dialog-clustermate',
                title: 'Timing Settings'});};
    window.plugin.options.clusters = function() {
        var html = '<div class="clustermatebox">' +
            '<a onclick="button.siteurl();return false;" tabindex="0">ANOMALY SITE</a>' +
            '<a onclick="button.cluster1();return false;" tabindex="0">CLUSTER 1</a>' +
            '<a onclick="button.cluster2();return false;" tabindex="0">CLUSTER 2</a>' +
            '<a onclick="button.cluster3();return false;" tabindex="0">CLUSTER 3</a>' +
            '<a onclick="button.cluster4();return false;" tabindex="0">CLUSTER 4</a>' +
            '</div>';
        dialog({html: html,
                id: 'plugin-clustermate-clusters',
                dialogClass: 'ui-dialog-clustermate',
                title: 'Cluster Settings'});};
    window.plugin.clusterCycle = function() {
        var clusterurl;
        var my_hour = new Date().getHours();
        switch (my_hour) {
            case parseInt(measurement1): clusterurl = cluster1; cluster = 'CLUSTER 1'; testcc1 = 'OK'; break;
            case parseInt(measurement2): clusterurl = cluster2; cluster = 'CLUSTER 2'; testcc2 = 'OK'; break;
            case parseInt(measurement3): clusterurl = cluster3; cluster = 'CLUSTER 3'; testcc3 = 'OK'; break;
            case parseInt(measurement4): clusterurl = cluster4; cluster = 'CLUSTER 4'; testcc4 = 'OK'; break;
            default: clusterurl = siteurl; cluster = 'ANOMALY SITE'; testcc5 = 'OK'; break;}
        if (new Date().getMinutes() > clusterview_stop) {cluster = 'ANOMALY SITE';}
        var my_min = new Date().getMinutes();
        if (override === 0) {
            if (my_min >= 0 && my_min < clusterview_stop) {
                if (window.location.href != clusterurl) {window.location.assign(clusterurl);}
            }else { if (window.location.href != siteurl) {window.location.assign(siteurl);}}}
        idleReset();};
    window.plugin.autoReload = function(elm) {
        var c = map.getCenter();
        var lat = Math.round(c.lat*1E6)/1E6;
        var lng = Math.round(c.lng*1E6)/1E6;
        var qry = 'll='+lat+','+lng+'&z=' + map.getZoom();
        var currenturl = ('https://www.ingress.com/intel?' + qry);
        var minrel = parseInt(new Date().getMinutes());
        if (override === 0) {
            if (minrel > clusterview_stop && minrel <=59) {window.location.assign(currenturl);}}};
    window.plugin.autoCenter = function(elm) {
        var c = map.getCenter();
        var lat = Math.round(c.lat*1E6)/1E6;
        var lng = Math.round(c.lng*1E6)/1E6;
        var qry = 'll='+lat+','+lng+'&z=' + map.getZoom();
        var currenturl = ('https://www.ingress.com/intel?' + qry);
        var my_hour = parseInt(new Date().getHours());
        switch (my_hour) {
            case parseInt(measurement1): clusterurl = cluster1; testac1 = 'OK'; break;
            case parseInt(measurement2): clusterurl = cluster2; testac2 = 'OK'; break;
            case parseInt(measurement3): clusterurl = cluster3; testac3 = 'OK'; break;
            case parseInt(measurement4): clusterurl = cluster4; testac4 = 'OK'; break;
            default: clusterurl = siteurl; testac5 = 'OK'; break;}
        if (currenturl !== clusterurl) {window.location.assign(clusterurl);}};
    window.plugin.clusterSec = function() {
        var my_min = parseInt(new Date().getMinutes());
        var my_hour = parseInt(new Date().getHours());
        var actualsec = parseInt(new Date().getHours())*3600 + parseInt(new Date().getMinutes())*60 + parseInt(new Date().getSeconds());
        if (actualsec < measurement1 * 3600) {counter = measurement1 * 3600 - actualsec; nextcluster = 'CLUSTER 1'; view = 0; testcs1 = 'OK';}
        else { if (my_min >= parseInt(clusterview_stop)) {if (actualsec > measurement1 * 3600 && actualsec < measurement2 * 3600) {counter = measurement2 * 3600 - actualsec; nextcluster = 'CLUSTER 2'; view = 0; testcs3 = 'OK';}
                                                          else if (actualsec > measurement2 * 3600 && actualsec < measurement3 * 3600) {counter = measurement3 * 3600 - actualsec; nextcluster = 'CLUSTER 3'; view = 0; testcs4 = 'OK';}
                                                          else if (actualsec > measurement3 * 3600 && actualsec < measurement4 * 3600) {counter = measurement4 * 3600 - actualsec; nextcluster = 'CLUSTER 4'; view = 0; testcs5 = 'OK';}
                                                          else if (actualsec > measurement4 * 3600) {counter = 24 * 3600 - actualsec + measurement1 * 3600; nextcluster = 'CLUSTER 1'; view = 0; testcs2 = 'OK';}}
              else {if (my_hour === parseInt(measurement1)) {counter = parseInt(measurement1) * 3600 + parseInt(clusterview_stop) * 60 - parseInt(actualsec); view = 1; testcs6 = 'OK';}
                    else if (my_hour === parseInt(measurement2)) {counter = parseInt(measurement2) * 3600 + parseInt(clusterview_stop) * 60 - parseInt(actualsec); view = 1; testcs7 = 'OK';}
                    else if (my_hour === parseInt(measurement3)) {counter = parseInt(measurement3) * 3600 + parseInt(clusterview_stop) * 60 - parseInt(actualsec); view = 1; testcs8 = 'OK';}
                    else if (my_hour === parseInt(measurement4)) {counter = parseInt(measurement4) * 3600 + parseInt(clusterview_stop) * 60 - parseInt(actualsec); view = 1; testcs9 = 'OK';}
                    else if (actualsec > measurement4 * 3600) {counter = 24 * 3600 - actualsec + measurement1 * 3600; nextcluster = 'CLUSTER 1'; view = 0; testcs2 = 'OK';}}}};
    window.plugin.countDown = function() {
        var thh = Math.floor(counter / 3600);
        var tmm = Math.floor((counter - (thh * 3600)) /60);
        var tss = Math.floor((counter - (thh * 3600) - (tmm * 60)));
        var sh; var sm; var ss;
        if (thh !== 1) {sh = 's';} else {sh = '';}
        if (tmm !== 1) {sm = 's';} else {sm = '';}
        if (tss !== 1) {ss = 's';} else {ss = '';}
        if (override === 0) {if (view === 0) {$('#chatinput tr').text(cluster + ' -  Time before next view (' + nextcluster + '):  ' + thh + ' Hour'+sh + ' ' + tmm + ' Minute'+sm + ' ' + tss + ' Second'+ss); counter = counter-1;}
                             else {$('#chatinput tr').text(cluster + ' -  MEASUREMENT TIME LEFT:  ' + tmm + ' Minute'+sm + ' ' + tss + ' Second'+ss); counter = counter-1;}
                             if (counter >= 30) {$('#chatinput tr').attr('style', 'color: rgb(255, 206, 0) !important; text-align: center !important;');} else {$('#chatinput tr').attr('style', 'color: rgb(255, 125, 0) !important; text-align: center !important;');}} else { $('#chatinput tr').text('- - - CLUSTERMATE PAUSED - - -'); $('#chatinput tr').attr('style', 'color: rgb(255, 206, 0) !important; text-align: center !important;'); counter = counter-1;}};
    plugin.clustermatetoolbox = function() {
        tbsettings = function() {window.plugin.options();};
        $('#toolbox').append(" <a id='clustermate' onclick='tbsettings();' accesskey='' title='ClusterMate Settings' style='display: inline-block;'>ClusterMate</a>");};
    window.plugin.cleanCast = function() {
        $('#scrollwrapper').hide();
        $('#sidebartoggle').hide();
        $('#chatcontrols').hide();
        $('#chat').hide();
        $('#chatinput td').hide();
        $('#portal_highlight_select').hide();
        $('#bkmrksTrigger').hide();
        $('#bookmarksBox').hide();
        $('a').hide(".leaflet-control-layers-toggle");
        $('a').hide(".leaflet-control-zoom-in");
        $('a').hide(".leaflet-control-zoom-out");
        $('.leaflet-control-attribution.leaflet-control').hide();
        $('.leaflet-bottom leaflet-left').hide();};
    window.plugin.cleanCast.off = function() {
        $('#scrollwrapper').show();
        $('#sidebartoggle').show();
        $('#chatcontrols').show();
        $('#chat').show();
        $('#chatinput td').show();
        $('#portal_highlight_select').show();
        $('#bkmrksTrigger').show();
        $('#bookmarksBox').show();
        $('a').show(".leaflet-control-layers-toggle");
        $('a').show(".leaflet-control-zoom-in");
        $('a').show(".leaflet-control-zoom-out");
        $('.leaflet-control-attribution.leaflet-control').show();
        $('.leaflet-bottom leaflet-left').show();};
    var cluster,
        nextcluster,
        counter,
        view,
        siteurl = localStorage.siteurl,
        cluster1 = localStorage.cluster1,
        cluster2 = localStorage.cluster2,
        cluster3 = localStorage.cluster3,
        cluster4 = localStorage.cluster4,
        zerozero = 'https://www.ingress.com/intel?ll=0,0&z=10',
        measurement1 = parseInt(localStorage.measurement1),
        measurement2 = parseInt(localStorage.measurement2),
        measurement3 = parseInt(localStorage.measurement3),
        measurement4 = parseInt(localStorage.measurement4),
        reload = parseInt(localStorage.reload),
        clusterview_stop = parseInt(localStorage.clusterview_stop),
        override = 0;
    var clustermate =  function() {
        window.plugin.cleanCast();
        plugin.clustermatetoolbox();
        plugin.clustermateoff();
        setInterval ( plugin.localstorages, 1000);
        setInterval ( window.plugin.clusterSec, 1000);
        setInterval ( window.plugin.countDown, 1000);
        setInterval ( window.plugin.clusterCycle, 1000 );
        setInterval ( window.plugin.autoReload, reload+60*1000 );};
    clustermate.info = plugin_info;
    if(!window.bootPlugins) window.bootPlugins = [];
    window.bootPlugins.push(clustermate);
    if(window.iitcLoaded && typeof clustermate === 'function') clustermate();}
var script = document.createElement('script');
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) info.script = { version: GM_info.script.version, name: GM_info.script.name, description: GM_info.script.description };
script.appendChild(document.createTextNode('('+ wrapper +')('+JSON.stringify(info)+');'));
(document.body || document.head || document.documentElement).appendChild(script);