WME Copy Segment Info Button

Voegt een knop toe in WME om geselecteerde segmentgegevens te kopiëren naar het clipboard

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         WME Copy Segment Info Button
// @version      0.2
// @description  Voegt een knop toe in WME om geselecteerde segmentgegevens te kopiëren naar het clipboard
// @author       Ronald (rdnnk)
// @match        https://www.waze.com/editor*
// @match        https://www.waze.com/*/editor*
// @match        https://beta.waze.com/editor*
// @match        https://beta.waze.com/*/editor*
// @grant        GM_setClipboard
// @namespace https://chat.openai.com/
// ==/UserScript==

(function() {
    'use strict';

    function waitForWME() {
        if (typeof W === 'undefined' || typeof W.selectionManager === 'undefined') {
            setTimeout(waitForWME, 5000);
        } else {
            addButtonWhenReady();
        }
    }

    function addButtonWhenReady() {
        const observer = new MutationObserver(() => {
            const overlayButtons = document.querySelector('.overlay-buttons-container');
            if (overlayButtons && !document.getElementById('copy-segment-button')) {
                const button = document.createElement('wz-button');
                button.id = 'copy-segment-button';
                button.className = 'layer-switcher-button overlay-button';
                button.setAttribute('color', 'clear-icon');

                const icon = document.createElement('i');
                icon.className = 'w-icon w-icon-copy';
                button.appendChild(icon);

                button.addEventListener('click', copySegmentInfo);

                overlayButtons.appendChild(button);

                console.log('✅ Copy Segment button edit in WME.');
                observer.disconnect(); // Stop observer 
            }
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

function copySegmentInfo() {
    const addressSpan = document.querySelector('.full-address-container .full-address');
    const button = document.getElementById('copy-segment-button');

    if (!addressSpan) {
        button.style.background = 'red';
        button.textContent = '❌ Geen adres';
        setTimeout(() => {
            button.style.background = '#2b8cbe';
            button.textContent = '📋 Copy Addr';
        }, 1500);
        return;
    }

    let addressText = addressSpan.innerText.trim();

    // Vind de laatste komma (scheiding stad/land)
    const lastCommaIndex = addressText.lastIndexOf(',');
    if (lastCommaIndex === -1) {
        button.style.background = 'red';
        button.textContent = '❌ Ongeldig adres';
        setTimeout(() => {
            button.style.background = '#2b8cbe';
            button.textContent = '📋';
        }, 4500);
        return;
    }

    const withoutCountry = addressText.substring(0, lastCommaIndex).trim(); // verwijder land

    // Vind de laatste komma opnieuw (nu tussen straat en stad)
    const secondLastCommaIndex = withoutCountry.lastIndexOf(',');
    if (secondLastCommaIndex === -1) {
        // Geen stad gevonden, gewoon kopiëren
        GM_setClipboard(withoutCountry);
    } else {
        const street = withoutCountry.substring(0, secondLastCommaIndex).trim();
        const city = withoutCountry.substring(secondLastCommaIndex + 1).trim();

        let formatted;
        if (street.toLowerCase() === city.toLowerCase() || city.toLowerCase()==='no city') {
            formatted = street;
        } else {
            formatted = `${street} in ${city}`;
        }

        GM_setClipboard(formatted);
    }

    // Visuele bevestiging
    button.style.background = 'gold';
    button.textContent = '✅';
    setTimeout(() => {
        button.style.background = '#2b8cbe';
        button.textContent = '📋';
    }, 1000);
}


    waitForWME();
})();