Greasy Fork is available in English.

WME myUR

Highlight URs based on days since last response

// ==UserScript==
// @name         WME myUR
// @namespace    https://greasyfork.org/en/users/668704-phuz
// @require      https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
// @version      1.07
// @description  Highlight URs based on days since last response
// @author       phuz
// @include      /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
// @require      http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js
// @grant        GM_xmlhttpRequest
// @grant        GM_info
// @grant        GM_fetch
// @grant        GM_addStyle

/* global OpenLayers */
/* global W */
/* global WazeWrap */
/* global $ */
/* global I18n */
/* global _ */
/* global MutationObserver */

// ==/UserScript==
let myURmarkers;
let mapBounds;
const sleep = (time) => {
    return new Promise((resolve) => setTimeout(resolve, time))
}

(function () {
    'use strict';
    //Bootstrap
    function bootstrap(tries = 1) {
        if (W && W.loginManager && W.map && W.loginManager.user && W.model
            && W.model.states && W.model.states.getObjectArray().length && WazeWrap && WazeWrap.Ready) {
            myURmarkers = new OpenLayers.Layer.Markers('myURmarkers');
            W.map.addLayer(myURmarkers);
            W.map.getOLMap().setLayerIndex(myURmarkers, 2);
            setTimeout(function () {
                if (W.map.getZoom() >= 12) {
                    getBounds();
                    getURs();
                }
                W.map.events.register("moveend", W.map, function () {
                    myURmarkers.clearMarkers();
                    setTimeout(function () {
                        if (W.map.getZoom() >= 12) {
                            getBounds();
                            getURs();
                        }
                    }, 250);
                });
                console.log("WME myUR Loaded!");
            }, 500);
        } else if (tries < 1000) {
            setTimeout(function () { bootstrap(++tries); }, 200);
        }
    }

    function getBounds() {
        mapBounds = W.map.getExtent();
    }

    async function getURs() {
        let URarray = document.getElementsByClassName("map-problem user-generated");

        //Scan the list of URs that were populated in WME
        for (let i = 0; i < URarray.length; i++) {
            let URid = document.getElementsByClassName("map-problem user-generated")[i].getAttribute("data-id");
            //Only highlight URs that are still open
            if (W.model.mapUpdateRequests.getObjectById(URid).editable) {
                //Continue if the UR is in the bounds of the WME window
                if ((W.model.mapUpdateRequests.getObjectById(URid).attributes.geometry.x > mapBounds.left) && (W.model.mapUpdateRequests.getObjectById(URid).attributes.geometry.x < mapBounds.right)) {
                    if ((W.model.mapUpdateRequests.getObjectById(URid).attributes.geometry.y > mapBounds.bottom) && (W.model.mapUpdateRequests.getObjectById(URid).attributes.geometry.y < mapBounds.top)) {
                        //Continue if the UR has comments
                        if (W.model.mapUpdateRequests.getObjectById(URid).attributes.hasComments) {
                            let updatedOn = W.model.mapUpdateRequests.getObjectById(URid).attributes.updatedOn;
                            let updatedDaysAgo = moment(new Date(Date.now()), "DD.MM.YYYY").startOf('day').diff(moment(new Date(updatedOn), "DD.MM.YYYY").startOf('day'), 'days');
                            let updatedBy = W.model.mapUpdateRequests.getObjectById(URid).attributes.updatedBy;
                            //console.log(URid + ":" + updatedDaysAgo);
                            //Continue if the last comment was 4 or more days ago
                            if ((updatedDaysAgo) >= 4 || (updatedBy == -1)) {
                                setTimeout(async function () {
                                    let URdata = await W.controller.descartesClient.getUpdateRequestSessionsByIds(URid);
                                    if (URdata.users.objects.length > 0) {
                                        for (let j = URdata.users.objects.length - 1; j >= 0; j--) {
                                            //Continue if my username matches a UR response
                                            if ((URdata.users.objects[j].userName == W.loginManager.user.userName) || true) {
                                                let commentLength = URdata.updateRequestSessions.objects[0].attributes.comments.length;
                                                let lastCommentTime = URdata.updateRequestSessions.objects[0].attributes.comments[commentLength - 1].createdOn;
                                                lastCommentTime = moment(new Date(lastCommentTime), "DD.MM.YYYY").startOf('day');
                                                let timeNow = moment(new Date(Date.now()), "DD.MM.YYYY").startOf('day');
                                                let daysSinceLastMessage = timeNow.diff(lastCommentTime, 'days');
                                                //console.log(URid + ":" + daysSinceLastMessage);
                                                if (daysSinceLastMessage >= 5) {
                                                    drawMarkers(URdata.updateRequestSessions.objects[0].attributes.id, "red");
                                                } else if (daysSinceLastMessage >= 4) {
                                                    drawMarkers(URdata.updateRequestSessions.objects[0].attributes.id, "orange");
                                                }
                                                break;
                                            }
                                        }
                                    }
                                }, 20);
                            }
                        }
                    }
                }
            }
            await sleep(20);
            Promise.resolve();
        }
    }

    function drawMarkers(URid, severity) {
        var lon = W.model.mapUpdateRequests.getObjectById(URid).attributes.geometry.x;
        var lat = W.model.mapUpdateRequests.getObjectById(URid).attributes.geometry.y;
        let image;

        //alert(new OpenLayers.LonLat([lon, lat]).transform('EPSG:3857', 'EPSG:4326'));
        const orangeCircle = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAGUUlEQVR4nO2ba4hWRRiAnzl8iIjEEiJLxBJitZqu5oVCIkWsRLM0wh9O9Sez7KamYmYkWipiZUohlIggo1ZioqUWZhcMNMVrLmoi/RBZREQkFpFlpx8zZ/fst+ebOd/unG9d2geW3Z3zznvemTP3eV9BzmhFBNQCI4CHgAFANXA30NuKNQLXgQbgInAWOAZcEpLmPO0TeSjViv7AM8AU4HGgqoOqGoBfgT3AXiG5EcTABEErQCsGA2uACUCvkLqBW8BPwGIhqQ+lNAqlyHINGEP4woPpLuMxXSUYQStASK4CK0LqLGKVkDSEVBi6BQCsBy7koPcS8GlopWVXgFbU2pE9FSG5DSzslFXpLBSSWw67Iq0YXq7SsipAK2YCp4CXXHJCshszYIXioJDs9MjMBI5YGzOTeRbQig+AZfbfq8AgIUsPSFoxBDgBFIC/gAPAUUxTvgb8CzRZ8QLQF+iHWSeMxswkQ6zMSCE57XhXFXAe6G+TlgjJyizlylQBWrEKeLcoeb2QzPHkmwqcE5JzWd6Tkr8WGOz7+lqxFphblLxSSJb43uGtAK14H/gw5dFt4OGQc3JHsJV0ivSp19sSnGOAVsygtdkX0wtYm8XInPmE0uuOFVrxgitzyRagFSOAP2hdr5diipB875HJBa2YCOzziDUCY4XkWNrD1BagFXcB2/AXHswmp6vI8u4+w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/KeKtUK7L2lH3AZfJ58LzmbfflYDtbyB1MTMgds91s4IsvyU2HR5V+xVU3SnkYrYRcmMoQ7unbbuAO3IFk5WvwNeBP3L7iKjAHfAl8XnJP63PDf2XUd7mudGAmAAAAAElFTkSuQmCC';
        var size = new OpenLayers.Size(60, 60);
        var offset = new OpenLayers.Pixel(-(size.w / 2), -(size.h / 1.3));
        switch (severity) {
            case "orange":
                image = orangeCircle;
                break;
            case "red":
                image = redCircle;
                break;
            default:
                image = orangeCircle;
        }
        var icon = new OpenLayers.Icon(image, size, offset);
        var epsg4326 = new OpenLayers.Projection("EPSG:4326"); //WGS 1984 projection
        var projectTo = W.map.getProjectionObject(); //The map projection (Spherical Mercator)
        var lonLat = new OpenLayers.LonLat(lon, lat).transform('EPSG:3857', 'EPSG:4326');
        lonLat = new OpenLayers.LonLat(lonLat.lon, lonLat.lat).transform(epsg4326, projectTo);
        var newMarker = new OpenLayers.Marker(lonLat, icon);
        newMarker.location = lonLat;
        myURmarkers.setOpacity(.75);
        myURmarkers.addMarker(newMarker);
    }

    bootstrap();

})();