WME Places Name Normalizer

Normaliza nombres de lugares en Waze Map Editor (WME)

目前為 2025-03-19 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         WME Places Name Normalizer
// @namespace    https://greasyfork.org/en/users/mincho77
// @version      1.0
// @description  Normaliza nombres de lugares en Waze Map Editor (WME)
// @author       mincho77
// @match        https://www.waze.com/*editor*
// @grant        none
// @license             MIT
// @run-at       document-end
// ==/UserScript==

(function() {
    "use strict";

    const SCRIPT_NAME = "PlacesNameNormalizer";
    const SCRIPT_VERSION = "1.0";

    function init() {
        console.log(`[${SCRIPT_NAME}] Initializing v${SCRIPT_VERSION}`);
        createSidebarTab();
        attachEvents();
    }

    function createSidebarTab() {
        const { tabLabel, tabPane } = W.userscripts.registerSidebarTab(SCRIPT_NAME);
        tabLabel.innerText = "Normalizer";
        tabLabel.title = "Normalize Waze Places";
        
        const tab = document.createElement("div");
        tab.id = "normalizer-tab";
        tab.innerHTML = `
            <h4>Place Normalizer <span style='font-size:11px;'>v${SCRIPT_VERSION}</span></h4>
            <button id="scan-places" class="btn btn-primary">Scan Places</button>
            <div id="normalizer-results" style="margin-top:10px;"></div>
            <button id="apply-normalization" class="btn btn-success" style="margin-top:10px;">Apply Changes</button>
        `;
        tabPane.appendChild(tab);
    }

    function attachEvents() {
        document.getElementById("scan-places").addEventListener("click", scanPlaces);
        document.getElementById("apply-normalization").addEventListener("click", applyNormalization);
    }

    function scanPlaces() {
        console.log("Scanning for places...");
        let resultsDiv = document.getElementById("normalizer-results");
        resultsDiv.innerHTML = "<p>Scanning...</p>";
        
        let venues = W.model.venues.getObjectArray();
        let foundPlaces = venues.filter(v => v.attributes.name && needsNormalization(v.attributes.name));
        
        resultsDiv.innerHTML = `<p>Found ${foundPlaces.length} places needing normalization.</p>`;
        
        let table = document.createElement("table");
        table.classList.add("table");
        table.innerHTML = `
            <thead>
                <tr>
                    <th>Select</th>
                    <th>Original Name</th>
                    <th>New Name</th>
                </tr>
            </thead>
            <tbody>
                ${foundPlaces.map((place, index) => `
                    <tr>
                        <td><input type='checkbox' class='place-checkbox' data-index='${index}'></td>
                        <td>${place.attributes.name}</td>
                        <td><input type='text' class='normalized-name' data-index='${index}' value='${normalizeName(place.attributes.name)}'></td>
                    </tr>
                `).join("")}
            </tbody>
        `;
        resultsDiv.appendChild(table);
    }

    function applyNormalization() {
        let checkboxes = document.querySelectorAll(".place-checkbox:checked");
        let updatedPlaces = [];

        checkboxes.forEach(cb => {
            let index = cb.dataset.index;
            let newName = document.querySelector(`.normalized-name[data-index='${index}']`).value;
            let place = W.model.venues.getObjectArray()[index];
            if (place) {
                place.attributes.name = newName;
                updatedPlaces.push(place);
            }
        });

        console.log("Updated Places:", updatedPlaces);
        alert("Normalization applied to selected places!");
    }

    function needsNormalization(name) {
        return /\b(el|la|los|las)\b/i.test(name);
    }

    function normalizeName(name) {
        return name.replace(/\b(el|la|los|las)\b/gi, "").trim();
    }

    init();
})();