WME Rapid UR Reply

Shortcut keys to answer URs with the F8 key and closes them with the F9 -- also, F8 can approve PURs and lock at L3.

// ==UserScript==
// @name         WME Rapid UR Reply
// @description  Shortcut keys to answer URs with the F8 key and closes them with the F9 -- also, F8 can approve PURs and lock at L3.
// @author       TxAgBQ
// @version      20250517
// @namespace    <https://greasyfork.org/en/users/820296-txagbq/>
// @icon         <https://www.google.com/s2/favicons?sz=64&domain=waze.com>
// @match        https://*.waze.com/*/editor*
// @match        https://*.waze.com/editor*
// @exclude      https://*.waze.com/user/editor*
// @require      https://greasyfork.org/scripts/24851-wazewrap/code/WazeWrap.js
// @grant        none
// ==/UserScript==

/* global W */

(function() {
    'use strict';

    function insertCommentViaShadowDOM(textToInsert) {
        const textareaHosts = document.querySelectorAll('wz-textarea');
        for (const host of textareaHosts) {
            const shadow = host.shadowRoot;
            if (!shadow) continue;

            const textarea = shadow.querySelector('textarea');
            if (!textarea) continue;

            textarea.focus();
            textarea.value = textToInsert;
            textarea.dispatchEvent(new Event('input', { bubbles: true }));
            return true;
        }
        return false;
    }

    function sendAndNext() {
        // Clicks send
        const sendBtn = document.querySelector("#panel-container .mapUpdateRequest .body .conversation .new-comment-form .send-button");
        if (sendBtn) sendBtn.click();

        // Clicks Next (for open URs)
        $('#panel-container .mapUpdateRequest .actions .navigation .waze-plain-btn').click();
    }

    document.addEventListener('keydown', (event) => {
        if (event.key === 'F8') {
            // Inserts this text
            // let textToInsert = "I got your map issue report.  What issue did you experience? \n\nWould you please tell us your destination EXACTLY as selected in Waze? Click on 'Where to?' then scroll down to see recent destinations.  Include both the bold name and address underneath, with city. If it's spelled out, spell it out. If it's abbreviated, abbreviate it.  How to find your recent destination: <https://www.youtube.com/watch?v=PExNTIQfJI8>  \n\nWe don't receive email replies so please respond in the Waze inbox.";

            // Clicks send
            const sendBtn = document.querySelector("#panel-container .mapUpdateRequest .body .conversation .new-comment-form .send-button");
            if (sendBtn) sendBtn.click();

            // Clicks Next (for open URs)
            $('#panel-container .mapUpdateRequest .actions .navigation .waze-plain-btn').click();


            if (insertCommentViaShadowDOM(textToInsert)) {
                setTimeout(sendAndNext, 0); // Delay to ensure UI updates before clicking-was 250
            }

            // Approves PUR then sets to lock level 2
            // Click Add and edit
            $('#panel-container .place-update-edit .actions .controls-container label[for="approved-true"]').click();
            // Clicks Next (for open PURs)
            $('#panel-container .place-update-edit .actions .navigation .waze-plain-btn').click();
        }

        if (event.key === 'F9') {
            // Inserts this text
            // Comment to close out report
            let textToInsert = "You asked us to help you with a General error report but you didn't respond back with the info we need to fix your problem, so we'll infer everything is okay and close your request. \n\nIf you were reporting a closure, the quickest way to get closures on the map is to use the Report > Closure feature built into the Waze app on your phone. <https://support.google.com/waze/answer/13753511> \n\nIf you believe Waze should automatically detect closures on its own, rather than routing you into them, you can vote here <https://waze.uservoice.com/forums/59223-waze-suggestion-box/suggestions/47051794-autodetect-closures-and-closed-roads>";

            if (insertCommentViaShadowDOM(textToInsert)) {
                setTimeout(() => {
                    // Clicks send
                    $("#panel-container .mapUpdateRequest .body .conversation .new-comment-form .send-button").click();

                    // Clicks NI
                    $('#panel-container .mapUpdateRequest .actions .controls-container label[for|="state-not-identified"]').click();

                    // Clicks Next (for open URs)- was 250
                    $('#panel-container .mapUpdateRequest .actions .navigation .waze-plain-btn').click();
                }, 0);
            }
        }
    });
})();