WazeMY

WME script for WazeMY editing moderation

目前为 2021-05-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         WazeMY
// @namespace    http://junyianl.net/
// @version      2021.05.05.01
// @description  WME script for WazeMY editing moderation
// @author       junyianl
// @include      /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/
// @require      https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
// @grant        none
// ==/UserScript==

/* global W */
/* global WazeWrap */

(function() {
    'use strict';

    var tooltipDiv = null;
    var tooltipOffset = {
        x: 15,
        y: 15
    };

    function initTooltip() {
        tooltipDiv = document.createElement("div");
        tooltipDiv.id = "wazemyTooltip";
        tooltipDiv.style.height = "auto";
        tooltipDiv.style.width = "auto";
        tooltipDiv.style.background = "rgba(0, 0, 0, 0.5)";
        tooltipDiv.style.color = "white";
        tooltipDiv.style.borderRadius = "5px";
        tooltipDiv.style.padding = "5px";
        tooltipDiv.style.position = "absolute";
        tooltipDiv.style.top = "0px";
        tooltipDiv.style.left = "0px";
        tooltipDiv.style.visibility = "hidden";
        tooltipDiv.style.zIndex = 10000;
        document.body.appendChild(tooltipDiv);
    }

    async function WazeMY_init() {
        // add WazeMY tab in sidebar
        let username = WazeWrap.User.Username();
        let rank = WazeWrap.User.Rank();
        let tabhtml = '<div>Version: ' + GM_info.script.version + '</div>';
        tabhtml += '<div>' + username + '(' + rank + ')</div>';

        tabhtml += '<br>';
        tabhtml += '<div id="wazemy_settings">';
        tabhtml += '<b>Settings</b><br>';
        tabhtml += '<input type="checkbox" id="wazemy_settings_popup">Map tooltip<br>';
        tabhtml += '</div>';

        tabhtml += '<br>';
        tabhtml += '<div id="wazemy_info">';
        tabhtml += '<b>Shortcuts</b><br>';
        tabhtml += 'Ctrl+Alt+c: Copy lat/lon of mouse position to clipboard<br>';
        tabhtml += '</div>';

        new WazeWrap.Interface.Tab('WazeMY', tabhtml, WazeMY_init2)

        // initialize tooltip for mouse over segments and places
        initTooltip();
        //WazeWrap.Events.register("mousemove", null, wazemy_showMapTooltip);

        // initialize keyboard shortcut for copying lat/lon
        new WazeWrap.Interface.Shortcut('WazeMY_latloncopy', 'Copies lat/lon of mouse position', 'wazemy', 'WazeMY', 'CA+c', wazemy_copyLatLon, null).add();
        // WazeWrap.Events.register("moveend", null, wazemy_moveend);
        // WazeWrap.Events.register("zoomend", null, wazemy_zoomend);
        // WazeWrap.Events.register("changelayer", null, wazemy_changelayer);
        // WazeWrap.Events.register("selectionchanged", null, wazemy_selectionchanged);
    }

    function WazeMY_init2() {
        // set up event handlers
         $('#wazemy_settings_popup').change(function() {
            if(this.checked) {
                WazeWrap.Events.register("mousemove", null, wazemy_showMapTooltip);
            }
            else {
                WazeWrap.Events.unregister("mousemove", null, wazemy_showMapTooltip);
            }
        });

    }

    function wazemy_moveend(arg1){
        console.log("WazeMY: moveend");
    }
    
    function wazemy_zoomend(arg1){
        console.log("WazeMY: zoomend");
    }
    
    function wazemy_showMapTooltip(e){
        var landmark = null;
        var segment = null;
        var result = "";
        var showTooltip = false;
        
        // landmark = W.map.landmarkLayer.getFeatureBy("renderIntent", "highlight");
        landmark = W.map.venueLayer.getFeatureBy("renderIntent", "highlight");
        segment = W.map.segmentLayer.getFeatureBy("renderIntent", "highlight");
        
        if (landmark != null) {
            // console.log(landmark.model);
            result = "<b>"+landmark.model.attributes.name+"</b><br>";
            var address = landmark.model.getAddress();
            try {
                result += (address.attributes.houseNumber?(address.attributes.houseNumber+", "):"") + (address.attributes.street.name?address.attributes.street.name:"No street") + "<br>";
                result += address.attributes.city.attributes.name + ", " + address.attributes.state.name + "<br>";
            }
            catch {
                result += "No address<br>";
            }
            result += "<b>Lock:</b> " + (landmark.model.getLockRank() + 1);
            showTooltip = true;
        }
        else if (segment != null) {
            // console.log(segment.model);
            var segmentId = segment.model.attributes.id;
            var primaryStreetId = WazeWrap.Model.getPrimaryStreetID(segmentId);
            var address = segment.model.getAddress();
            result = "<b>" + (address.attributes.street.name?address.attributes.street.name:"No street") + "</b><br>";
            result += address.attributes.city.attributes.name + ", " + address.attributes.state.name + "<br>";
            result += "<b>ID:</b> " + segmentId + "<br>";
            result += "<b>Lock:</b> " + (segment.model.getLockRank() + 1);
            showTooltip = true;
        }
        
        if (showTooltip == true) { // adjust tooltip position and make it visible
            var tw = tooltipDiv.clientWidth;
            var th = tooltipDiv.clientHeight;
            var tooltipX = e.clientX + window.scrollX + tooltipOffset.x;
            var tooltipY = e.clientY + window.scrollY + tooltipOffset.y;
            if ((tooltipX + tw) > W.map.$map.innerWidth()) { // adjust tooltip position to keep within map window
                tooltipX -= tw + 20; // 20 = scroll bar size
                if (tooltipX < 0) tooltipX = 0;
            }
            if ((tooltipY + th) > W.map.$map.innerHeight()) {
                tooltipY -= th + 20;
                if (tooltipY < 0) tooltipY = 0;
            }
            tooltipDiv.style.top = tooltipY + "px";
            tooltipDiv.style.left = tooltipX + "px";
            tooltipDiv.innerHTML = result;
            tooltipDiv.style.visibility = "visible";
        }
        else { // hide tooltip
            tooltipDiv.style.visibility = "hidden";
        }
    }
    
    function wazemy_copyLatLon(){
        let elMousepos = document.getElementsByClassName("mouse-position");
        copyToClipboard(elMousepos[0].innerText);
    }
    
    function wazemy_changelayer(arg1){
        console.log("WazeMY: changelayer");
    }
    
    function wazemy_selectionchanged(arg1){
        console.log("WazeMY: selectionchanged");
    }
    
    // courtesy of PIE
    var copyToClipboard = function(str) {
        var $temp = $('<input>');
        $('body').append($temp);
        $temp.val(str).select();
        document.execCommand('copy');
        $temp.remove();
    };

    function wazemy_bootstrap(tries) {
        if (W && W.map && W.model && WazeWrap.Ready) {
            WazeMY_init();
            // WazeWrap.Alerts.info(GM_info.script.name, "Loaded...");
        }
        else if (tries < 1000) {
            // console.log(GM_info.script.name, "wazemy_bootstrap failed. Trying again...");
            window.setTimeout(wazemy_bootstrap, 200, ++tries);
        }
        else {
            WazeWrap.Alerts.error(GM_info.script.name, "Bootstrap timed out.");
        }
    }
    wazemy_bootstrap(1);
})();