WME Open DeFlock

Adds a direct link to this location on the DeFlock map.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         WME Open DeFlock
// @namespace    https://greatest.deepsurf.us/en/users/1365511-robosphinx
// @version      2026.05.18.001
// @description  Adds a direct link to this location on the DeFlock map.
// @author       robosphinx
// @match        *://*.waze.com/*editor*
// @exclude      *://*.waze.com/user/editor*
// @grant        none
// @license      GPLv3
// @grant        none
// @grant        GM_info
// ==/UserScript==

// Format: https://maps.deflock.org/?lat=[lat2.4]&lng=[lon2.4]&zoom=16.00
// Link updating idea ripped from gncnpk search for closure

(function() {
    'use strict';

    const SCRIPT_LONG_NAME = GM_info.script.name;
    const SCRIPT_SHORT_NAME = "WME-ODF";
    const SCRIPT_VERSION = GM_info.script.version;

    let successfulStartup = true;

    // Projection conversion ripped from traffic light checker by jushag
    let _humanProj = null;
    let _wmeProj = null;
    let link;

    function getOLProjections() {
        if (!_humanProj) _humanProj = new OpenLayers.Projection("EPSG:4326");
        if (!_wmeProj) _wmeProj = W.map.getProjectionObject();
        return { humanProj: _humanProj, wmeProj: _wmeProj };
    }
    // Finish Projection conversion ripped from traffic light checker by jushag

    function fancyLogMessage(tag, message) {
        if (tag == "ERROR") {
            console.error(SCRIPT_SHORT_NAME + ": " + tag + ": " + message);
        }
        else {
            console.log(SCRIPT_SHORT_NAME + ": " + tag + ": " + message);
        }
    }

    function addTheLink() {
        link = document.createElement("a");
        link.className = "wz-map-black-link";
        // link.style = "margin-left: auto;";
        link.target = "_blank";
        link.innerHTML = "DeFlock";
        $(".wz-map-ol-footer")[0].appendChild(link);
        updateTheLink();
        fancyLogMessage("DEBUG", "Startup - added link!");
    }

    function updateTheLink() {
        let center = W.map.getCenter();
        const { humanProj, wmeProj } = getOLProjections();
        const projected = new OpenLayers.LonLat(center.lon, center.lat).transform( wmeProj, humanProj );
        let wmeZoom = W.map.getZoom();
        let flockLink = `https://maps.deflock.org/?lat=${projected.lat}&lng=${projected.lon}&zoom=${wmeZoom-1}`;
        link.href = flockLink;
        fancyLogMessage("DEBUG", `Map move - updated link to ${flockLink}`);
    }

    function init() {
        const wmeSDK = getWmeSdk({ scriptId: SCRIPT_SHORT_NAME, scriptName: SCRIPT_LONG_NAME });
        fancyLogMessage("INFO", SCRIPT_LONG_NAME + " " + SCRIPT_VERSION + " started");
        addTheLink();

        // Event registration ripped from tl checker, too
        if (wmeSDK?.Events?.on) {
            wmeSDK.Events.on({
                eventName: "wme-map-move-end",
                eventHandler: updateTheLink
            });

            wmeSDK.Events.on({
                eventName: "wme-map-zoom-changed",
                eventHandler: updateTheLink
            });
        }
        else if (W?.map?.events?.register) {
            W.map.events.register("moveend", null, updateTheLink);
            W.map.events.register("zoomend", null, updateTheLink);
        }
        // Finish Event registration ripped from tl checker, too
        else {
            successfulStartup = false;
        }

        if (successfulStartup) {
            fancyLogMessage("INFO", SCRIPT_LONG_NAME + " initialized!");
        }
        else {
            fancyLogMessage("ERROR", SCRIPT_LONG_NAME + " could not initialize.");
        }
    }

    function bootstrap() {
        if (typeof W === 'object' && W.userscripts?.state.isReady) {
            init();
        } else {
            document.addEventListener('wme-ready', init, { once: true });
        }
    }

    bootstrap();
})();