WME WazeMY

WME script for WazeMY editing moderation

目前为 2022-07-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         WME WazeMY
// @namespace    https://greasyfork.org/en/scripts/404584-wazemy
// @version      2022.07.21.01
// @description  WME script for WazeMY editing moderation
// @author       junyianl
// @include      https://beta.waze.com/*
// @include      https://www.waze.com/forum/*
// @include      https://www.waze.com/editor*
// @include      https://www.waze.com/*/editor*
// @exclude      https://www.waze.com/user/editor*
// @require      https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
// @grant        none
// @license      MIT
// ==/UserScript==

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

(function() {
    'use strict';
    
    var settings = {};

    function bootstrap(tries = 1) {
        if (W && W.map && W.model && W.loginManager.user && 
            $ && WazeWrap.Ready) {
            init();
        } else if (tries < 1000) {
            setTimeout(function () {bootstrap(++tries);}, 200);
        } else {
            WazeWrap.Alerts.error(GM_info.script.name, "Bootstrap timeout.")
        }
    }

    async function init() {
        let $section = $('<div>');
        $section.html([
            '<div>',
            'Version: <span id="wazemyVersion"></span><br>',
            '<span id="wazemyUsername"></span> (<span id="wazemyRank"></span>)',
            '</div><br>',
            '<div id="wazemySettings">',
            '<b>Settings</b><br>',
            '<input type="checkbox" id="wazemySettings_tooltip"><label for="wazemySettings_tooltip">Map tooltip</label></br>',
            '</div><br>',
            '<div>',
            '<b>Shortcuts</b><br>',
            'Ctrl+Alt+C: Copy lat/lon of mouse position to clipboard.<br>',
            '</div>'
        ].join(' '));

        new WazeWrap.Interface.Tab('WazeMY', $section.html(), initializeSettings);
        
        // Initialize features of WME WazeMY
        wazemyTooltip_init();

        // Initialize keyboard shortcuts
        new WazeWrap.Interface.Shortcut('WazeMY_latloncopy', 'Copies lat/lon of mouse position', 'wazemy', 'WazeMY', 'CA+c', wazemyCopyLatLon, null).add();
    }

    function wazemyTooltip_init() {
        let $tooltip = $('<div/>');
        $tooltip.attr('id', 'wazemyTooltip');
        $tooltip.css({
            'height':'auto',
            'width':'auto',
            'background':'rgba(0,0,0,0.5)',
            'color':'white',
            'borderRadius':'5px',
            'padding':'5px',
            'position':'absolute',
            'top':'0px',
            'left':'0px',
            'visibility':'hidden',
            'zIndex':'10000'
        })
        $tooltip.appendTo('body');
    }

    function wazemyTooltip_show(e) { // from URO+
        var result = '';
        var showTooltip = false;

        let landmark = W.map.venueLayer.getFeatureBy('renderIntent', 'highlight');
        let segment = W.map.segmentLayer.getFeatureBy('renderIntent', 'highlight');

        if (landmark != null) {
            result = '<b>' + landmark.model.attributes.name + '</b><br>';
            let address = landmark.model.getAddress();
            try {
                result += address.attributes.houseNumber ? (address.attributes.houseNumber + ', ') : ''
                result += address.attributes.street.name ? address.attributes.street.name : 'No street' + '<br>';
                result += address.attributes.city.attributes.name + ', ';
                result += address.attributes.state.name + '<br>';
            }
            catch {
                result += 'No address<br>';
            }
            result += '<b>Lock:</b> ' + (landmark.model.getLockRank() + 1);
            showTooltip = true;
        } else if (segment != null) {
            let segmentId = segment.model.attributes.id;
            // let primaryStreetId = WazeWrap.Model.getPrimaryStreetId(segmentId);
            let 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) {
            let tw = $('#wazemyTooltip').width();
            let th = $('#wazemyTooltip').height();
            var tooltipX = e.clientX + window.scrollX + 15;
            var tooltipY = e.clientY + window.scrollY + 15;
            if ((tooltipX + tw) > W.map.$map.innerWidth()) {
                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;
            }
            $('#wazemyTooltip').html(result);
            $('#wazemyTooltip').css({
                'top':tooltipY + 'px',
                'left':tooltipX + 'px',
                'visibility':'visible'
            });
        } else {
            $('#wazemyTooltip').css('visibility', 'hidden');
        }
    }

    function wazemyCopyLatLon(){
        copyToClipboard($('.mouse-position').text());
    }

    function initializeSettings() {
        loadSettings();

        $('#wazemyVersion').text(GM_info.script.version);
        $('#wazemyUsername').text(WazeWrap.User.Username());
        $('#wazemyRank').text(WazeWrap.User.Rank());

        // Tooltip
        setChecked('wazemySettings_tooltip', settings.tooltip);
        if (settings.tooltip)
            WazeWrap.Events.register('mousemove', null, wazemyTooltip_show);
        $('#wazemySettings_tooltip').change(function() {
            var settingName = $(this)[0].id.substr(15); // strip off the "wazemySettings_" prefix
            settings[settingName] = this.checked;
            saveSettings();
            if(this.checked)
                WazeWrap.Events.register('mousemove', null, wazemyTooltip_show);
            else
                WazeWrap.Events.unregister('mousemove', null, wazemyTooltip_show);
        });
    }

    function saveSettings() {
        if (localStorage) {
            var localsettings = {
                tooltip: settings.tooltip
            };

            localStorage.setItem('WME_wazemySettings', JSON.stringify(localsettings));
        }
    }

    function loadSettings() {
        var loadedSettings = $.parseJSON(localStorage.getItem("WME_wazemySettings"));
        var defaultSettings = {
            tooltip: false,
        };
        settings = loadedSettings ? loadedSettings : defaultSettings;
        for (var prop in defaultSettings) {
            if (!settings.hasOwnProperty(prop))
                settings[prop] = defaultSettings[prop];
        }
    }

    function setChecked(checkboxId, checked) {
        $('#' + checkboxId).prop('checked', checked);
    }

    // utility functions
    var copyToClipboard = function(str) { // from PIE
        var $temp = $('<input>');
        $('body').append($temp);
        $temp.val(str).select();
        document.execCommand('copy');
        $temp.remove();
    };
    
    bootstrap();
})();

''