Skip to content

Instantly share code, notes, and snippets.

@edhedges
Created July 23, 2012 20:16
Show Gist options
  • Save edhedges/3165979 to your computer and use it in GitHub Desktop.
Save edhedges/3165979 to your computer and use it in GitHub Desktop.
This is a little script I started to inject a random /r/aww image into my 404 page.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>edTest</title>
</head>
<body>
<img id="random_image" alt="random aww image" />
</body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var random_image = "";
var json_url = "http://www.reddit.com/r/aww/.json?jsonp=?";
$.ajax({
url: json_url,
dataType: 'json',
success: function(data) {
var post_number = 0;
var url_posted = "";
var url_split = [];
var not_imgur = true;
var imgur_url_path = "";
var imgur_hash = "";
while(not_imgur){
post_number = Math.floor(Math.random()*25);
url_posted = data.data.children[post_number].data.url;
url_split = url_posted.split("/");
if(url_split[2].indexOf("imgur") != -1){
not_imgur = false;
imgur_url_path = url_split[3];
if(imgur_url_path.indexOf(".") != -1){
random_image = url_posted;
}
else{
imgur_hash = imgur_url_path;
convertImgurHash(imgur_hash);
}
}
}
$("img#random_image").attr("src", random_image);
}
});
});
function convertImgurHash(imgur_hash){
var json_url = "http://api.imgur.com/2/image/" + imgur_hash + ".json";
$.ajax({
url: json_url,
dataType: 'json',
success: function(data) {
var random_image = data.image.links.original;
$("img#random_image").attr("src", random_image);
}
});
}
</script>
</html>​​​​​​​​​​​​​​​​​​​​​​​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment