Skip to content

Instantly share code, notes, and snippets.

@damien5314
Last active November 27, 2020 14:33
Show Gist options
  • Select an option

  • Save damien5314/4cd478ebdbac3dc51e40c66bb884fece to your computer and use it in GitHub Desktop.

Select an option

Save damien5314/4cd478ebdbac3dc51e40c66bb884fece to your computer and use it in GitHub Desktop.
User script for checking available inventory on a Newegg product search
// ==UserScript==
// @name Newegg Inventory Check
// @version 5
// @description Repeatedly refresh a given Newegg page, look for products in stock, and make a lot of noise on success
// @namespace https://gist.github.com/damien5314/4cd478ebdbac3dc51e40c66bb884fece
// @author https://github.com/damien5314 (forked from github.com/beporter)
// @match https://www.newegg.com/p/*
// @match https://secure.newegg.com/wishlist/md/*
// @grant none
// @run-at document-idle
// @require https://cdnjs.cloudflare.com/ajax/libs/howler/2.1.3/howler.min.js#sha256-/Q4ZPy6sMbk627wHxuaWSIXS1y7D2KnMhsm/+od7ptE=
// ==/UserScript==
(function() {
'use strict';
const scanDelay = 2;
const refreshDelay = 30;
var readySound = new window.Howl({
src: ['//freesound.org/data/previews/187/187404_635158-lq.mp3'],
autoplay: false,
loop: false,
volume: 1.0,
});
function scan() {
const results = document.evaluate(
"//div[@class='item-container']",
document,
null,
XPathResult.ANY_TYPE,
null
);
var found = false;
var itemContainer = results.iterateNext();
while (itemContainer != null) {
var foundMatch = false;
if (!scanForElement(".//p[contains(., 'OUT OF STOCK')]", itemContainer) &&
!scanForElement(".//span[contains(., 'OUT OF STOCK')]", itemContainer) &&
!scanForElement(".//button[contains(., 'AUTO NOTIFY')]", itemContainer) &&
!scanForElement(".//span[contains(., 'SOLD OUT')]", itemContainer)
) {
console.log(`[Newegg Inventory Check] Found an item container WITHOUT an out of stock notice`);
return itemContainer;
}
itemContainer = results.iterateNext();
}
console.log(`[Newegg Inventory Check] No products in stock`);
return null;
}
function scanForElement(xpath, itemContainer) {
var matches = document.evaluate(
xpath,
itemContainer,
null,
XPathResult.ANY_TYPE,
null
);
var match = matches.iterateNext();
return match;
}
function refreshInSecs(secs) {
window.setTimeout(() => {
window.location.reload(true);
}, secs * 1000);
}
function waitToScan(callback) {
window.setTimeout(() => {
callback();
}, scanDelay * 1000);
}
function locationStartsWithAnyOfUrls(urls) {
return urls.reduce((acc, url) => {
return acc || window.location.href.startsWith(url);
}, false);
}
function tryClickAddToCart(itemContainer) {
const button = scanForElement(".//button[contains(., 'ADD TO CART')]", itemContainer);
if (button == null) {
console.log(`Product found, but not "Add to Cart" present; itemContainer=`);
console.log(itemContainer);
return;
}
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('click', true, true);
button.dispatchEvent(clickEvent);
console.log(`Clicked "Add to Cart" for product`);
}
// function main()
waitToScan(() => {
var found = scan()
if (found != null) {
readySound.play();
tryClickAddToCart(found);
} else {
refreshInSecs(refreshDelay);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment