WME Openers (StreetView.vn, Mapillary, OSM)

Opens StreetView.vn (Ctrl+Alt+F), Mapillary (Ctrl+Alt+G), or OSM (Ctrl+Alt+H) at the current WME map center and zoom level.

目前為 2025-10-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         WME Openers (StreetView.vn, Mapillary, OSM)
// @version      1.0.4
// @description  Opens StreetView.vn (Ctrl+Alt+F), Mapillary (Ctrl+Alt+G), or OSM (Ctrl+Alt+H) at the current WME map center and zoom level.
// @author       Minh Tan
// @match        https://www.waze.com/editor*
// @match        https://www.waze.com/*/editor*
// @match        https://beta.waze.com/editor*
// @match        https://beta.waze.com/*/editor*
// @exclude      https://www.waze.com/user/editor*
// @exclude      https://www.waze.com/*/user/editor*
// @grant        none
// @noframes
// @license      MIT
// @namespace https://greasyfork.org/users/1440408
// ==/UserScript==

(function() {
    'use strict';

    // Function to get coordinates in WGS84 (EPSG:4326)
    function getWGS84Center() {
        if (!window.W || !window.W.map || !window.W.map.getCenter) {
            console.error("WME Openers: WME map object (W.map) not available.");
            return null;
        }
        const mapCenter = window.W.map.getCenter(); // Center in Web Mercator (EPSG:900913)
        try {
            const proj900913 = new OpenLayers.Projection("EPSG:900913");
            const proj4326 = new OpenLayers.Projection("EPSG:4326");
            // Create a LonLat object and transform it from Web Mercator to WGS84
            const lonLat = new OpenLayers.LonLat(mapCenter.lon, mapCenter.lat);
            const transformedLonLat = lonLat.transform(proj900913, proj4326);

            return {
                lat: transformedLonLat.lat,
                lng: transformedLonLat.lon
            };
        } catch (e) {
            console.error("WME Openers: Error transforming coordinates:", e);
            return null;
        }
    }

    // Function to get the current zoom level from Waze
    function getWazeZoom() {
        if (!window.W || !window.W.map || typeof window.W.map.getZoom !== 'function') {
            console.error("WME Openers: WME map object (W.map) or getZoom function not available.");
            return null;
        }
        return window.W.map.getZoom();
    }


    function handleShortcut(event) {
        const isCtrl = event.ctrlKey;
        const isAlt = event.altKey; // Corrected from isShift and alttKey
        const isFKey = event.key === 'f' || event.key === 'F';
        const isGKey = event.key === 'g' || event.key === 'G';
        const isHKey = event.key === 'h' || event.key === 'H';
        const isMacCmd = event.metaKey;

        const openStreetView = (isCtrl || isMacCmd) && isAlt && isFKey;
        const openMapillary = (isCtrl || isMacCmd) && isAlt && isGKey;
        const openOSM = (isCtrl || isMacCmd) && isAlt && isHKey;

        if (openStreetView || openMapillary || openOSM) {
            event.preventDefault();
            event.stopPropagation();

            const coords = getWGS84Center();
            const zoom = getWazeZoom(); // Get current Waze zoom level

            if (coords && zoom !== null) {
                if (openStreetView) {
                    // Assuming StreetView.vn might accept a 'z' or 'zoom' parameter.
                    const url = `https://www.streetview.vn/?lat=${coords.lat}&lng=${coords.lng}&z=${zoom}`;
                    window.open(url, '_blank');
                    console.log(`WME Openers: Opened StreetView.vn: ${url}`);
                 } else if (openMapillary) {
                    const mapillaryUrl = `https://www.mapillary.com/app/?lat=${coords.lat}&lng=${coords.lng}&z=${zoom}`;
                    window.open(mapillaryUrl, '_blank');
                    console.log(`WME Openers: Opened Mapillary: ${mapillaryUrl}`);
                } else if (openOSM) {
                    const osmUrl = `https://www.openstreetmap.org/edit#map=${zoom}/${coords.lat}/${coords.lng}`;
                    window.open(osmUrl, '_blank');
                    console.log(`WME Openers: Opened OpenStreetMap: ${osmUrl}`);
                }
            } else {
                console.warn("WME Openers: Could not retrieve WME map coordinates or zoom level.");
            }
        }
    }

    function init() {
        document.addEventListener('keydown', handleShortcut);
        console.log("WME Openers (StreetView.vn, Mapillary, OSM) script loaded. Use Ctrl+Alt+[F,G,H]");
    }

    // Wait for WME to be ready
    if (window.W && window.W.map) {
        init();
    } else {
        document.addEventListener('wme-initialized', init, { once: true });
    }

})();