WME PA GIS Map

Open a PA GIS map in another window, at the same location as the WME map. Keeps the location of the GIS map synced to WME.

// ==UserScript==
// @name         WME PA GIS Map
// @namespace    https://greasyfork.org/users/45389
// @version      0.1
// @description  Open a PA GIS map in another window, at the same location as the WME map.  Keeps the location of the GIS map synced to WME.
// @author       MapOMatic
// @include     /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
// @include     /^https?:\/\/www\.arcgis\.com\/home\/webmap\/viewer\.html\?webmap=8f0eff71cd5d487694ded927009c6d6d.*/
// @license      GNU GPLv3
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAARuklEQVR42u3d/28b933H8dcdTwxLUQLtyLLs2XHmNHJTebaAFY2RZBiQBFvdtRuGeRsWbMWGrcWwYb/vv9iP+20YsB+6/TAMGBCkRdoZyewgDgTHsWOn3mQvTdJUSmxHcRSRpqnjfrjPR/rooyP1jUfxqOcDOEgipSMp3vv1+cL7IgEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADLuRfAGxfkNOiD70AiM0i5yuAAQoAW/iRWdzWP5bU9L4C2ECUw8IvSio5IWCLvi6pYf6GEAA2oZCD4o8kDUn6iqRhSaOSHpX0VUkT5ncKpjcTS2pJWiYAgPz2ANxWv2iWilkek/THkk6aVv9lSW86r8UdBhACQI4CwO/ulySVzTI+NDR0dmRk5Hu1Wm2iVqvZYi9L+sR83zChUOetBfITAH6Lb4u/Imm0UCic2rdv3/dPnDhxampqqnzz5k29/vrrarVaoaTHJT0paU7Solnc+QFs/z3Z7ccIM15/7H2/53qNUR9sYGmFb1v9x4eHh//k6NGj33r66aer09PTqlarunv3roIgUKvVkgmJcfN3drgQ9mDj6fciDHe5CHf6+GHGzztOKfzmXh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CsSEQmr/7H/P77nNv5ikAmk435o6kH5qewGMmWd0ZTnlvXiOlKLa7kcSbLIp2RdLQ1lqp+gaP7y8Ptljs2wm1eAdhGHchUOMdrncrQ8524eP2DOx2ueR0//0A8O+338+b7fh5JYfwhh2Kf8a08POmB7LgFLVMyNhTgi04PYz3JV1IGZ5kMgQIMhpWFE1K7jfLmDZ3jHOcQRhtp1vZrSKIu/jcsgqBuMtFv1EQxBm9v3Gbpen1SBtOT8DvIcgZBhTN9lpxFnus/qiS/fWfUbIPQeQ8h3umx3vRFPaCaQTvOUMA2wOwR/3ZxX4qsWj+bs78rRsecbeLNcshwKJT6HXzAjdzYEOWG8l2AyHuclHEXSy6brXsccbrjDN8z9v1APxeQNoSpzzPtGGD23s4L+k9SSckfc1s13Om+/5zp/gXnB7Aolln6PQE7OMsmXXEzvzEfWd+IpOJwCx6AG4vwO7KWNbmd2vsVZdxuz2CnU5ixV0sxm7OdXRzvVkEz2afT7xBjyD2ij7usA27vYGS0yOwvQB7sE7Z2Z4bzuSjGwLuXIP9tKHirLPkBIC7jkUngHITAKHzIt2DGfyxf9jjYuhG0XerGLJu/Xfr7+KMnst2gr3THMFWt+PIa9AqTgGXnN50w2vBF53uu23J/XXagAmdXkHaMCU3AeD+80Kl79IY7vJGkoe/zfL/sdu9jjw8j3YNWimld+sWbz1lItEfaoReg+ie+sufEM/sMuJBDzaqtIIPc1IQu/V4u4mDtzpvy25Ptqj15+2Lvda7qc5X+wlTGsdYPbp8eMB7Cmy7Z+sGgr3PP7//VlrvsNchTAAA3QmDtPkHelPAHgsEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2gP8HT7fJq7UJgi0AAAAASUVORK5CYII=
// ==/UserScript==

(function() {
    'use strict';

    var debugLevel = 0;
    var mapWindow;
    var Extent;
    var SpatialReference;
    var receiverAdded = false;

    function log(message, level) {
        if (message && level <= debugLevel) {
            console.log('PA GIS:', message);
        }
    }

    function onButtonClick() {
        var wazeExt = W.map.getExtent();
        var url = 'http://www.arcgis.com/home/webmap/viewer.html?webmap=8f0eff71cd5d487694ded927009c6d6d&extent=';
        url += wazeExt.left + '%2C' + wazeExt.bottom + '%2C' + wazeExt.right + '%2C' + wazeExt.top + '%2C102113';
        if (!mapWindow || mapWindow.closed) {
            mapWindow = window.open(null, 'wv_gis_map');
            try {
                if (mapWindow.location && mapWindow.location.href) {
                    mapWindow.location.assign(url);
                }
            } catch (ex) {
                if (ex.code === 18) {
                    // Ignore if accessing location.href is blocked by cross-domain.
                } else {
                    throw ex;
                }
            }
        }
        mapWindow.focus();
        syncGISMapExtent(mapWindow);
    }

    function syncGISMapExtent(myMapWindow) {
      if (myMapWindow && !myMapWindow.closed) {
            var wazeExt = W.map.getExtent();
            try {
                myMapWindow.postMessage({type:'setExtent', xmin:wazeExt.left, xmax:wazeExt.right, ymin:wazeExt.bottom, ymax:wazeExt.top, spatialReference: 102113}, 'http://www.arcgis.com');
            } catch (ex) {
                log(ex, 0);
            }
            try {
                myMapWindow.postMessage({type:'setExtent', xmin:wazeExt.left, xmax:wazeExt.right, ymin:wazeExt.bottom, ymax:wazeExt.top, spatialReference: 102113}, 'https://www.arcgis.com');
            } catch (ex) {
                log(ex, 0);
            }
        }
    }

    function init() {
        $('.WazeControlPermalink').prepend(
            $('<div>').css({float:'left',display:'inline-block', padding:'0px 5px 0px 3px'}).append(
                $('<a>',{id:'wv-gis-button',title:'Open the PA GIS map in a new window', href:'javascript:void(0)'})
                .text('PA-GIS')
                .css({float:'left',textDecoration:'none', color:'#000000', fontWeight:'bold'})
                .click(onButtonClick)
            )
        );

        setInterval(function() {
            var $btn = $('#wv-gis-button');
            if ($btn.length > 0) {
                $btn.css('color', (mapWindow && !mapWindow.closed) ? '#1e9d12' : '#000000');
            }
        }, 500);

        /* Event listeners */
        W.map.events.register('moveend',null, function(){syncGISMapExtent(mapWindow);});

        log('Initialized.', 1);
    }

    function receiveMessageGIS(event) {
        log(event, 1);
        var data = event.data;
        if (!Extent) {
            Extent = unsafeWindow.require('esri/geometry/Extent');
            SpatialReference = unsafeWindow.require('esri/SpatialReference');
        }
        switch (data.type) {
            case 'setExtent':
        }
        var map = unsafeWindow.arcgisonline.map.main.map;
        var ext = new Extent({xmin:data.xmin, xmax:data.xmax, ymin:data.ymin, ymax:data.ymax, spatialReference:new SpatialReference({wkid:data.spatialReference})});
        unsafeWindow.arcgisonline.map.main.map.setExtent(ext);
    }

    function receiveMessageWME(event) {
        // TBD
    }

    function bootstrap() {
        if (window.location.host.toLowerCase() === "www.arcgis.com") {
            window.addEventListener("message", receiveMessageGIS, false);
        } else {
            if (!receiverAdded) {
                window.addEventListener("message", receiveMessageWME, false);
                receiverAdded = true;
            }
            if (W && W.loginManager &&
                W.loginManager.events.register &&
                W.map) {
                log('Initializing...', 1);
                init();
            } else {
                log('Bootstrap failed. Trying again...', 1);
                window.setTimeout(function () {
                    bootstrap();
                }, 200);
            }
        }
    }

    log('Bootstrap...', 1);
    bootstrap();
})();