// ==UserScript== // @name WME EZRoads // @namespace https://greasyfork.org/en/scripts/518381-wme-ezroad // @version 0.1.2 // @description Easily update roads // @author https://github.com/michaelrosstarr // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor.*$/ // @exclude https://www.waze.com/user/*editor/* // @exclude https://www.waze.com/*/user/*editor/* // @grant GM_getValue // @grant GM_setValue // @icon https://www.google.com/s2/favicons?sz=64&domain=waze.com // @grant none // @license MIT // @downloadURL none // ==/UserScript== const ScriptName = GM_info.script.name; const ScriptVersion = GM_info.script.version; let wmeSDK; const defaultOptions = { roadType: 1, unpaved: false, setStreet: false, autosave: false, setSpeed: 60 }; const log = (message) => { if (typeof message === 'string') { console.log('WME_EZRoads: ' + message); } else { console.log('WME_EZRoads: ', message); } } window.SDK_INITIALIZED.then(initScript); function initScript() { wmeSDK = getWmeSdk({ scriptId: "wme-ez-roads", scriptName: "EZ Roads" }); WME_EZRoads_bootstrap(); } const getCurrentCountry = () => { return wmeSDK.DataModel.Countries.getTopCountry(); } const getTopCity = () => { return wmeSDK.DataModel.Cities.getTopCity(); } const getAllCities = () => { return wmeSDK.DataModel.Cities.getAll(); } const saveOptions = (options) => { window.localStorage.setItem('WME_EZRoads_Options', JSON.stringify(options)); } const getOptions = () => { const savedOptions = JSON.parse(window.localStorage.getItem('WME_EZRoads_Options')) || {}; // Merge saved options with defaults to ensure all expected options exist return { ...defaultOptions, ...savedOptions }; } const WME_EZRoads_bootstrap = () => { if ( !document.getElementById('edit-panel') || !wmeSDK.DataModel.Countries.getTopCountry() ) { setTimeout(WME_EZRoads_bootstrap, 250); return; } if (wmeSDK.State.isReady) { WME_EZRoads_init(); } else { wmeSDK.Events.once({ eventName: 'wme-ready' }).then(WME_EZRoads_init()); } } let openPanel; const WME_EZRoads_init = () => { log("Initing"); const roadObserver = new MutationObserver((mutations) => { mutations.forEach((mutation) => { for (let i = 0; i < mutation.addedNodes.length; i++) { const addedNode = mutation.addedNodes[i]; if (addedNode.nodeType === Node.ELEMENT_NODE) { let editSegment = addedNode.querySelector('#segment-edit-general'); if (editSegment) { openPanel = editSegment; const quickButton = document.createElement('wz-button'); quickButton.setAttribute('type', 'button'); quickButton.setAttribute('style', 'margin-bottom: 5px, width: 100%'); quickButton.setAttribute('disabled', 'false'); quickButton.classList.add('send-button', 'ez-comment-button'); quickButton.textContent = 'Quick Set Road'; editSegment.parentNode.insertBefore(quickButton, editSegment); quickButton.addEventListener('mousedown', () => handleUpdate()); } } } }); }); roadObserver.observe(document.getElementById('edit-panel'), { childList: true, subtree: true }); constructSettings(); document.addEventListener("keydown", (event) => { // Check if the active element is an input or textarea const isInputActive = document.activeElement && ( document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA' || document.activeElement.contentEditable === 'true' || document.activeElement.tagName === 'WZ-AUTOCOMPLETE' || document.activeElement.tagName === 'WZ-TEXTAREA' ); log(document.activeElement.tagName); log(isInputActive); // Only trigger the update if the active element is not an input or textarea if (!isInputActive && event.key.toLowerCase() === "u") { handleUpdate(); } }); log("Completed Init") } const getEmptyStreet = () => { } const getEmptyCity = () => { return wmeSDK.DataModel.Cities.getCity({ cityName: '', countryId: getCurrentCountry().id }) || wmeSDK.DataModel.Cities.addCity({ cityName: '', countryId: getCurrentCountry().id }); } const handleUpdate = () => { const selection = wmeSDK.Editing.getSelection(); if (!selection || selection.objectType !== 'segment') return; log('Updating RoadType'); const options = getOptions(); selection.ids.forEach(id => { // Road Type if (options.roadType) { const seg = wmeSDK.DataModel.Segments.getById({ segmentId: id }); if (seg.roadType !== options.roadType) { wmeSDK.DataModel.Segments.updateSegment({ segmentId: id, roadType: options.roadType }); } } // Speed Limit if (options.setSpeed != -1) { wmeSDK.DataModel.Segments.updateSegment({ segmentId: id, fwdSpeedLimit: parseInt(options.setSpeed), revSpeedLimit: parseInt(options.setSpeed) }); } // Handling the street if (options.setStreet) { let city; let street; city = getTopCity() || getEmptyCity(); street = wmeSDK.DataModel.Streets.getStreet({ cityId: city.id, streetName: '', }); log(`City ${city.id}`); if (!street) { street = wmeSDK.DataModel.Streets.addStreet({ streetName: '', cityId: city.id }); } wmeSDK.DataModel.Segments.updateAddress({ segmentId: id, primaryStreetId: street.id }) } log(options); // Updated unpaved handler with fallback if (options.unpaved) { // First try the new method - look for the unpaved chip using the icon class const unpavedIcon = openPanel.querySelector('.w-icon-unpaved-fill'); let unpavedToggled = false; if (unpavedIcon) { // Click the parent wz-checkable-chip element const unpavedChip = unpavedIcon.closest('wz-checkable-chip'); if (unpavedChip) { unpavedChip.click(); log('Clicked unpaved chip'); unpavedToggled = true; } } // If new method failed, try the old method as fallback if (!unpavedToggled) { try { const wzCheckbox = openPanel.querySelector('wz-checkbox[name="unpaved"]'); if (wzCheckbox) { const hiddenInput = wzCheckbox.querySelector('input[type="checkbox"][name="unpaved"]'); if (hiddenInput) { hiddenInput.click(); log('Clicked unpaved checkbox (fallback method)'); unpavedToggled = true; } } } catch (e) { log('Fallback unpaved toggle method failed: ' + e); } } if (!unpavedToggled) { log('Could not toggle unpaved setting - no compatible elements found'); } } }) // Autosave if (options.autosave) { wmeSDK.Editing.save().then(() => { }); } } const constructSettings = () => { const localOptions = getOptions(); const update = (key, value) => { const options = getOptions(); options[key] = value; localOptions[key] = value; saveOptions(options); }; // Reset all options to defaults const resetOptions = () => { saveOptions(defaultOptions); // Refresh the page to reload settings window.location.reload(); }; // Road type definitions const roadTypes = [ { id: 1, name: 'Street', value: 1 }, { id: 2, name: 'Primary Street', value: 2 }, { id: 3, name: 'Private Road', value: 17 }, { id: 4, name: 'Parking Lot Road', value: 20 }, { id: 5, name: 'Offroad', value: 8 }, { id: 6, name: 'Railroad', value: 18 } ]; // Checkbox option definitions const checkboxOptions = [ { id: 'setStreet', text: 'Set Street To None', key: 'setStreet' }, { id: 'autosave', text: 'Autosave on Action', key: 'autosave' }, { id: 'unpaved', text: 'Set Road as Unpaved', key: 'unpaved' } ]; // Helper function to create radio buttons const createRadioButton = (roadType) => { const id = `road-${roadType.id}`; const isChecked = localOptions.roadType === roadType.value; const div = $(`