Skip to content

Instantly share code, notes, and snippets.

@ento
Created June 17, 2020 01:57
Show Gist options
  • Save ento/0ca18aca9993da0cb513f2424b88b336 to your computer and use it in GitHub Desktop.
Save ento/0ca18aca9993da0cb513f2424b88b336 to your computer and use it in GitHub Desktop.
Add columns for monthly cost to AWS pricing tables
function findHourlyPriceColumns(table) {
for (const row of table.querySelectorAll("thead tr")) {
const headers = Array.from(row.querySelectorAll("th"));
const columns = headers.reduce(function(acc, current, index) {
if (/hourly/i.test(current.innerText) || /price per hour/i.test(current.innerText)) {
acc.push(index)
}
return acc;
}, []);
if (columns.length > 0) return {hourlyColumns: columns, totalColumns: headers.length};
}
return null;
}
function extractHourlyPrice(text) {
return parseFloat(text.match(/[0-9.]+/)[0]);
}
function insertMonthlyPrices(table, hourlyColumns) {
table.querySelectorAll("tbody tr").forEach(function(row) {
const cells = row.querySelectorAll("td");
if (cells.length != hourlyColumns.totalColumns) return;
hourlyColumns.hourlyColumns.forEach(function(index) {
const hourlyPrice = extractHourlyPrice(cells[index].innerText);
const monthlyPrice = (hourlyPrice * 24 * 30).toFixed(0);
const monthlyCell = document.createElement("td");
monthlyCell.innerText = `$${monthlyPrice}/mo`;
row.insertBefore(monthlyCell, cells[index].nextSibling);
})
})
}
document.querySelectorAll(".aws-table table").forEach(function(each) {
const columns = findHourlyPriceColumns(each)
if (!columns) return;
insertMonthlyPrices(each, columns)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment