Skip to content

Instantly share code, notes, and snippets.

@jdoconnor
Created February 24, 2023 18:16
Show Gist options
  • Save jdoconnor/469bd9d42c1fe8dc93df3ae903342394 to your computer and use it in GitHub Desktop.
Save jdoconnor/469bd9d42c1fe8dc93df3ae903342394 to your computer and use it in GitHub Desktop.

Revisions

  1. jdoconnor created this gist Feb 24, 2023.
    66 changes: 66 additions & 0 deletions safeway_just_for_u.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    // ==UserScript==
    // @name Just For U Coupon Clipper
    // @version 1
    // @grant GM_notification
    // @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==

    /// While the load more button exists, load more
    async function loadUntilDone() {
    // Wait for the page to load and then start collecting 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')
    }

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

    /// 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!");
    }

    async function informUser(){
    GM_notification({
    text: "All coupons have been clicked",
    title: "All done!",
    timeout: 3000
    });
    }

    await sleep(5000);
    await loadUntilDone()
    await informUser();