Created
May 27, 2022 14:16
-
-
Save jdoconnor/255093d874cf31151700198508abaa46 to your computer and use it in GitHub Desktop.
just for u coupon clipper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Just For U Coupon Clipper | |
// @version 1 | |
// @grant none | |
// @match https://www.safeway.com/foru/coupons-deals.html* | |
// @description Clip all the coupons on the current (as of 5/21/2020) Safeway Just For U coupon system. | |
// @namespace https://greasyfork.org/users/22981 | |
// ==/UserScript== | |
// forked from the original version (created by https://greasyfork.org/users/22981) | |
/// While the load more button exists, load more | |
async function loadUntilDone() { | |
// Wait for the page to load and then start collecting coupons | |
console.warn("Waiting to load coupons"); | |
let buttons = document.getElementsByClassName('load-more') | |
while (buttons.length > 0) { | |
// Still a load more button. Click until it goes away | |
console.warn("Loading more coupons...") | |
try { | |
buttons[0].click() | |
} catch (e) { | |
console.error(e) | |
} | |
await sleep(3000); | |
buttons = document.getElementsByClassName('load-more') | |
} | |
console.warn("done loading coupons"); | |
// Now find and click all the coupons | |
let unclickedButtons = document.getElementsByClassName('grid-coupon-btn'); | |
console.warn("Clicking " + unclickedButtons.length + " coupons"); | |
while (unclickedButtons.length > 3){ // clicking all appears to be a loop. unsure why | |
console.warn(unclickedButtons.length + " buttons left") | |
await clickAllUnclicked(unclickedButtons); | |
unclickedButtons = document.getElementsByClassName('grid-coupon-btn') | |
} | |
return ("done"); | |
} | |
/// 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('coupon-clip-add-to-list')) { | |
console.warn("Click element " + i + ": " + elem) | |
elem.click() | |
await sleep(100) | |
} | |
} | |
console.log("All coupons clicked!") | |
} | |
await sleep(5000); | |
loadUntilDone() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment