Skip to content

Instantly share code, notes, and snippets.

@SgtPooki
Forked from beporter/add_to_cart.user.js
Last active November 8, 2021 20:19
Show Gist options
  • Save SgtPooki/868d30d70b5a51922e371d6542b58d4d to your computer and use it in GitHub Desktop.
Save SgtPooki/868d30d70b5a51922e371d6542b58d4d 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*
// @match https://www.walmart.com/*
// @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', 'https://www.walmart.com/ip/seot/443574645'],
selector: 'button[type="button"][aria-label*="Add to cart"]',
loadWait: 2,
cooldown: 3, // 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: false,
},
{
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: false,
},
];
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);
}
});
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment