Skip to content

Instantly share code, notes, and snippets.

@dcollien
Created April 27, 2016 11:31
Show Gist options
  • Select an option

  • Save dcollien/d3c276ecb4dd2eec565608e502de855a to your computer and use it in GitHub Desktop.

Select an option

Save dcollien/d3c276ecb4dd2eec565608e502de855a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<head>
<script>
var start_indicator = 'START';
var lineNum = 0;
var render = function(newData) {
var content = '';
newData = newData.replace(/^\s+|\s+$/g, '');
if (newData === '') return '';
var lines = newData.split('\\n');
lines.forEach(function(line, i) {
if (line !== '' || i !== lines.length-1) {
content += '<span id="line' + lineNum + '"></span>' + line + '<br>';
lineNum++;
}
});
return content;
};
window.onload = function() {
var bodyContent = document.body.innerHTML;
document.body.style.background = '#222';
document.body.style.color = '#eee';
document.body.style.fontFamily = 'Monaco, monospace';
document.body.style.fontWeight = 'bold';
document.body.innerHTML = '<pre id="log-container">' + render(bodyContent) + '</pre>';
if (window.location.hash) {
window.location.href = window.location.hash;
}
setInterval(function() {
var isScrolled = false;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var body = xhr.responseText.split('<!-- ' + start_indicator + ' -->')[1];
var newData = body.replace(bodyContent, '');
var newHTML = render(newData);
if (newHTML) {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
isScrolled = true;
}
document.getElementById('log-container').innerHTML += newHTML;
if (isScrolled) {
window.scrollTo(0, document.body.scrollHeight);
}
}
bodyContent = body;
}
};
xhr.open("GET", location.href, true);
xhr.send();
}, 2500);
};
</script>
</head>
<!-- START -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment