Skip to content

Instantly share code, notes, and snippets.

@beporter
Last active September 20, 2024 18:48
Show Gist options
  • Save beporter/ce76204bcba35d9edb66b395bb5e9305 to your computer and use it in GitHub Desktop.
Save beporter/ce76204bcba35d9edb66b395bb5e9305 to your computer and use it in GitHub Desktop.
Greasemonkey script to repeatedly refresh a given page, look for specific "Add to Cart" buttons, click them if present, and make a lot of noise on success.

Add Items to Cart

Greasemonkey script to repeatedly refresh a given page, look for specific "Add to Cart" buttons, click them if present, and make a lot of noise on success.

This script was concieved and created due to the Nintendo Switch shortage during the COVID-19 pandemic. Its original purpose was to monitor for the Nintendo Switch console to come back into stock and get it added to your cart as quickly as possible. Granted, you stand very little chance compared to scalpers using fully automated tools like Bird Bot.

Installation

  • Install Tampermonkey or Greasemonkey for your browser.
  • Install the script into your browser by clicking the "Raw" button in the top-right corner of the js script below.

Setup

  • Sign into your Amazon.com, BestBuy.com or Walmart.com account (or all three).
  • Make sure your payment and shipping info is up to date to save time later.
  • Add the item(s) you want to buy to a new "wishlist" or "save for later" list.
  • (This script operates on the assumption that the items are out of stock or not available. If they're available... then just buy them.)

Usage

TODO

  • Move the selectors into GM local storage to allow end users to customize settings instead of having to edit the script itself.
  • Register a browser menu button to toggle the search/refresh on/off per-site.
// ==UserScript==
// @name Add Saved Items to Cart
// @namespace https://gist.github.com/beporter/ce76204bcba35d9edb66b395bb5e9305
// @version 0.5
// @description Repeatedly refresh a given "saved items" page (Amazon, Walmart, BestBuy), look for specific "Add to Cart" buttons, click them if present, and make a lot of noise on success.
// @author https://github.com/beporter
// @match https://www.amazon.com/gp/registry/wishlist/*
// @match https://www.amazon.com/hz/wishlist/ls/*
// @match https://www.bestbuy.com/cart
// @match https://www.bestbuy.com/site/customer/lists/manage/saveditems
// @match https://www.walmart.com/lists*
// @grant none
// @run-at document-idle
// @require https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.3/howler.min.js#sha256-/Q4ZPy6sMbk627wHxuaWSIXS1y7D2KnMhsm/+od7ptE=
// @downloadURL https://gist.githubusercontent.com/beporter/ce76204bcba35d9edb66b395bb5e9305/raw/add_to_cart.user.js
// ==/UserScript==
(function() {
'use strict';
const SELECTORS = [
{
site: 'Walmart',
urls: ['https://www.walmart.com/lists'],
selector: 'div.ListDetails button.RegularItemTile-add-to-cart',
loadWait: 2,
cooldown: 5, // Set to 0 to disable the refresh.
active: true,
},
{
site: 'BestBuy',
urls: ['https://www.bestbuy.com/site/customer/lists/manage/saveditems'],
selector: '#your-saved-items .add-to-cart-button button:not([disabled])',
loadWait: 5,
cooldown: 5,
active: true,
},
{
site: 'Amazon',
urls: ['https://www.amazon.com/gp/registry/wishlist/', 'https://www.amazon.com/hz/wishlist/ls/'],
selector: 'div#my-lists-tab span[data-action=add-to-cart] a[role=button]',
loadWait: 5,
cooldown: 5,
active: true,
},
];
var readySound = new window.Howl({
src: ['//freesound.org/data/previews/187/187404_635158-lq.mp3'],
autoplay: false,
loop: true,
volume: 1.0,
});
// Scan the page for the provided selector and "click" them if present.
function triggerClicks(sel) {
var anyClicked = false;
const buttons = document.querySelectorAll(sel.selector);
// No available "Add to Cart" buttons. Cool down and refresh.
if (!buttons.length) {
console.log(`${sel.site}: No active "Add to Cart" buttons.`);
return anyClicked;
}
buttons.forEach((b) => {
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('click', true, true);
b.dispatchEvent(clickEvent);
console.log(`${sel.site}: Clicked "Add to Cart" button.`);
anyClicked = true;
});
return anyClicked;
}
function refreshInSecs(secs) {
console.log(`Scheduling page refresh in ${secs} secs.`);
window.setTimeout(() => {
window.location.reload(true);
}, secs * 1000);
}
function waitToClick(sel, callback) {
console.log(`Scheduling clicks for ${sel.site}.`);
window.setTimeout(() => {
callback(sel);
}, sel.loadWait * 1000);
}
function locationStartsWithAnyOfUrls(urls) {
return urls.reduce((acc, url) => {
return acc || window.location.href.startsWith(url);
}, false);
}
// function main()
SELECTORS.forEach((sel) => {
if (sel.active && locationStartsWithAnyOfUrls(sel.urls)) {
waitToClick(sel, (sel) => {
if (triggerClicks(sel)) {
readySound.play();
} else if (sel.cooldown) {
refreshInSecs(sel.cooldown);
}
});
}
});
})();
@StarLordRammi
Copy link

StarLordRammi commented Apr 13, 2021

is it possible to add this site please?
https://www.cclonline.com/product/346125/GV-R67XTEAGLE-12GD/Graphics-Cards/Gigabyte-Radeon-RX-6700-XT-EAGLE-12GB-Graphics-Card/VGA6121/

no wishlist. just need to click Add to cart button

@Yaidier-br
Copy link

If you can help me for a store that sells at a specific time and does not have a wish list.

@hewilliam
Copy link

this is working great but i have to keep solving captcha's after a while. it would be awesome if we can put in a 2captcha.com key to bypass the captchas. none the less, this is awesome work!

@kkapuria3
Copy link

Hi !

check this for for best buy !

https://github.com/kkapuria3/BestBuy-GPU-Bot

@SgtPooki
Copy link

SgtPooki commented Nov 8, 2021

Walmart was doing an online-only Xbox series x sale (2021 NOV 05 15:00 ET). I attempted to use this script, but had to make the following changes:

  1. Add a different url since the lists page doesn't seem to have add to cart buttons anymore.
  2. Update the add to cart selector. See https://gist.github.com/SgtPooki/868d30d70b5a51922e371d6542b58d4d#file-add_to_cart-user-js-L26

@azineth
Copy link

azineth commented Dec 11, 2021

hello, le add-to-cart don't work for amazon.
the selector : selector: 'div#my-lists-tab span[data-action=add-to-cart] a[role=button]', not work !

buttons. or button. , normaly buttons.

// No available "Add to Cart" buttons. Cool down and refresh.
if (!buttons.length) {
console.log(${sel.site}: No active "Add to Cart" buttons.);
return anyClicked;
}

    buttons.forEach((b) => {
        var clickEvent = document.createEvent('MouseEvents');
        clickEvent.initEvent('click', true, true);
        b.dispatchEvent(clickEvent);
        **console.log(`${sel.site}: Clicked "Add to Cart" button.`);**
        anyClicked = true;
    });

Thank you for help !!!

@ddizzle22
Copy link

Would this be possible to get in touch to see about using this script on another site where an item is out of stock but then is loaded in and it automatically puts it to final checkout screen?

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