Skip to content

Instantly share code, notes, and snippets.

@madzen
Created June 18, 2012 01:11
Show Gist options
  • Save madzen/2946267 to your computer and use it in GitHub Desktop.
Save madzen/2946267 to your computer and use it in GitHub Desktop.
Brewdog "Out of Stock" Remover - Hide all out of stock products on the Brewdog online store.
// ==UserScript==
// @name Brewdog "Out of Stock" Remover
// @description Live in blissful ignorance of the things you can't buy!
// @author Madzen
// @include http://www.brewdog.com/shop*
// @include https://www.brewdog.com/shop*
// @include http://www.brewdog.com/product*
// @include https://www.brewdog.com/product*
// @version 1.0
// ==/UserScript==
/******************************************************************************
* Copyright (c) 2012 Madzen
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*****************************************************************************/
/******************************************************************************
* There is quite a mixture of activity and inactivity on the Brewdog online
* shop from time to time, as special beers are announced, swathes of guest
* beers are added all at once (and immediately begin to disappear almost as
* fast as they are being posted) or as regular stock builds up and winds down.
*
* This script avoids some of the frustration by simply removing any options
* from the store which are out of stock. If just some options of a product are
* unavailable, it just removes the ones that aren't there. If all of them are
* out, the product is removed entirely from view.
*
* This script will work across the main shop link, the shop sub-sections and
* the specific product pages.
*****************************************************************************/
var removeClassName, products, numProd;
/**
* Determines the parent element of a particular node at a given depth.
*
* @param {Object} nodeName The name of the node to determine parents for.
* @param {Number} depth The depth the desired node is sitting at.
* @return {Object} The relevant parent node.
*/
function getParentNode(nodeName, depth) {
var node = nodeName;
for (var x = 0; x < depth; x++) {
node = node.parentNode;
}
return node;
}
/**
* Removes the given elements from within a wider element group.
*
* @param {Object} eList The wider element group to remove from.
* @param {String} eName The name of the element class to remove.
* @param {Number} depth The depth of the embedded element.
*/
function removeElement(eList, eName, depth) {
var elementList = eList.getElementsByClassName(eName),
numElements = elementList.length,
elementNode;
while (numElements--) {
elementNode = getParentNode(elementList[numElements], depth);
elementNode.style.display = 'none';
}
}
// The format is slightly different between the product page and the main shop
// so the location needs to be checked first.
if (window.location.pathname.split('/')[1] === 'product') {
removeClassName = 'product_versions';
}
else {
removeClassName = 'product_listing';
}
// Grab the list of the relevant products.
products = document.getElementsByClassName(removeClassName);
numProd = products.length;
while (numProd--) {
// Check whether the product contains any "Add to cart" options and if
// not, all versions are out of stock, so remove the entire product.
var regExp = /\+ Add to cart/;
if (products[numProd].innerHTML.search(regExp) === -1) {
products[numProd].style.display = 'none';
}
// If the option to add to cart was found, we need to dig a little deeper
// and only remove the specific options that are unavailable.
else {
// Find any products with the out of stock span class and remove them.
removeElement(products[numProd], 'outofstock', 2);
// Each out of stock product has an email notifier option that needs
// removed separately.
removeElement(products[numProd], 'email_reminder', 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment