PA UC Autochatter

7/2/2020, 4:43:13 PM

As of 2020-07-02. See the latest version.

// ==UserScript==
// @name        PA UC Autochatter
// @namespace   Violentmonkey Scripts
// @match       https://www.uc.pa.gov/Chat/index.aspx
// @grant       none
// @version     2.0
// @author      Barkin MAD
// @require https://code.jquery.com/jquery-3.5.1.min.js
// @description 7/2/2020, 4:43:13 PM
// ==/UserScript==

// In the below 4 fields enter your relevant information inbetween the quotes to be sent into chat. Do NOT delete anything that is not inbetween the quotes or it will break. Example: FIRSTNAME = "Dimitri";
const FIRSTNAME = "FIRST_NAME_HERE";
const LASTNAME = "LAST_NAME_HERE";
const EMAIL = "EMAIL_HERE";
const PHONE = "PHONE_NUMBER_HERE";

// Warning! Do not edit anything below this message unless you know what you're doing. Things can break.
//.........................................................................................
//.WWW...WWWWW...WWW...AAAA......RRRRRRRRR....NNNN....NNN..III..NNNN....NNN.....GGGGGG.....
//.WWW...WWWWW...WWW...AAAAA.....RRRRRRRRRRR..NNNN....NNN..III..NNNN....NNN...GGGGGGGGGG...
//.WWWW..WWWWW..WWWW...AAAAA.....RRRRRRRRRRR..NNNNN...NNN..III..NNNNN...NNN...GGGGGGGGGGG..
//.WWWW..WWWWW..WWWW..AAAAAA.....RRR.....RRR..NNNNN...NNN..III..NNNNN...NNN..GGGG....GGGG..
//..WWW.WWWWWWW.WWW...AAAAAAA....RRR.....RRR..NNNNNN..NNN..III..NNNNNN..NNN..GGG......GG...
//..WWW.WWW.WWW.WWW..AAAA.AAA....RRRRRRRRRRR..NNNNNNN.NNN..III..NNNNNNN.NNN.NGGG...........
//..WWWWWWW.WWW.WWW..AAA..AAAA...RRRRRRRRRR...NNN.NNN.NNN..III..NNN.NNN.NNN.NGGG...GGGGGG..
//..WWWWWWW.WWWWWWW..AAAAAAAAA...RRRRRRRR.....NNN.NNNNNNN..III..NNN.NNNNNNN.NGGG...GGGGGG..
//...WWWWWW..WWWWW..AAAAAAAAAA...RRR..RRRR....NNN..NNNNNN..III..NNN..NNNNNN..GGG...GGGGGG..
//...WWWWW...WWWWW..AAAAAAAAAAA..RRR...RRRR...NNN..NNNNNN..III..NNN..NNNNNN..GGGG.....GGG..
//...WWWWW...WWWWW..AAA.....AAA..RRR....RRRR..NNN...NNNNN..III..NNN...NNNNN...GGGGGGGGGGG..
//...WWWWW...WWWWW.WAAA.....AAAA.RRR....RRRR..NNN....NNNN..III..NNN....NNNN...GGGGGGGGGG...
//...WWWW.....WWWW.WAA......AAAA.RRR.....RRRR.NNN....NNNN..III..NNN....NNNN.....GGGGGG.....
//.........................................................................................
// Warning! Do not edit anything below this message unless you know what you're doing. Things can break.


const SUBJECT = 1;
// BS messages to be ignored
const BSCHAT = [
    "Chat Started",
    "DLI Chat Connected",
    "Chat is available Monday – Friday, 7 a.m. - 6 p.m.   An Unemployment Compensation Chat Agent will be with you shortly.",
    "We are experiencing a much higher than normal volume. An Unemployment Compensation Chat Agent will be with you shortly.",
    "All of our chat agents are busy assisting other customers. We are unable to process your chat request at this time. Please try again later.",
    "Our office is closed. Please try again during our normal hours of operation.",
    "Have a great day.",
    "Chat Ended"
];

// Messages meaning the chat has ended.
const CHATENDPROMPTS = ["All of our chat agents are busy assisting other customers. We are unable to process your chat request at this time. Please try again later.", "Have a great day."];

$('document').ready(findChatButton);

// Wait for chat button to appear.
function findChatButton() {
  console.log("Starting...");
  var startTime = performance.now();
  var startButtonExists = setInterval(function() {
    // Try to close chat incase it was left open.
    $(".cx-end-confirm").trigger("click");
    $(".cx-button-close").trigger("click");
    console.log("Searching for start button...");
    if($(".cx-webchat-chat-button").length) {
      console.log("start button ready. in: " + (performance.now() - startTime) + "ms");
      clearInterval(startButtonExists);
      openAndWaitForChat($(".cx-webchat-chat-button"));
    }
  }, 100);
}

// Click chat button, wait for chat to open.
function openAndWaitForChat(cxWebChatButton) {
  cxWebChatButton.trigger('click');
  var startTime = performance.now();
  var chatExists = setInterval(function() {
    console.log("Searching for chat window...");
    if($(".cx-webchat").length){
      console.log("Chat window ready in: " + (performance.now() - startTime) + "ms");
      clearInterval(chatExists);
      fillFormDetails();
    }
  }, 100);
}

// Enters personal information into the chat window.
function fillFormDetails() {
    console.log("Filling in form details.")
    var event = new Event('input', {
      bubbles: true,
      cancelable: true,
    });
  
    document.getElementById("cx_webchat_form_firstname").value = FIRSTNAME;
    document.getElementById("cx_webchat_form_firstname").dispatchEvent(event);
    document.getElementById("cx_webchat_form_lastname").value = LASTNAME;
    document.getElementById("cx_webchat_form_lastname").dispatchEvent(event);
    document.getElementById("cx_webchat_form_email").value = EMAIL;
    document.getElementById("cx_webchat_form_email").dispatchEvent(event);
    document.getElementById("cx_webchat_form_phone").value = PHONE;
    document.getElementById("cx_webchat_form_phone").dispatchEvent(event);
    document.getElementById("cx_webchat_form_subject").selectedIndex = SUBJECT;
  
    ListenMessages();
}

// Starts chat session, when chat ends refreshes page and tries again, if someone types the script stops.
function ListenMessages() {
  console.log("Starting chat session.");
  $(".cx-submit.cx-btn.cx-btn-primary.i18n").trigger("click");
  console.log("Listening for messages...");
  messageListener = setInterval(function(){
    $(".cx-message-text").each(function(){
      var message = this.textContent; 
      if (BSCHAT.indexOf(message) > -1) {
          if(CHATENDPROMPTS.indexOf(message) > -1){
            // Chat ended. click close chat, wait for confirm button, press it,  then refresh.
            console.log("End prompt detected: " + message + " trying again.");
            $(".cx-button-close").trigger("click");
            var startTime = performance.now();
            var confirmCloseExists = setInterval(function() {
              console.log("Searching for close button...");
              if($(".cx-end-confirm").length) {
              console.log("confirm close ready in: " + (performance.now() - startTime) + "ms");
              clearInterval(confirmCloseExists);
              $(".cx-end-confirm").trigger("click");
              $(".cx-button-close").trigger("click");
              clearInterval(messageListener);
              Refresh();
            }}, 100);  
          }
      } else {
          // Agent or user typed into chat. Stop script.
          console.log("Message is real: " + message + "| Stopping script");
          clearInterval(messageListener);
      }
    });
  }, 100);
}

function Refresh() {
    location.reload();
}