TJKeller.xyz USPS Scheduled Package Pickup Autofill Userscript
August 9, 2024
Tags: Userscripts Webdev

Introduction

The Script

Here is the script I wrote to autofill this form. It is a very verbose, quick & dirty userscript that gets the job done wonderfully.

Git

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
// ==UserScript==
// @name        Fill out usps scheduled pickup
// @namespace   Violentmonkey Scripts
// @match       https://tools.usps.com/schedule-pickup-steps.htm*
// @version     1.0
// @author      Tim Keller
// @description 2/11/2024, 7:23:34 PM
// ==/UserScript==

/* Basic config */
const fill = {
  emailAddress: "youremail@example.com",
  phoneNumber: "777-777-7777",
  zipCode: "60606",
  state: "IL",
  city: "CHICAGO",
  addressLineOne: "701 My Lovely Home St.",
  lastName: "Smith",
  firstName: "John"
}

const packageLocationFill = "Porch"

/* Fill funcs */
function fillInitialFields() {
  for (let fieldName in fill)
    document.getElementById(fieldName).value = fill[fieldName]
}

function fillDogField() {
  const f = document.querySelector("#second-radio-verification[name=isDogHere]") // the 'no' option (I do not own a dog)
  f.checked = true
  return f
}

function fillPackageLocation() {
  const f = document.getElementById("packageLocation")
  f.value = packageLocationFill
  return f
}

function fillPickupTime() {
  const f = document.getElementById("pickup-regular-time")
  f.click()
  return f
}

function fillPickupDate() {
  const f = document.querySelector("#schedule-pickup-cal td:not(.ui-datepicker-unselectable)") // Gets first date that is selectable
  f.click()
  return f
}

function fillTerms() {
  const f = document.querySelector(".termsConditions")
  f.checked = true
  return f
}

function fillHazmat() {
  const f = document.querySelector("#hazmat-no")
  f.checked = true
  return f
}

function scrollToDetails() {
  document.getElementById("quantityCheck").scrollIntoView()
}

function showPickupDateInStepFour(pickupDateTD) {
  const month = parseInt(pickupDateTD.dataset.month) + 1
  const year  = pickupDateTD.dataset.year
  const day   = pickupDateTD.firstElementChild.innerText

  const date = `${month}-${day}-${year}`

  const dateElement = document.createElement("div")
  dateElement.class = "step-four-top-header"
  dateElement.innerHTML =  `<p class="step-four-row-header"></p>`
  dateElement.innerHTML += `<p class="step-four-row-header" style="font-weight:bold;">Date of Pickup: ${date}</p>`

  const container = document.querySelector("div.pickup-summary-gray-box-wrapper div")
  const sibiling = container.querySelector("div.step-four-top-header")
  container.insertBefore(dateElement, sibiling)
  return dateElement
}

/* Start */
fillInitialFields()

document.querySelector("#webToolsAddressCheck").addEventListener("click", () => {
  const magicChange = new Event("change")

  fillDogField()       .dispatchEvent(magicChange)
  fillPackageLocation().dispatchEvent(magicChange)

  fillPickupTime() // Works through click

  setTimeout(() => {
    showPickupDateInStepFour(fillPickupDate())  // Need to wait for this (calendar) to load in first since it's loaded dynamically
    fillHazmat()
    fillTerms()
    scrollToDetails()
  }, 750)
})