Skip to content

Instantly share code, notes, and snippets.

@raxityo
Last active March 27, 2024 13:59
Show Gist options
  • Save raxityo/c82abe49b77d5feaeb5cf6b69f0e0d9d to your computer and use it in GitHub Desktop.
Save raxityo/c82abe49b77d5feaeb5cf6b69f0e0d9d to your computer and use it in GitHub Desktop.
Clip all available BJ's coupons
// ==UserScript==
// @name BJ's Coupon Clipper
// @namespace Violentmonkey Scripts
// @match https://www.bjs.com/
// @grant none
// @version 1.0
// @author @raxityo
// @description 3/24/2024, 11:01:30 PM
// ==/UserScript==
/**
* Clip all available BJ's coupons
* - Login to BJ's website
* - Open dev console and paste this function
* - Run it! Example `await clipAllOffers()`
* @return {Promise<T>}
*/
(async function clipAllOffers() {
const membershipNumber = localStorage.getItem("x_MembershipNumber");
const zipcode = JSON.parse(
localStorage.getItem("clubDetailsForClubId")
).postalCode;
await fetch(
"https://api.bjs.com/digital/live/api/v1.0/member/available/offers",
{
method: "post",
credentials: "include",
body: JSON.stringify({
membershipNumber,
zipcode,
category: "",
isPrev: false,
isNext: true,
pagesize: 500,
searchString: "",
indexForPagination: 0,
brand: ""
})
}
)
.then((r) => r.json())
.then(([{ availableOffers }]) => {
// Intentionally doing sequential requests to avoid hammering the backend
availableOffers.forEach(async ({ offerId, storeId }) => {
await fetch(
`https://api.bjs.com/digital/live/api/v1.0/store/${storeId}/coupons/activate?zip=07302&offerId=${offerId}`,
{
credentials: "include"
}
);
});
});
})().then();
@TheMarf
Copy link

TheMarf commented Mar 23, 2024

Love this code, was able to get it to work like a charm. Not very versed in js, is there any easy way to save this to a bookmark or something to easily run in firefox?

Edit: tried making a bookmark with the following...don't think it worked. Will try again in a couple days when coupons refresh.

javascript:(async function clipAllOffers() { const membershipNumber = localStorage.getItem('x_MembershipNumber') const zipcode = JSON.parse(localStorage.getItem('clubDetailsForClubId')).postalCode await fetch('https://api.bjs.com/digital/live/api/v1.0/member/available/offers', { method: 'post', credentials: 'include', body: JSON.stringify({ membershipNumber, zipcode, 'category': '', 'isPrev': false, 'isNext': true, 'pagesize': 500, 'searchString': '', 'indexForPagination': 0, 'brand': '' }) }) .then(r => r.json()) .then(([{ availableOffers }]) => { // Intentionally doing sequential requests to avoid hammering the backend availableOffers.forEach(async ({ offerId, storeId }) => { await fetch( https://api.bjs.com/digital/live/api/v1.0/store/${storeId}/coupons/activate?zip=07302&offerId=${offerId}`, { credentials: 'include' } ) }) }) })().then()`

@raxityo
Copy link
Author

raxityo commented Mar 25, 2024

@TheMarf I have just converted this to a UserScript, so you can use it in FireFox or other browsers using plugins such as Tampermonkey or ViolentMonkey and installing the script as a UeserScript. Doing so would automatically run the script whenever you visit bjs.com.

@TheMarf
Copy link

TheMarf commented Mar 27, 2024

@raxityo Great idea. Thanks for the tip and the code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment