-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrafficcam.js
66 lines (54 loc) · 2.44 KB
/
trafficcam.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
javascript:(function(){
var pageTitle = 'Calgary Traffic Cams';
var refreshEvery = 30; // seconds
// City of Calgary currently has 79 (somewhat) active traffic cameras
var numImages = 79;
var baseUrl = 'http://trafficcam.calgary.ca/loc%d.jpg';
// Open a new window for the traffic cams
var newWindow = window.open('', pageTitle);
newWindow.document.head.innerHTML = '<title>' + pageTitle + '</title>';
/**
* Add the images to the page
*/
function addImages() {
// Clear the contents of this page in case that someone re-runs this script (i.e. if there already is a 'Calgary Traffic Cams' page)
if (newWindow.document.body !== null) {
newWindow.document.body.innerHTML = '';
}
for (var i = 0; i <= numImages; i++) {
var imgUrl = baseUrl.replace(/%d/, i);
newWindow.document.write('<img id="cam_' + i + '" style="float: left; margin: 5px;" src="' + imgUrl + '" /> ');
}
}
/**
* Refresh the images in place on the page
*/
function refreshImages() {
for (var i = 0; i <= numImages; i++) {
// Append a timestamp to the url to force the browser not to cache it
var timestamp = new Date().getTime();
var imgUrl = baseUrl.replace(/%d/, i) + '?' + timestamp;
newWindow.document.getElementById('cam_' + i).src = imgUrl;
}
}
/**
* Update the refresh countdown
*/
function updateRefreshCountdown() {
if (newWindow.document.getElementById('refresh_counter') == null) {
newWindow.document.write('<div id="refresh_counter" data-elapsed="0" data-max="' + refreshEvery + '" style="font-size: 11px; position: absolute; top: 0; right: 0; border: 1px solid #aaa; padding: 2px 3px; margin: 1px;"></div>');
}
var refreshCounter = newWindow.document.getElementById('refresh_counter');
var elapsedSeconds = parseInt(refreshCounter.getAttribute("data-elapsed")) + 1;
var totalSeconds = parseInt(refreshCounter.getAttribute("data-max"));
if (elapsedSeconds >= totalSeconds) {
elapsedSeconds = 0;
refreshImages();
}
refreshCounter.setAttribute("data-elapsed", elapsedSeconds);
refreshCounter.innerHTML = (totalSeconds - elapsedSeconds) + ' seconds until refresh';
}
addImages();
updateRefreshCountdown();
setInterval(updateRefreshCountdown, 1000);
})();