aboutsummaryrefslogtreecommitdiffstats
path: root/misc/usps-fill-out-scheduled-pickup.user.js
blob: 7bf5811a16b9cfbcbfb0a72efeb68845a78de006 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// ==UserScript==
// @name        Fill out usps scheduled pickup
// @namespace   Violentmonkey Scripts
// @match       https://tools.usps.com/schedule-pickup-steps.htm*
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_registerMenuCommand
// @homepageURL https://git.tjkeller.xyz/userscripts/about
// @downloadURL https://git.tjkeller.xyz/userscripts/plain/misc/usps-fill-out-scheduled-pickup.user.js
// @version     2.0
// @author      tjkeller.xyz
// @description 2/11/2024, 7:23:34 PM
// ==/UserScript==

/* Save/load data */
const initialFields = [
	"addressLineOne",
	"addressLineTwo",
	"city",
	"emailAddress",
	"firstName",
	"isBusinessAddress",
	"lastName",
	"middleInitial",
	"phoneExt",
	"phoneNumber",
	"state",
	"zipCode",
]
const otherFields = [
	"first-radio-verification",
	"packageLocation",
	"second-radio-verification",
	"step-two-desc-item-textarea",
]
function isCheckable(input) { return input.type == "checkbox" || input.type == "radio" }
function saveFilledData(key, fieldNames) {
	const data = {}
	for (let fieldName of fieldNames) {
		const input = document.getElementById(fieldName)
		if (!input) {
			//alert(`Failed to save. Not all fields are filled. Make sure to continue after "Check Availability"`)
			alert(`Field "${fieldName}" not found`)
			return false
		}
		const label = input.previousElementSibling
		const required = label ? label.innerText.startsWith("*") : false // inputs not marked required
		data[fieldName] = isCheckable(input) ? input.checked : input.value
		if (required && !data[fieldName]) {
			input.focus()
			alert(`Failed to save. Missing required data for "${label.innerText.substring(1)}"`)
			return false
		}
	}
	GM_setValue(key, data)
	alert(`Saved successfully. Reload to check`)
	return true
}

GM_registerMenuCommand("Save Step 1 Fields", () => saveFilledData("initialFields", initialFields))
GM_registerMenuCommand("Save Step 2 Fields", () => saveFilledData("otherFields", otherFields))

/* Fill funcs */
const magicChange = new Event("change")
function fillFields(data) {
	for (let fieldName in data) {
		const input = document.getElementById(fieldName)
		if (isCheckable(input))
			input.checked = data[fieldName]
		else
			input.value = data[fieldName]
		input.dispatchEvent(magicChange)
	}
}
function fillInitialFields() {
	const data = GM_getValue("initialFields", {})
	fillFields(data)
}

function fillOtherFields() {
	const data = GM_getValue("otherFields", {})
	fillFields(data)
}

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

function fillPickupDate() {
	const f = document.querySelector("#schedule-pickup-cal td:not(:first-child):not(.ui-datepicker-unselectable):not(.holiday):not(.ui-state-disabled)") // 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 += `<mark class="step-four-row-header" style="font-weight:bold;">Date of Pickup: ${date}</mark>`

	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", () => {
	fillOtherFields()

	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)
})