// ==UserScript== // @name WME EZRoad // @namespace https://greasyfork.org/en/scripts/518381-wme-ezroad // @version 0.0.7 // @description Easily update roads // @author https://github.com/michaelrosstarr // @match https://www.waze.com/*/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 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 = () => { return JSON.parse(window.localStorage.getItem('WME_EZRoads_Options')) || {roadType: 1, unpaved: false, setStreet: false, autosave: false}; } 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()); } } 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) { // add the button here // if (addedNode.querySelector('div.speed-limit-fwd') || addedNode.querySelector('div.speed-limit-rev')) { // makeSigns(); // } let editSegment = addedNode.querySelector('#segment-edit-general'); if (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) => { if (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 => { 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 }) } } 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 }) } }) if (options.autosave) { wmeSDK.Editing.save().then(() => {}); } } const constructSettings = () => { let localOptions = getOptions(); const update = (key, value) => { const options = getOptions(); options[key] = value; localOptions[key] = value; saveOptions(options); } // -- Set up the tab for the script wmeSDK.Sidebar.registerScriptTab().then(({ tabLabel, tabPane }) => { tabLabel.innerText = 'EZRoads'; tabLabel.title = 'Easily Update Roads'; tabPane.innerHTML = '
'; const scriptContentPane = $('#ezroads-settings'); scriptContentPane.append(`