Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HoraceShmorace/58ed4211bd3cd48d5316965a611572ee to your computer and use it in GitHub Desktop.
Save HoraceShmorace/58ed4211bd3cd48d5316965a611572ee to your computer and use it in GitHub Desktop.
Amazon.com: Check for delivery slots for Fresh and Whole Foods
const ALLOW_AUTO_PURCHASE = false
const CHECKOUT_URL = 'https://www.amazon.com/gp/buy/shipoptionselect/handlers/display.html?hasWorkingJavascript=1'
const { pathname } = location
const isShipOptionPage = /buy\/shipoptionselect/.test(pathname)
const isPaySelectPage = /buy\/payselect/.test(pathname)
const isPurchasePage = /buy\/spc/.test(pathname)
const isThankYouPage = /buy\/thankyou/.test(pathname)
let attemptNum = 1
const doAmazonPurchaseFlow = () => {
console.log(`Amazon Purchase attempt #${attemptNum++} STARTED...`)
if (isShipOptionPage) doShipOptionPage()
else if (isPaySelectPage) doPaySelectPage()
else if (isPurchasePage) doPurchasePage()
else if (isThankYouPage) doThankYouPage()
else location.href = CHECKOUT_URL
}
const doShipOptionPage = () => {
var availabilityButton = document.querySelector('.ufss-limited-available .ufss-slot-toggle-native-button')
if (!availabilityButton) {
console.log(`Amazon Purchase attempt #${attemptNum} FAILED :(`)
return
}
const title = 'Amazon Order Started!'
const body = `Found open delivery window on attempt #${attemptNum}!`
doNotification(title, body)
var availabilityButton = document.querySelector('.ufss-limited-available .ufss-slot-toggle-native-button')
const continueButton = document.querySelector('input.a-button-input')
availabilityButton.click()
setTimeout(() => continueButton.click(), 100)
}
const doPaySelectPage = () => {
const continueButton = document.querySelector('#continue-top')
continueButton.click()
}
const doPurchasePage = () => {
const orderButton = document.querySelector('input[value="Place your order"')
if (ALLOW_AUTO_PURCHASE) return orderButton.click()
const title = 'Amazon Order Ready!'
const body = 'Amazon order ready for purchase!'
doNotification(title, body)
}
const doThankYouPage = () => {
const title = 'Amazon Order Placed!'
const body = document.querySelector('.a-alert-success span.a-size-medium.a-color-success.a-text-bold').innerText
doNotification(title, body)
}
const doNotification = (title, body) => {
console.log(body)
Notification.requestPermission().then(permission => permission === 'granted' &&
new Notification(title, {
icon: 'https://www.amazon.com/favicon.ico',
tag: 'amzn-availability',
renotify: true,
requireInteraction: true,
silent: false,
body: body
})
)
}
doAmazonPurchaseFlow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment