WME Copy Segment Info Button

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 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();
})();