WazeMY

WME script for WazeMY editing moderation

当前为 2020-06-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         WazeMY
// @namespace    http://junyianl.net/
// @version      2020.06.03.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);
    }

    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><br>';
        tabhtml += '<div>' + username + '(' + rank + ')</div>';
        new WazeWrap.Interface.Tab('WazeMY', tabhtml, null)

        // 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_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);
})();