Skip to content

Instantly share code, notes, and snippets.

@Sneagan
Forked from blach/airpod_availability.html
Last active January 2, 2017 20:22
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 Sneagan/97f870f71a4a0d664f43777b8cb8297d to your computer and use it in GitHub Desktop.
Save Sneagan/97f870f71a4a0d664f43777b8cb8297d to your computer and use it in GitHub Desktop.
Checks AirPod store availability by using JavaScript to query Apple's servers. Created with Textastic and works great with its built-in web preview. Uses YQL and JSONP to work around JavaScript's same origin policy. Inspired by https://gist.github.com/omarshahine/f8eb4598af4f1767ab1a9f01662e74b1
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
<title>Check AirPod availability in Apple Stores</title>
<style>
body {
font-size: 12px;
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
padding: 1em;
}
table {
max-width: 100%;
width: 500px;
text-align: left;
border-collapse: collapse;
border: 1px solid #69c;
margin-bottom: 1em;
}
table th {
padding: .5em;
font-weight: normal;
font-size: 1.1em;
color: #039;
border-bottom: 1px solid #69c;
}
table td {
padding: .5em;
color: #669;
font-size: 1em;
}
</style>
<!-- using jQuery to easily make JSONP calls -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
function insertHeaderRow(thead, text) {
var row = thead.insertRow(-1);
var th = document.createElement("th");
th.innerHTML = text;
th.colSpan = 2;
row.appendChild(th);
}
function insertRow(tbody, text1, text2) {
var row = tbody.insertRow(-1);
var cell1 = row.insertCell(0);
cell1.innerHTML = text1;
var cell2 = row.insertCell(1);
cell2.innerHTML = text2;
}
function checkZipCode(country, zip, partnum) {
var table = document.createElement("table");
document.body.appendChild(table);
var header = table.createTHead();
var tbody = table.createTBody();
insertHeaderRow(header, "Checking AirPods for " + country.toUpperCase() + ", " + zip);
// the JSON url to get
// see http://brianbrunner.com/2016/09/20/iphone-pickup.html for details
var url = "http://www.apple.com/" + country + "/shop/retail/pickup-message?parts.0=" + partnum + "&location=" + zip;
// use YQL (Yahoo! Query Language) to circumvent JavaScript cross origin limitations
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&format=json&callback=?';
//console.log(url);
// use JQuery to make a JSONP call to YQL
$.getJSON(yql, function(data) {
var json = data.query.results.body.content;
var obj = JSON.parse(json);
obj.body.stores.forEach(function(store, index) {
var availability = store.partsAvailability[partnum].storePickupQuote;
insertRow(tbody, store.city, availability);
});
});
}
function checkAvailability() {
checkZipCode("us", "95112", "MMEF2AM/A");
}
</script>
</head>
<body onload="checkAvailability()">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment