Save the following code as xkcd.html and point your browser to it as its home page.
When it activates it will ask for a comic number. If you want the most recent then just click OK.
I got it from here while looking for a widget that did the same thing. Thinking about it, I decided that having xkcd as my opening home page was a better idea than having a cron task which may not run when I first turn on my laptop or a widget burning clock cycles in some loop, waiting to fire.
Code:
<!DOCTYPE html>
<html>
<head>
<title>xkcd via JSONP</title>
<script type="text/javascript">
var details = {};
function dataloaded(o){details = o;}
function pageloaded()
{
with(document.getElementById("image"))
{
src = details.img;
alt = details.title;
title = details.alt;
}
document.getElementById("title").innerHTML = details.title;
document.getElementById("num").innerHTML += details.num;
document.getElementById("date").innerHTML = details.day + "/" + details.month + "/" + details.year;
document.getElementById("mouseover").innerHTML = details.alt;
//Format Transcript
var str = details.transcript;
//Fix < and >
str = str.replace(/</g, '<')
str = str.replace(/>/g, '>')
//Fix newlines
str = str.replace(/\n/g, '<br>\n');
//Add extra breaks before bracketed sections
str = str.replace(/[[]{2}/g, '<br>\n[[');
//Remove alt text from transcript
str = str.replace(/{.*}/g, '');
document.getElementById("transcript").innerHTML = str;
var url = "http://xkcd.com/" + num;
with(document.getElementById("original"))
{
href = url;
innerHTML = url;
}
}
var num = prompt("Comic number");
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src = "http://dynamic.xkcd.com/api-0/jsonp/comic/"+num+"?callback=dataloaded";
head.appendChild(script);
</script>
</head>
<body onload="pageloaded()">
<h2 id="title"></h2>
<h3 id="num">#</h3>
<div id="date"></div><br>
<img id="image" style="border: 1px solid #888; padding: 10px;">
<!-- img id="image" style="border: 5px solid #000;"-->
<h3>Mouseover</h3>
<div id="mouseover"></div>
<!-- hr style="border: 1px solid #00F;" -->
<h3>Transcript</h3>
<div id="transcript" style="width: 50%;" ></div>
<br>
<a id=original></a>
<br><hr><br>
</body>
</html>
I got it from here while looking for a widget that did the same thing. Thinking about it, I decided that having xkcd as my opening home page was a better idea than having a cron task which may not run when I first turn on my laptop or a widget burning clock cycles in some loop, waiting to fire.





Comment