Skip to content

Instantly share code, notes, and snippets.

@mmattozzi
Created April 7, 2012 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmattozzi/2330716 to your computer and use it in GitHub Desktop.
Save mmattozzi/2330716 to your computer and use it in GitHub Desktop.
(function() {
if (document.URL.match(/beermenus\.com\/places/)) {
var trs = document.getElementsByTagName("tr");
var minPpoz = null;
for (var i = 0; i < trs.length; i++) {
var tds = trs[i].children;
var name = null;
var serving = null;
var abv = null;
var price = null;
var priceElem = null;
for (var j = 0; j < tds.length; j++) {
var td = tds[j];
var clazz = td.getAttribute("class");
if (clazz != null) {
if (clazz == "serving") {
serving = td.innerText;
} else if (clazz == "abv") {
abv = parseFloat(td.innerText);
} else if (clazz == "price") {
var parsePrice = td.innerText.match(/\d+\.\d+/);
if (parsePrice) {
price = parseFloat(parsePrice);
}
priceElem = td;
}
} else {
if (td.children && td.children.length > 0 && td.children[0].tagName == "A") {
name = td.children[0].innerText;
}
}
}
if (name != null && serving != null && abv != null && ! isNaN(abv) && price != null) {
var servingMatch = serving.match(/(\d+)oz\. ((\d+) Pack)?/);
if (servingMatch) {
var oz = servingMatch[1];
var pack = 1;
if (servingMatch[3]) {
pack = parseInt(servingMatch[3]);
}
var ppoz = price / (oz * pack * abv * .01);
var ppozDiv = document.createElement("div");
ppozDiv.setAttribute("style", "color: blue");
ppozDiv.innerText = "$" + ppoz.toFixed(2) + " ppoza";
priceElem.appendChild(ppozDiv);
if (minPpoz == null || minPpoz.price > ppoz) {
minPpoz = { name: name, price: ppoz };
}
}
}
}
if (minPpoz) {
var p = document.getElementsByTagName("p");
for (var i = 0; i < p.length; i++) {
if (p[i].getAttribute("class") && p[i].getAttribute("class") == "left") {
var ppozDiv = document.createElement("div");
ppozDiv.setAttribute("style", "color: blue");
ppozDiv.innerText = "Best value: " + minPpoz.name;
p[i].appendChild(ppozDiv);
}
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment