-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-blog.js
34 lines (31 loc) · 1.15 KB
/
github-blog.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
function GithubBlog(postsRepo, blog_file) {
this.src = postsRepo;
this.blog_file = (!!blog_file)? blog_file : "index.blog.json";
this.makeInternalGetRequest = function(url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
callback(request.responseText);
} else {
callback({error: "Bad request"});
}
};
request.onerror = function() {
callback({error: "Connection error"});
};
request.send();
}
this.loadIndex = function(callback) {
this.makeInternalGetRequest(`https://raw.githubusercontent.com/${this.src}/master/${this.blog_file}`, function(response) {
if (response.error) {
callback(response);
} else {
callback(JSON.parse(response));
}
});
}
this.loadPost = function(postLink, callback) {
this.makeInternalGetRequest(`https://raw.githubusercontent.com/${this.src}/master/${postLink}`, callback);
}
}