-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfront-page-jquery.js
132 lines (108 loc) · 4.2 KB
/
front-page-jquery.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
$(document).ready(function(){
$("#submit").click(function(){
getInfo($("#url")[0].value,$("#returnEmbed"),"fill");
var text = getSnippet($("#url")[0].value);
$("#returnText").html(text);
});
var passedUrl = getQueryVariable();
if(passedUrl.length > 0){
console.log(passedUrl);
getInfo(passedUrl,$("#returnEmbed"),"fill");
//getInfo(passedUrl,$("#returnEmbed"),"prepend");
}
getFrontPageLink();
setInterval(function(){
$(".item").each(function(){updateInfo($(this));});
},5000);
});
var getSnippet = function(url){
return "<div class="reddit_post"><a href=""+url+""></a></div><script src="https://rawgit.com/jhaenchen/ReddBed/master/src.js"></script>";
};
var updateInfo = function(toUpdate){
var postUrl = $(toUpdate).find(".title").children("a").attr("href");
console.log(postUrl);
getInfo(postUrl,toUpdate,"replace");
};
var getQueryVariable = function()
{
var query = window.location.search.substring(1);
var vars = query.split("&");
return vars;
};
var getFrontPageLink = function(){
$.ajax({
url: 'http://api.reddit.com/',
dataType: 'json',
success: function(response) {
//console.log("Front page link:" + response);
console.log("Front page:" + response.data.children[0].data);
var posts = response.data.children;
var frontPost;
for (i = 0; i < response.data.children.length-1; i++) {
if (response.data.children[i].data.url.length > 0) {
frontPost = response.data.children[i].data.url;
getInfo(frontPost,$("#sample"),"prepend");
};
}
}
});
};
var getInfo = function(url,object,mode){
$.ajax({
url: 'http://www.reddit.com/api/info.json?url='+url,
dataType: 'json',
success: function(response) {
document.getElementById("loading").style.display = 'none';
//$("#loading").hide();
console.log("received: " + response);
console.log(response.data.children[0].data);
var post = response.data.children[0].data;
var html = createEmbed(post.title,post.url,post.domain,post.created_utc,post.author,post.subreddit,post.num_comments,post.score,post.thumbnail,post.permalink);
switch(mode){
case "replace":
object.replaceWith(html);
break;
case "prepend":
object.prepend(html);
break;
case "fill":
object.html(html);
break;
default:
break;
}
}
});
};
var createEmbed = function(title,url,domain,date,user,subreddit,commentCount,score,thumbnail,link){
return "<div class=\"item\"><div class=\"vote_container\"><div class=\"vote up\"></div><p class=\"votes\">"+score+"</p><div class=\"vote down\"></div></div><div class=\"thumbnail\"><img src=\""+thumbnail+"\"></div><div class=\"details\"><p class=\"title\"><a href=\""+url+"\">"+title+"</a><span style=\"font-size:10px; color: rgb(136, 136, 136);\">("+domain+")</span></p><p class=\"tagline\">submitted <time title=\""+date+"\" class=\"live-timestamp\">"+timeSince(date)+" ago</time> by <a href=\"http://www.reddit.com/user/"+user+"\">"+user+"</a> to <a href=\"http://www.reddit.com/r/"+subreddit+"/\">/r/"+subreddit+"</a></p><p class=\"comment\"><a href=\"http://www.reddit.com"+link+"\"<b>View "+commentCount+" comments</b></p></div></div>";
};
function timeSince(date) {
var seconds = Math.floor(((new Date().getTime()/1000) - date)),
interval = seconds / 31536000;
var floor = Math.floor(interval);
if (floor > 1){
return floor + " years, "+Math.floor((interval%1)*12)+"months";
};
interval = seconds / 2592000;
var floor = Math.floor(interval);
if (floor > 1){
return floor + " months, "+Math.floor((interval%1)*30)+"days"; //i know this is bad, but it's a boring bug. someone else can fix it.
};
interval = seconds / 86400;
var floor = Math.floor(interval);
if (floor >= 1){
return floor + " days, "+Math.floor((interval%1)*24)+"hours";
};
interval = seconds / 3600;
var floor = Math.floor(interval);
if (floor >= 1){
return floor + " hours, "+Math.floor((interval%1)*60)+" minutes";
};
interval = seconds / 60;
var floor = Math.floor(interval);
if (floor > 1){
return floor + " minutes, "+Math.floor((interval%1)*60)+" seconds";
};
return Math.floor(seconds) + " seconds";
}