Created
June 11, 2012 16:53
-
-
Save littlelazer/2911221 to your computer and use it in GitHub Desktop.
arstechnica liveblogging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var timer = null; | |
var status_timer = null; | |
var loaded = false; | |
var interval = 3 * 1000; | |
var status_interval = 60 * 1000; | |
var update = true; | |
var img_re = /^http[^\s]*\.(?:jpe?g|gif|png|bmp|svg)[^\/]*$/i; | |
var url_re = /(https?:\/\/[^\s<"]*)/ig; | |
var updatePosts = function(posts) { | |
timer = setTimeout(fetchPosts, interval); | |
$(posts).sort(function(a,b){ | |
return (a['id'] - b['id']); | |
}).each(function(i, post) { | |
if (post['delete']) { | |
$('#post-' + post['id']).remove(); | |
return; | |
} | |
if ($('#post-' + post['id']).length) return; | |
var postList = $('#ars-liveblog-posts'); | |
var item = $(post['html']); | |
if (loaded) { | |
item.addClass("new"); | |
loaded = true; | |
} | |
postList.prepend(item); | |
processPost(post); | |
}); | |
setTimeout(function(){ $('.ars-liveblog-post.new').removeClass("new") }, 1000); | |
}; | |
var processPost = function(post) { | |
linkify(document.getElementById("post-" + post['id'])); | |
imagify(post['id']); | |
cleanDate(post['id']); | |
if (post['sticky']) stickPost(post['id']); | |
}; | |
var stickPost = function(post_id) { | |
if ($('#sticky-'+post_id).length) return; | |
var stickies = $('#stickies ul'); | |
var post = $('#post-'+post_id); | |
if (post.length) { | |
var content = post.find('.post-content').html(); | |
var li = $('<li>'); | |
li.attr('id', 'sticky-'+post_id); | |
li.html(content); | |
stickies.append(li); | |
} | |
}; | |
var fetchStatus = function() { | |
clearTimeout(status_timer); | |
$.ajax({ | |
url: "status.json?"+(new Date()).getTime(), | |
dataType: "json", | |
success: function(data) { | |
update = data['updating']; | |
status_el = $('#status').get(0); | |
if (update) { | |
status_el.className = "updating"; | |
$('#not-started').remove(); | |
} else { | |
status_el.className = "stopped"; | |
} | |
status_timer = setTimeout(fetchStatus, status_interval); | |
}, | |
error: function(req) { | |
status_timer = setTimeout(fetchStatus, status_interval); | |
} | |
}); | |
}; | |
var fetchPosts = function() { | |
clearTimeout(timer); | |
if (!update) { | |
timer = setTimeout(fetchPosts, interval); | |
return; | |
} | |
$.ajax({ | |
url: "events.json?"+(new Date()).getTime(), | |
dataType: "json", | |
success: updatePosts, | |
error: function(req) { | |
timer = setTimeout(fetchPosts, interval); | |
} | |
}); | |
}; | |
var linkify = function(elem) { | |
var children = elem.childNodes; | |
var length = children.length; | |
for (var i=0; i < length; i++) { | |
var node = children[i]; | |
if (node.nodeName == "A") { | |
continue; | |
} | |
else if (node.nodeName != "#text") { | |
linkify(node); | |
} | |
else if (node.nodeValue.match(url_re)) { | |
var span = document.createElement("SPAN"); | |
var escaped = $('<div/>').text(node.nodeValue).html(); | |
span.innerHTML = escaped.replace( | |
url_re, '<a href="$1" target="_blank" rel="noreferrer">$1</a>'); | |
node.parentNode.replaceChild(span, node); | |
} | |
} | |
}; | |
var imagify = function(post_id) { | |
$('a').each(function(i, a) { | |
a = $(a); | |
var href = a.html(); | |
if (img_re.test(href)) { | |
var img = $('<img/>'); | |
img.on('load', function() { | |
a.html(img); | |
var width = img.width(), height = img.height(); | |
if (width > 500) { | |
img.removeAttr("width"); | |
img.removeAttr("height"); | |
img.attr("width", 500); | |
} | |
if (height > 300) { | |
img.removeAttr("width"); | |
img.removeAttr("height"); | |
img.attr("height", 300); | |
} | |
}); | |
img.attr('target', '_blank'); | |
img.attr('src', href); | |
} | |
}); | |
}; | |
var track_pageview = function(path) { | |
path = path||null; | |
try { | |
if(path === null) { | |
_gaq.push(['_trackPageview']); | |
} else { | |
_gaq.push(['_trackPageview', path]); | |
} | |
} catch(e) {} | |
try { | |
eval(s.t()); // omniture!!! | |
} catch(e) {} | |
try { | |
PARSELY.beacon.trackPageView({ | |
url: "" + window.location, | |
urlref: "", | |
js: 1, | |
action_name: "liveblog" | |
}); | |
} catch(e) {} | |
return false; | |
}; | |
var reload_ads = function() { | |
try{ | |
cnp.ad.manager.reloadAds(); | |
} catch(err) {} | |
return false; | |
}; | |
var pageview = function(path) { | |
path = path||null; | |
reload_ads(); | |
track_pageview(path); | |
return false; | |
}; | |
var months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]; | |
var cleanDate = function(post_id) { | |
var li = $('#post-' + post_id); | |
if (li.length) { | |
var date_display = li.find('.post-date'); | |
var seconds = li.attr("data-time"); | |
var date = new Date(seconds * 1000); | |
var month = months[date.getMonth()]; | |
var day = date.getDate(); | |
var year = date.getYear() + 1900; | |
var hours = String(date.getHours()); | |
var min = String(date.getMinutes()); | |
if (hours.length < 2) hours = "0" + hours; | |
if (min.length < 2) min = "0" + min; | |
date_display.html(hours + ":" + min); | |
date_display.attr('title', date.toString()); | |
} | |
}; | |
$(document).ready(fetchPosts); | |
$(document).ready(fetchStatus); | |
setInterval(pageview, 1000 * 60); | |
})(); | |
if (window.location.toString().match("sub")) { | |
ars.sidebar_ad = function(){}; | |
ars.masthead_ad = function(){}; | |
$(document).ready(function(){ | |
$('#stickies').css({"border-top" : "none", "padding-top": 0}); | |
$('#side-ad, #masthead-ad').remove() | |
}); | |
if (window.history.pushState) { | |
window.history.pushState({}, document.title, window.location.pathname); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment