Skip to content

Instantly share code, notes, and snippets.

@OZARIN
Created September 20, 2018 23:40
Show Gist options
  • Select an option

  • Save OZARIN/aa8eed669255fc625745cc918ab0e599 to your computer and use it in GitHub Desktop.

Select an option

Save OZARIN/aa8eed669255fc625745cc918ab0e599 to your computer and use it in GitHub Desktop.
Fife Council Bin Collection
<!doctype html>
<html lang="en">
<head>
<title>Fife Council Bin Collection</title>
<style>
.list {
list-style: none;
margin: 0;
padding: 0;
}
/* only display 5 items */
.collection:nth-child(n + 6) {
display: none;
}
.binList {
margin-bottom: 20px;
}
.date {
margin: 0 0 10px;
}
</style>
</head>
<body>
<div id="root">
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
//the url extension with your specific address details - example provided
var name = '?fuseaction=binroutes.display&postcode=KY7%206AA&proid=320037449&requesttimeout=10000#routefinder';
//the council website - check here first
$.get('https://www.fifedirect.org.uk/env_bins/index.cfm' + name, function(response) {
//create empty array, ready to use
var bins = [];
//we don't need images - remove references to <img /> from the data
response = response.replace(/<img[^>]*>/g, '');
//within the response, find the following markup and push our desired date to our empty array
$(response).find('table > tbody > tr').each(function (index, row) {
var resultData = $(row).find('.resultData');
if (resultData.length >= 2) {
var date = ('Date', $(resultData[0]).text());
var bin = ('Bins', $(resultData[1]).text());
function justBin(str) {
return str.split(/\s+/).slice(1,2).join();
}
function justDate(str) {
return str.split(/\s+/).slice(1,4).join(' ');
}
date = justDate(date);
bin = justBin(bin.toLowerCase());
bins.push({ date, bin })
}
})
var app = document.getElementById('root');
var list = document.createElement('ul');
list.setAttribute('class', 'list');
app.appendChild(list);
var collections = bins.length;
var date = '';
var bintype = '';
for (var bin = 0; bin < collections; bin++) {
if (date != (bins[bin].date)) {
date = (bins[bin].date);
var li = document.createElement('li');
li.setAttribute('class', 'collection');
list.appendChild(li);
var h2 = document.createElement('h2');
h2.className += 'date';
h2.textContent = date;
li.appendChild(h2);
var subUl = document.createElement('ul');
subUl.setAttribute('class', 'binList');
li.appendChild(subUl);
}
binType = (bins[bin].bin) + ' bin';
var subLi = document.createElement('li');
subLi.setAttribute('class', 'bin ' + binType);
subLi.textContent = binType;
subUl.appendChild(subLi);
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment