WME MapRaider

WME MapRaid Helper

// ==UserScript==
// @name:hu             WME MapRaider
// @name:en             WME MapRaider
// @description:hu      Magyar MapRaid Segéd
// @description:en      WME MapRaid Helper
// @copyright           2014-2017, ragacs
// @name                WME MapRaider
// @description         WME MapRaid Helper
// @version             0.6
// @include             https://www.waze.com/editor*
// @include             https://www.waze.com/*/editor*
// @include             https://beta.waze.com/editor*
// @include             https://beta.waze.com/*/editor*
// @namespace https://greasyfork.org/users/6330
// ==/UserScript==

var wmemr_version = "0.6";
var wmemr_col_num = 20;
var wmemr_col_str = 6;
var wmemr_col_title = 7;

/* bootstrap, will call initialiseMapRaider() */
function bootstrapMapRaider()
{
    var bGreasemonkeyServiceDefined = false;

    try {
        bGreasemonkeyServiceDefined = (typeof Components.interfaces.gmIGreasemonkeyService === "object");
    }
    catch (err) { /* Ignore */ }

    if (typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
        unsafeWindow    = ( function () {
            var dummyElem = document.createElement('p');
            dummyElem.setAttribute('onclick', 'return window;');
            return dummyElem.onclick();
        }) ();
    }

    /* begin running the code! */
    setTimeout(initialiseMapRaider, 999);
}

/* helper function */
function getElementsByClassName(classname, node) {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for (var i=0,j=els.length; i<j; i++)
        if (re.test(els[i].className)) a.push(els[i]);
    return a;
}

function getId(node) {
    return document.getElementById(node);
}

function processNewPermalinks()
{
    var lines = getId("_taMapRaidInput").value.split("\n");
    var mapRaiderCounter = getId("_inMapRaidStart").value;
    if(lines.length == 1 && lines[0].search(/^CFG:\s*[A-Z]+,\s*[A-Z]+,\s*[A-Z]+$/i) == 0)
    {
        lines[0]=lines[0].toUpperCase();
        var cfg = lines[0].slice(lines[0].indexOf(":") + 1).split(",");
        cfg[0] = cfg[0].trim();
        cfg[1] = cfg[1].trim();
        cfg[2] = cfg[2].trim();
        wmemr_col_num = cfg[0].charCodeAt(0) - 65;
        if(cfg[0].length > 1) wmemr_col_num = (wmemr_col_num + 1) * 26 + cfg[0].charCodeAt(1) - 65;
        wmemr_col_str = cfg[1].charCodeAt(0) - 65;
        if(cfg[1].length > 1) wmemr_col_str = (wmemr_col_str + 1) * 26 + cfg[1].charCodeAt(1) - 65;
        wmemr_col_title = cfg[2].charCodeAt(0) - 65;
        if(cfg[2].length > 1) wmemr_col_title = (wmemr_col_title + 1) * 26 + cfg[2].charCodeAt(1) - 65;
        console.log("WME-MapRaider:" + wmemr_col_num);
        updateMapRaiderConfigStr();
        return;
    }
    if(lines.length < 2) return;

    for(var i=0; i<lines.length; i++)
    {
        var linestr = "";
        var titlestr = "";
        var lon = "", lat = "", zoom = "6", segs = "";
        var cells = lines[i].split("\t");
        if(cells.length < 1) continue;
        for(var c=0; c < cells.length; c++)
        {
            // https://www.waze.com/editor/?lon=19.154683728784565&lat=47.46686402709748&zoom=6&marker=true&segments=102441276
            if(cells[c].search(/https:\/\/(www|beta).waze.com\/.*editor/) == 0)
            {
                lon = cells[c].replace(/.*lon=/,"").replace(/&.*$/,"");
                lat = cells[c].replace(/.*lat=/,"").replace(/&.*$/,"");
                if(cells[c].search("zoom") > 0)
                    zoom = cells[c].replace(/.*zoom=/,"").replace(/&.*$/,"");
                if(cells[c].search("segments") > 0)
                    segs = cells[c].replace(/.*segments=/,"").replace(/&.*$/,"");
            }
            else if(c == wmemr_col_str) linestr = cells[c];
            else if(c == wmemr_col_title) titlestr = cells[c];
            else if(c == wmemr_col_num) mapRaiderCounter = cells[c];
        }
        if(lon.length == 0 || lat.length == 0)
            continue;
        if(linestr.length == 0) linestr = "Bookmark";
        else linestr = mapRaiderCounter++ + ". (*)" + linestr;
        var item = document.createElement('div');
        var redx = document.createElement('img');
        redx.setAttribute("src", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABE0lEQVQ4jaWTsWoCURRED0EkyBbBDwhBxEJCEAsLf1BEUlgE8QusxMIiRbASEbEQSWURUoQUKUREUgWZNLvZm+tbU2ThFo+dmX0zsxfcI6gIOoKVYB/Ps6ArqHq8JeYEbcGXQBlzFDwILkPk0Rmin6dfIoKWeXkQvAVIO8G7OfcTctlcey9oCq4FLwa8FdRjbCJ+FNz5r68EUSyciGwFdWN1bPA9BHN31akTqRnywGE3CD4Cfn9EzpAl+LzIaLUK3JhzEWhk9b9wqt5zwWVyYsGGuHPkQSCTVx9iydS4FlwFPE8FkSAveDQ11hIbbQNeCoaBwGauwv5/fuWJMvaho7+XqXdCdkIVwb3SdT4oXedbj/8G3YhVhG0/RlMAAAAASUVORK5CYII=");
        redx.onclick = function(event) {
            event = event || window.event;
            var adiv = this.parentNode;
            adiv.parentNode.removeChild(adiv);
            event.stopPropagation();
        };
        var crosshair = document.createElement('img');
        crosshair.setAttribute("src", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAA4mSURBVHhe7Z0HjG5FFcfX3g2IIGJhI2LB8lawYUNjiWDBqFGDBVQsgMinQY2K+ERFQTEoAooaK1gSeU8lJmJ5a4+isDQFRVkbKioP7DX6/318d9393vfNmbl37r0z385JTrbcKWfOOXfuzJlzzszNzSbspGHtKXyi8DDhCcIzhF8UXihcFl4h3DpCfr9ceIHwC8LThW8THiJ8vHBBeOvZZFX+o9pFQ9hXuFF4lvBHwr8K/xsZ/6L2LhF+RniU8NHC2+TPvvxGcB2RfF/hq4VbhNdEFnSI4jCDnC08cjRL5MfNjChG6G8WMoWHCKnLsueNZqINGfE1aVJ3FHUvEn4zYaFPUzBmp+cKt0+aw4kSdzfRxcLtNxkKflwhfqkxvEV4p0R5nRRZe4majwn/NgOCH1eEP2tM7xfeKymOJ0IM30wE/5+WBP8vtcuC7WfCfzj64BllKEudNtYP9IEi3D0R3vdKBlu4kwyhhAgBBbpM+Fkh+/i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LNCuHv2OFy6fF87pOcrG7DwQ4m/PjIPNIsTq2HAY3VT/H7gt6IV3zZMvAAAAAElFTkSuQmCC");
        crosshair.setAttribute("width", "16");
        crosshair.onclick = function(event) {
            event = event || window.event;
            var tmplatlon=new OpenLayers.LonLat(this.parentNode.getAttribute("lon"), this.parentNode.getAttribute("lat"));
            tmplatlon.transform(Waze.map.displayProjection, Waze.map.getProjectionObject());
            Waze.map.setCenter(tmplatlon, this.parentNode.getAttribute("zoom"));
            event.stopPropagation();
        };
        item.innerHTML = linestr;
        item.appendChild(redx);
        item.appendChild(crosshair);
        item.setAttribute("lon", lon);
        item.setAttribute("lat", lat);
        item.setAttribute("zoom", zoom);
        item.setAttribute("segs", segs);
        item.setAttribute("title", titlestr);
        item.onclick = function(event) {
            event = event || window.event;
            if (event.ctrlKey)
            {
                this.removeAttribute("style");
                return;
            }
            var tmplatlon=new OpenLayers.LonLat(this.getAttribute("lon"), this.getAttribute("lat"));
            tmplatlon.transform(Waze.map.displayProjection, Waze.map.getProjectionObject());
            Waze.map.setCenter(tmplatlon, this.getAttribute("zoom"));
            var segs = this.getAttribute("segs");
            try
            {
                if(segs.length)
                {
                    var segstrarray = segs.split(",");
                    var segarray = [];
                    var good = 0, bad = 0;
                    for(var s=0; s < segstrarray.length; s++)
                    {
                        var aseg = Waze.model.segments.get(segstrarray[s]);
                        if(aseg === undefined)
                            bad++;
                        else
                        {
                            good++;
                            segarray.push(aseg);
                        }
                    }
                    Waze.selectionManager.select(segarray);
                    if(bad)
                    {
                        if(good)
                            this.style.backgroundColor = "#dddd20";
                        else
                            this.style.backgroundColor = "#bb5020";
                    }
                    else
                        this.style.backgroundColor = "#50bb20";
                }
            }
            catch(err)
            {
                this.style.backgroundColor = "#bb5020";
            }
        };
        getId("_divMapRaidPermalinks").appendChild(item);
    }
    getId("_taMapRaidInput").value = "";
    getId("_inMapRaidStart").value = mapRaiderCounter;
}

function clearMapRaiderVisited()
{
    var listedlinks=getId("_divMapRaidPermalinks").childNodes;
    for(var l = 0; l < listedlinks.length; l++)
    {
        var color = listedlinks.item(l).style.backgroundColor;
        if( color )
        {
            getId("_divMapRaidPermalinks").removeChild(listedlinks.item(l--));
        }
    }
}

function clearMapRaiderAll()
{
    getId("_divMapRaidPermalinks").innerHTML = '';
}

function updateMapRaiderConfigStr()
{ 
    var line = "Cfg: ";
    if(wmemr_col_num > 25) line += String.fromCharCode(~~(wmemr_col_num / 26) + 64);
    line += String.fromCharCode(wmemr_col_num % 26 + 65) + ", ";
    if(wmemr_col_str > 25) line += String.fromCharCode(~~(wmemr_col_str / 26) + 64);
    line += String.fromCharCode(wmemr_col_str % 26 + 65) + ", ";
    if(wmemr_col_title > 25) line += String.fromCharCode(~~(wmemr_col_title / 26) + 64);
    line += String.fromCharCode(wmemr_col_title % 26 + 65);
    getId("_divMapRaidConfig").innerHTML = line;
}

/* =========================================================================== */

function initialiseMapRaider()
{
    // global variables
    betaMode = location.hostname.match(/beta.waze.com/);

    // add new box to left of the map
    var addon = document.createElement('section');
    addon.id = "mapraider-addon";

    if (navigator.userAgent.match(/Chrome/)) {
        addon.innerHTML  = '<b>'
            + 'WME MapRaider</b> &nbsp; v' + wmemr_version;
    } else {
        addon.innerHTML  = '<b>'
            + 'WME MapRaider</b> &nbsp; v' + wmemr_version;
    }

    section = document.createElement('p');
    section.style.padding = "8px 16px";
    //section.style.textIndent = "-16px";
    section.id = "nameMapRaider";
    section.innerHTML  = '<button id="_btnMapRaidClearVisited" title="Tip: Use Ctrl+Click on items to clear visited state">Clear Visited</button>'
        +  '<button id="_btnMapRaidClearAll">Clear All</button>'
        +  '<div><b>Next No.:</b> &nbsp; <input type="number" value="1" title="Starting number (if index column is not found)" id="_inMapRaidStart"/></div>'
        +  '<div><b>Table rows:</b> &nbsp; <textarea rows="500" cols="10" title="Copy here complete rows from the MapRaid table\n'
        +  'or use Cfg:<index column>,<name column>,<description column> to configure. Eg. Cfg: A, E, F" id="_taMapRaidInput"></textarea></div>'
        +  '<div id="_divMapRaidConfig" title="Index Column, Name Column, Description Column"></div>'
        +  '<div id="_divMapRaidPermalinks"></div>';
    addon.appendChild(section);

    var userTabs = getId('user-info');
    var navTabs = getElementsByClassName('nav-tabs', userTabs)[0];
    var tabContent = getElementsByClassName('tab-content', userTabs)[0];

    newtab = document.createElement('li');
    newtab.innerHTML = '<a href="#sidepanel-mapraider" data-toggle="tab">MapRaider</a>';
    navTabs.appendChild(newtab);

    addon.id = "sidepanel-mapraider";
    addon.className = "tab-pane";
    tabContent.appendChild(addon);
    updateMapRaiderConfigStr();    

    getId('_taMapRaidInput').oninput = processNewPermalinks;    
    getId('_btnMapRaidClearVisited').onclick = clearMapRaiderVisited;    
    getId('_btnMapRaidClearAll').onclick = clearMapRaiderAll;    
}

/* engage! =================================================================== */
bootstrapMapRaider();

/* end ======================================================================= */