Skip to content

Instantly share code, notes, and snippets.

@klinquist
Created November 22, 2023 16:19
Show Gist options
  • Save klinquist/267fb3495d9fad4ffa0c1f89c8262531 to your computer and use it in GitHub Desktop.
Save klinquist/267fb3495d9fad4ffa0c1f89c8262531 to your computer and use it in GitHub Desktop.
Safeway Just4u clipper
function loadUntilDone() {
let buttons = document.getElementsByClassName('load-more')
if (buttons.length > 0) {
// Still a load more button. Click until it goes away
console.log("Loading more coupons...")
try {
buttons[0].click()
} catch (e) {
console.error(e)
}
// Give it some time to load.
setTimeout(loadUntilDone, 1000)
} else {
// Now find and click all the coupons
console.log("Clicking all coupons...")
clickAllUnclicked(document.querySelectorAll('.grid-coupon-btn'))
}
}
/// Resolve after the given delay
async function sleep(delay) {
return new Promise((resolve, reject) => {
setTimeout(resolve, delay)
})
}
/// Click on every element in the given collection, at a sensible pace, unless alredy clicked
async function clickAllUnclicked(elems) {
for (let i = 0; i < elems.length; i++) {
let elem = elems[i];
if (!elem.classList.contains('btn grid-coupon-btn btn-default')) {
console.log("Click element " + i + ": " + elem)
elem.click()
await sleep(100)
}
}
console.log("All coupons clicked!")
}
// Wait for the page to load and then start collecting coupons
console.log("Waiting to load coupons")
setTimeout(loadUntilDone, 4000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment